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,Davos #1392

Merged
merged 1 commit into from
Apr 3, 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 @@ -184,6 +184,7 @@
"cvault-finance",
"cyclone",
"cygnus-finance",
"davos-protocol",
"defi-swap",
"definitive.fi",
"definix",
Expand Down
30 changes: 30 additions & 0 deletions src/adapters/davos-protocol/arbitrum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getDavosLendingBalances } from '@adapters/davos-protocol/common/balance'
import { getDavosVaults } from '@adapters/davos-protocol/common/vault'
import type { AdapterConfig, BaseContext, GetBalancesHandler } from '@lib/adapter'

const vaultAddresses: `0x${string}`[] = [
'0x7ef3991f54d2cbefe247c2ff7c35a8a9609dcefa',
'0x601ab2230c2f7b8e719a0111febdfa94bb462c69',
'0x5a691001bf7065a17e150681f5bfbd7bc45a668e',
'0x9c44e6a927302da33dd76abe4558f26e31c48019',
'0x02c7420407a6439d49e9816399a5d5b03187363b',
]

export const getContracts = async (ctx: BaseContext) => {
const vaults = await getDavosVaults(ctx, vaultAddresses)
return {
contracts: { vaults },
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const vaultBalances = await getDavosLendingBalances(ctx, contracts.vaults || [])

return {
groups: [...vaultBalances],
}
}

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

const abi = {
locked: {
inputs: [
{ internalType: 'address', name: 'token', type: 'address' },
{ internalType: 'address', name: 'usr', type: 'address' },
],
name: 'locked',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
borrowed: {
inputs: [
{ internalType: 'address', name: 'token', type: 'address' },
{ internalType: 'address', name: 'usr', type: 'address' },
],
name: 'borrowed',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
collateralRate: {
inputs: [{ internalType: 'address', name: 'token', type: 'address' }],
name: 'collateralRate',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
} as const

const DUSD: { [key: string]: Contract } = {
ethereum: { chain: 'ethereum', address: '0xa48f322f8b3edff967629af79e027628b9dd1298', decimals: 18, symbol: 'DUSD' },
arbitrum: { chain: 'arbitrum', address: '0x8ec1877698acf262fe8ad8a295ad94d6ea258988', decimals: 18, symbol: 'DUSD' },
optimism: { chain: 'optimism', address: '0xb396b31599333739a97951b74652c117be86ee1d', decimals: 18, symbol: 'DUSD' },
polygon: { chain: 'polygon', address: '0xec38621e72d86775a89c7422746de1f52bba5320', decimals: 18, symbol: 'DUSD' },
}

export async function getDavosLendingBalances(ctx: BalancesContext, vaults: Contract[]): Promise<any[]> {
const [collaterals, borrows, collRates] = await Promise.all([
multicall({
ctx,
calls: vaults.map((vault) => ({ target: vault.address, params: [vault.token!, ctx.address] }) as const),
abi: abi.locked,
}),
multicall({
ctx,
calls: vaults.map((vault) => ({ target: vault.address, params: [vault.token!, ctx.address] }) as const),
abi: abi.borrowed,
}),
multicall({
ctx,
calls: vaults.map((vault) => ({ target: vault.address, params: [vault.token!] }) as const),
abi: abi.collateralRate,
}),
])

return mapMultiSuccessFilter(
collaterals.map((_, i) => [collaterals[i], borrows[i], collRates[i]]),

(res, index) => {
const vault = vaults[index]
const [{ output: coll }, { output: borrow }, { output: collRate }] = res.inputOutputPairs

const MCR = 1 / parseFloatBI(collRate, 18)

const lendBalance: Balance = {
...vault,
amount: coll,
underlyings: vault.underlyings as Contract[],
rewards: undefined,
MCR,
category: 'lend',
}

const borrowBalance: Balance = {
...DUSD[ctx.chain],
amount: borrow,
underlyings: undefined,
rewards: undefined,
category: 'borrow',
}

return { balances: [lendBalance, borrowBalance] }
},
)
}
52 changes: 52 additions & 0 deletions src/adapters/davos-protocol/common/vault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { BaseContext, Contract } from '@lib/adapter'
import { mapMultiSuccessFilter } from '@lib/array'
import { multicall } from '@lib/multicall'

const abi = {
interaction: {
inputs: [],
name: 'interaction',
outputs: [{ internalType: 'contract IInteraction', name: '', type: 'address' }],
stateMutability: 'view',
type: 'function',
},
collateral: {
inputs: [],
name: 'collateral',
outputs: [{ internalType: 'contract IERC20Upgradeable', name: '', type: 'address' }],
stateMutability: 'view',
type: 'function',
},
underlying: {
inputs: [],
name: 'underlying',
outputs: [{ internalType: 'contract IWrapped', name: '', type: 'address' }],
stateMutability: 'view',
type: 'function',
},
} as const

export async function getDavosVaults(ctx: BaseContext, vaultAddresses: `0x${string}`[]): Promise<Contract[]> {
const [vaultInteractions, vaultTokens, vaultUnderlyings] = await Promise.all([
multicall({ ctx, calls: vaultAddresses.map((address) => ({ target: address }) as const), abi: abi.interaction }),
multicall({ ctx, calls: vaultAddresses.map((address) => ({ target: address }) as const), abi: abi.collateral }),
multicall({ ctx, calls: vaultAddresses.map((address) => ({ target: address }) as const), abi: abi.underlying }),
])

return mapMultiSuccessFilter(
vaultTokens.map((_, i) => [vaultInteractions[i], vaultTokens[i], vaultUnderlyings[i]]),

(res, index) => {
const router = vaultAddresses[index]
const [{ output: address }, { output: token }, { output: underlying }] = res.inputOutputPairs

return {
chain: ctx.chain,
address,
token,
router,
underlyings: [underlying],
}
},
)
}
30 changes: 30 additions & 0 deletions src/adapters/davos-protocol/ethereum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getDavosLendingBalances } from '@adapters/davos-protocol/common/balance'
import { getDavosVaults } from '@adapters/davos-protocol/common/vault'
import type { AdapterConfig, BaseContext, GetBalancesHandler } from '@lib/adapter'

const vaultAddresses: `0x${string}`[] = [
'0xd25b3dbb79888f548ccfb3ffcf530fb0cb69bc4f',
'0x573674618c934c14f5c7cf85b5e586bae9991b63',
'0xeed1122d9e564b9cf281ae0ce9c5590d94db52d9',
'0x3693980607bc6d1b7384e3f484685c02be3ed0b7',
'0x8855d3fbcda1dfccf44ac6079d093bcf3a833f2d',
]

export const getContracts = async (ctx: BaseContext) => {
const vaults = await getDavosVaults(ctx, vaultAddresses)
return {
contracts: { vaults },
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const vaultBalances = await getDavosLendingBalances(ctx, contracts.vaults || [])

return {
groups: [...vaultBalances],
}
}

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

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

const adapter: Adapter = {
id: 'davos-protocol',
ethereum: ethereum,
polygon: polygon,
arbitrum: arbitrum,
optimism: optimism,
}

export default adapter
27 changes: 27 additions & 0 deletions src/adapters/davos-protocol/optimism/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getDavosLendingBalances } from '@adapters/davos-protocol/common/balance'
import { getDavosVaults } from '@adapters/davos-protocol/common/vault'
import type { AdapterConfig, BaseContext, GetBalancesHandler } from '@lib/adapter'

const vaultAddresses: `0x${string}`[] = [
'0x7a447d3fa34715010273a17c3fd29eb3926d7f2c',
'0x86e956154df2cd9495b6d92d9b0c2b00f8e390b1',
]

export const getContracts = async (ctx: BaseContext) => {
const vaults = await getDavosVaults(ctx, vaultAddresses)
return {
contracts: { vaults },
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const vaultBalances = await getDavosLendingBalances(ctx, contracts.vaults || [])

return {
groups: [...vaultBalances],
}
}

export const config: AdapterConfig = {
startDate: 1702339200,
}
27 changes: 27 additions & 0 deletions src/adapters/davos-protocol/polygon/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getDavosLendingBalances } from '@adapters/davos-protocol/common/balance'
import { getDavosVaults } from '@adapters/davos-protocol/common/vault'
import type { AdapterConfig, BaseContext, GetBalancesHandler } from '@lib/adapter'

const vaultAddresses: `0x${string}`[] = [
'0xe2023c00f78a384dd96333590aea1e3a0a91fd6a',
'0x046b71694b3b659f491247167eda42e0556123cf',
]

export const getContracts = async (ctx: BaseContext) => {
const vaults = await getDavosVaults(ctx, vaultAddresses)
return {
contracts: { vaults },
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const vaultBalances = await getDavosLendingBalances(ctx, contracts.vaults || [])

return {
groups: [...vaultBalances],
}
}

export const config: AdapterConfig = {
startDate: 1676332800,
}
2 changes: 2 additions & 0 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import curveDex from '@adapters/curve-dex'
import cvaultFinance from '@adapters/cvault-finance'
import cyclone from '@adapters/cyclone'
import cygnusFinance from '@adapters/cygnus-finance'
import davosProtocol from '@adapters/davos-protocol'
import defiSwap from '@adapters/defi-swap'
import definitiveFi from '@adapters/definitive.fi'
import definix from '@adapters/definix'
Expand Down Expand Up @@ -518,6 +519,7 @@ export const adapters: Adapter[] = [
cvaultFinance,
cyclone,
cygnusFinance,
davosProtocol,
defiSwap,
definitiveFi,
definix,
Expand Down
Loading