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,Definitive #1377

Merged
merged 1 commit into from
Mar 27, 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
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
"cyclone",
"cygnus-finance",
"defi-swap",
"definitive.fi",
"definix",
"defiplaza",
"deltaprime",
Expand Down
27 changes: 27 additions & 0 deletions src/adapters/definitive.fi/arbitrum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getDefinitiveBalances } from '@adapters/definitive.fi/common/balance'
import { getDefinitiveContracts } from '@adapters/definitive.fi/common/pool'
import type { AdapterConfig, BaseContext, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'

const poolAddresses: `0x${string}`[] = ['0x449b72B665C28D6190ff08A21b2130CaCf06E1c8']

export const getContracts = async (ctx: BaseContext) => {
const pools = await getDefinitiveContracts(ctx, poolAddresses)
return {
contracts: { pools },
}
}

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

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

export const config: AdapterConfig = {
startDate: 1709769600,
}
30 changes: 30 additions & 0 deletions src/adapters/definitive.fi/avalanche/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getDefinitiveBalances } from '@adapters/definitive.fi/common/balance'
import { getDefinitiveContracts } from '@adapters/definitive.fi/common/pool'
import type { AdapterConfig, BaseContext, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'

const poolAddresses: `0x${string}`[] = [
'0xa460802fc6e7c1401B06078B332d5A1B52dff0D1',
'0x7402282F04740F2f8CE97eE426f90d6F800A3C21',
]

export const getContracts = async (ctx: BaseContext) => {
const pools = await getDefinitiveContracts(ctx, poolAddresses)
return {
contracts: { pools },
}
}

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

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

export const config: AdapterConfig = {
startDate: 1709769600,
}
31 changes: 31 additions & 0 deletions src/adapters/definitive.fi/base/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getDefinitiveBalances } from '@adapters/definitive.fi/common/balance'
import { getDefinitiveContracts } from '@adapters/definitive.fi/common/pool'
import type { AdapterConfig, BaseContext, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'

const poolAddresses: `0x${string}`[] = [
'0xC2cA42Ac871753d4623766581b7A963c2AD7209B',
'0x035569b57390a095b4b3f7754214b39CA3145C75',
'0xB3E741Ee16Df64eF9274261A397Df6Fd54073FFB',
]

export const getContracts = async (ctx: BaseContext) => {
const pools = await getDefinitiveContracts(ctx, poolAddresses)
return {
contracts: { pools },
}
}

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

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

export const config: AdapterConfig = {
startDate: 1709769600,
}
61 changes: 61 additions & 0 deletions src/adapters/definitive.fi/common/balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { Balance, BalancesContext, Contract } from '@lib/adapter'
import { mapMultiSuccessFilter } from '@lib/array'
import { multicall } from '@lib/multicall'
import { isNotNullish } from '@lib/type'

const abi = {
balanceOf: {
inputs: [{ internalType: 'address', name: 'account', type: 'address' }],
name: 'balanceOf',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
totalAssets: {
inputs: [],
name: 'totalAssets',
outputs: [{ internalType: 'uint256', name: 'totalAssetsAmount', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
totalSupply: {
inputs: [],
name: 'totalSupply',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
} as const

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

return mapMultiSuccessFilter(
userShares.map((_, i) => [userShares[i], totalSupplies[i], totalAssets[i]]),

(res, index) => {
const pool = pools[index]
const rawUnderlying = pool.underlyings![0] as Contract
if (!rawUnderlying) return null

const [{ output: share }, { output: totalShare }, { output: totalAsset }] = res.inputOutputPairs
const underlyings = [{ ...rawUnderlying, amount: (share * totalAsset) / totalShare }]

return {
...pool,
amount: share,
underlyings,
rewards: undefined,
category: 'farm',
}
},
).filter(isNotNullish)
}
23 changes: 23 additions & 0 deletions src/adapters/definitive.fi/common/pool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { BaseContext, Contract } from '@lib/adapter'
import { mapSuccessFilter } from '@lib/array'
import { multicall } from '@lib/multicall'

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

export async function getDefinitiveContracts(ctx: BaseContext, poolAddresses: `0x${string}`[]): Promise<Contract[]> {
const assets = await multicall({
ctx,
calls: poolAddresses.map((address) => ({ target: address }) as const),
abi: abi.asset,
})

return mapSuccessFilter(assets, (res) => ({ chain: ctx.chain, address: res.input.target, underlyings: [res.output] }))
}
30 changes: 30 additions & 0 deletions src/adapters/definitive.fi/ethereum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getDefinitiveBalances } from '@adapters/definitive.fi/common/balance'
import { getDefinitiveContracts } from '@adapters/definitive.fi/common/pool'
import type { AdapterConfig, BaseContext, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'

const poolAddresses: `0x${string}`[] = [
'0x954F286AEc288af89601F53e5D8727540ba2f00f',
'0x4184a083307a208f5bF20d0B44E161Bc55aae996',
]

export const getContracts = async (ctx: BaseContext) => {
const pools = await getDefinitiveContracts(ctx, poolAddresses)
return {
contracts: { pools },
}
}

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

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

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

import * as optimism from './optimism'
import * as base from './base'
import * as arbitrum from './arbitrum'
import * as polygon from './polygon'
import * as ethereum from './ethereum'
import * as avalanche from './avalanche'

const adapter: Adapter = {
id: 'definitive.fi',
optimism: optimism,
base: base,
arbitrum: arbitrum,
polygon: polygon,
ethereum: ethereum,
avalanche: avalanche,
}

export default adapter
30 changes: 30 additions & 0 deletions src/adapters/definitive.fi/optimism/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getDefinitiveBalances } from '@adapters/definitive.fi/common/balance'
import { getDefinitiveContracts } from '@adapters/definitive.fi/common/pool'
import type { AdapterConfig, BaseContext, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'

const poolAddresses: `0x${string}`[] = [
'0x3796103d23D207fB5db2CFEc97fd7a0ac0A70D82',
'0xB2a74028CcCA97C4fA4686802246FdDEAa3A941B',
]

export const getContracts = async (ctx: BaseContext) => {
const pools = await getDefinitiveContracts(ctx, poolAddresses)
return {
contracts: { pools },
}
}

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

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

export const config: AdapterConfig = {
startDate: 1709769600,
}
27 changes: 27 additions & 0 deletions src/adapters/definitive.fi/polygon/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getDefinitiveBalances } from '@adapters/definitive.fi/common/balance'
import { getDefinitiveContracts } from '@adapters/definitive.fi/common/pool'
import type { AdapterConfig, BaseContext, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'

const poolAddresses: `0x${string}`[] = ['0x8347B60460421EE565F3aC26DaFbAC9D2fE8930e']

export const getContracts = async (ctx: BaseContext) => {
const pools = await getDefinitiveContracts(ctx, poolAddresses)
return {
contracts: { pools },
}
}

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

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

export const config: AdapterConfig = {
startDate: 1709769600,
}
2 changes: 2 additions & 0 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import cvaultFinance from '@adapters/cvault-finance'
import cyclone from '@adapters/cyclone'
import cygnusFinance from '@adapters/cygnus-finance'
import defiSwap from '@adapters/defi-swap'
import definitiveFi from '@adapters/definitive.fi'
import definix from '@adapters/definix'
import defiplaza from '@adapters/defiplaza'
import deltaprime from '@adapters/deltaprime'
Expand Down Expand Up @@ -504,6 +505,7 @@ export const adapters: Adapter[] = [
cyclone,
cygnusFinance,
defiSwap,
definitiveFi,
definix,
defiplaza,
deltaprime,
Expand Down
Loading