Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:Adapter,MatrixFarm #1372

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/adapters/matrix-farm/arbitrum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { getMatrixFarmBalances } from '@adapters/matrix-farm/common/balance'
import { getMatrixVaults } from '@adapters/matrix-farm/common/vault'
import type { AdapterConfig, BaseContext, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'

export const getContracts = async (ctx: BaseContext) => {
const pools = await getMatrixVaults(ctx)

return {
contracts: { pools },
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
pools: getMatrixFarmBalances,
})

return {
groups: [{ balances }],
}
}

export const config: AdapterConfig = {
startDate: 1661904000,
}
26 changes: 26 additions & 0 deletions src/adapters/matrix-farm/base/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { getMatrixFarmBalances } from '@adapters/matrix-farm/common/balance'
import { getMatrixVaults } from '@adapters/matrix-farm/common/vault'
import type { AdapterConfig, BaseContext, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'

export const getContracts = async (ctx: BaseContext) => {
const pools = await getMatrixVaults(ctx)

return {
contracts: { pools },
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
pools: getMatrixFarmBalances,
})

return {
groups: [{ balances }],
}
}

export const config: AdapterConfig = {
startDate: 1698019200,
}
45 changes: 45 additions & 0 deletions src/adapters/matrix-farm/common/balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { Balance, BalancesContext, Contract } from '@lib/adapter'
import { mapMultiSuccessFilter } from '@lib/array'
import { abi as erc20Abi } from '@lib/erc20'
import { multicall } from '@lib/multicall'
import { getUnderlyingBalances } from '@lib/uniswap/v2/pair'
import { parseEther } from 'viem'

const abi = {
getPricePerFullShare: {
inputs: [],
name: 'getPricePerFullShare',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
} as const

export async function getMatrixFarmBalances(ctx: BalancesContext, pools: Contract[]): Promise<Balance[]> {
const [userShares, pricePerFullShares] = await Promise.all([
multicall({
ctx,
calls: pools.map(({ address }) => ({ target: address, params: [ctx.address] }) as const),
abi: erc20Abi.balanceOf,
}),
multicall({ ctx, calls: pools.map((pool) => ({ target: pool.address }) as const), abi: abi.getPricePerFullShare }),
])

const poolBalances: Balance[] = mapMultiSuccessFilter(
userShares.map((_, i) => [userShares[i], pricePerFullShares[i]]),

(res, index) => {
const pool = pools[index]
const [{ output: share }, { output: pricePerFullShare }] = res.inputOutputPairs
const assetBalance = BigInt(share * pricePerFullShare) / parseEther('1.0')

return {
...(pool as Balance),
amount: assetBalance,
category: 'farm',
}
},
)

return getUnderlyingBalances(ctx, poolBalances, { getAddress: (pool) => pool.token! })
}
35 changes: 35 additions & 0 deletions src/adapters/matrix-farm/common/vault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { BaseContext, Contract } from '@lib/adapter'
import { mapSuccessFilter } from '@lib/array'
import { multicall } from '@lib/multicall'
import { getPairsDetails } from '@lib/uniswap/v2/factory'

const abi = {
want: {
inputs: [],
name: 'want',
outputs: [{ internalType: 'contract IERC20', name: '', type: 'address' }],
stateMutability: 'view',
type: 'function',
},
} as const

const API_URL: string = 'https://api-v2.matrix.farm/statistics/latest'

export async function getMatrixVaults(ctx: BaseContext): Promise<Contract[]> {
const {
tvl: { vaults },
} = await fetch(API_URL).then((res) => res.json())

const rawPools: Contract[] = vaults
.filter((vault: Contract) => vault.chain === ctx.chain)
.map(({ chain, address, provider }: Contract) => ({ chain, address, provider }))

const lpTokens = await multicall({
ctx,
calls: rawPools.map(({ address }) => ({ target: address }) as const),
abi: abi.want,
})

const pools: Contract[] = mapSuccessFilter(lpTokens, (res, index) => ({ ...rawPools[index], token: res.output }))
return getPairsDetails(ctx, pools, { getAddress: (pool) => pool.token! })
}
26 changes: 26 additions & 0 deletions src/adapters/matrix-farm/fantom/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { getMatrixFarmBalances } from '@adapters/matrix-farm/common/balance'
import { getMatrixVaults } from '@adapters/matrix-farm/common/vault'
import type { AdapterConfig, BaseContext, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'

export const getContracts = async (ctx: BaseContext) => {
const pools = await getMatrixVaults(ctx)

return {
contracts: { pools },
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
pools: getMatrixFarmBalances,
})

return {
groups: [{ balances }],
}
}

export const config: AdapterConfig = {
startDate: 1637020800,
}
16 changes: 16 additions & 0 deletions src/adapters/matrix-farm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Adapter } from '@lib/adapter'

import * as arbitrum from './arbitrum'
import * as base from './base'
import * as fantom from './fantom'
import * as optimism from './optimism'

const adapter: Adapter = {
id: 'matrix-farm',
arbitrum: arbitrum,
base: base,
fantom: fantom,
optimism: optimism,
}

export default adapter
26 changes: 26 additions & 0 deletions src/adapters/matrix-farm/optimism/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { getMatrixFarmBalances } from '@adapters/matrix-farm/common/balance'
import { getMatrixVaults } from '@adapters/matrix-farm/common/vault'
import type { AdapterConfig, BaseContext, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'

export const getContracts = async (ctx: BaseContext) => {
const pools = await getMatrixVaults(ctx)

return {
contracts: { pools },
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
pools: getMatrixFarmBalances,
})

return {
groups: [{ balances }],
}
}

export const config: AdapterConfig = {
startDate: 1658188800,
}
Loading