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

Fix, Compound_v3 Interactions issues #1048

Merged
merged 1 commit into from
Nov 16, 2023
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
11 changes: 6 additions & 5 deletions src/adapters/compound-v3/arbitrum/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getAssetsContracts } from '@adapters/compound-v3/common/asset'
import { getCompLendBalances, getCompRewardBalances } from '@adapters/compound-v3/common/balance'
import { getCompLendBalances } from '@adapters/compound-v3/common/balance'
import type { BalancesContext, BaseContext, Contract, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'
import { getSingleStakeBalances } from '@lib/stake'
Expand All @@ -25,13 +25,15 @@ const USDC_e: Token = {
const cUSDCv3_n: Contract = {
chain: 'arbitrum',
address: '0x9c4ec768c28520B50860ea7a15bd7213a9fF58bf',
token: USDC.address,
underlyings: [USDC],
}

// cUSDCv3 through USDC.e Bridged version
const cUSDCv3_b: Contract = {
chain: 'arbitrum',
address: '0xA5EDBDD9646f8dFF606d7448e414884C7d905dCA',
token: USDC_e.address,
underlyings: [USDC_e],
}

Expand All @@ -41,21 +43,20 @@ const rewarder: Contract = {
}

export const getContracts = async (ctx: BaseContext) => {
const assets = await getAssetsContracts(ctx, [cUSDCv3_n, cUSDCv3_b])
const compounders = await getAssetsContracts(ctx, [cUSDCv3_n, cUSDCv3_b])

return {
contracts: { compounders: [cUSDCv3_n, cUSDCv3_b], assets, rewarder },
contracts: { compounders },
revalidate: 60 * 60,
}
}

const compoundBalances = async (ctx: BalancesContext, compounders: Contract[], rewarder: Contract) => {
return Promise.all([getSingleStakeBalances(ctx, compounders), getCompRewardBalances(ctx, rewarder, compounders)])
return Promise.all([getSingleStakeBalances(ctx, compounders), getCompLendBalances(ctx, compounders, rewarder)])
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
assets: (...args) => getCompLendBalances(...args, [cUSDCv3_n, cUSDCv3_b]),
compounders: (...args) => compoundBalances(...args, rewarder),
})

Expand Down
9 changes: 4 additions & 5 deletions src/adapters/compound-v3/base/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getAssetsContracts } from '@adapters/compound-v3/common/asset'
import { getCompLendBalances, getCompRewardBalances } from '@adapters/compound-v3/common/balance'
import { getCompLendBalances } from '@adapters/compound-v3/common/balance'
import type { BalancesContext, BaseContext, Contract, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'
import { getSingleStakeBalances } from '@lib/stake'
Expand Down Expand Up @@ -37,21 +37,20 @@ const rewarder: Contract = {
}

export const getContracts = async (ctx: BaseContext) => {
const assets = await getAssetsContracts(ctx, [cUSDbCv3, cWETHv3])
const compounders = await getAssetsContracts(ctx, [cUSDbCv3, cWETHv3])

return {
contracts: { compounders: [cUSDbCv3, cWETHv3], assets, rewarder },
contracts: { compounders },
revalidate: 60 * 60,
}
}

const compoundBalances = async (ctx: BalancesContext, compounders: Contract[], rewarder: Contract) => {
return Promise.all([getSingleStakeBalances(ctx, compounders), getCompRewardBalances(ctx, rewarder, compounders)])
return Promise.all([getSingleStakeBalances(ctx, compounders), getCompLendBalances(ctx, compounders, rewarder)])
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
assets: (...args) => getCompLendBalances(...args, [cUSDbCv3, cWETHv3]),
compounders: (...args) => compoundBalances(...args, rewarder),
})

Expand Down
31 changes: 29 additions & 2 deletions src/adapters/compound-v3/common/asset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { BaseContext, Contract } from '@lib/adapter'
import { mapSuccessFilter, range } from '@lib/array'
import { mapMultiSuccessFilter, mapSuccessFilter, range } from '@lib/array'
import { abi as erc20Abi } from '@lib/erc20'
import { multicall } from '@lib/multicall'

const abi = {
Expand Down Expand Up @@ -62,10 +63,36 @@ export async function getAssetsContracts(ctx: BaseContext, compounders: Contract
abi: abi.getAssetInfo,
})

return mapSuccessFilter(assetsInfoRes, (res) => ({
const rawAssets = mapSuccessFilter(assetsInfoRes, (res) => ({
chain: ctx.chain,
address: res.output.asset,
compounder: res.input.target,
collateralFactor: res.output.borrowCollateralFactor,
}))

const [symbolRes, decimalsRes] = await Promise.all([
multicall({ ctx, calls: rawAssets.map((asset) => ({ target: asset.address }) as const), abi: erc20Abi.symbol }),
multicall({ ctx, calls: rawAssets.map((asset) => ({ target: asset.address }) as const), abi: erc20Abi.decimals }),
])

const assets: Contract[] = mapMultiSuccessFilter(
symbolRes.map((_, i) => [symbolRes[i], decimalsRes[i]]),

(res, index) => {
const rawAsset = rawAssets[index]
const [{ output: symbol }, { output: decimals }] = res.inputOutputPairs

return {
...rawAsset,
decimals,
symbol,
}
},
)

compounders.forEach((compounder) => {
compounder.assets = assets.filter((asset) => asset.compounder === compounder.address)
})

return compounders
}
52 changes: 18 additions & 34 deletions src/adapters/compound-v3/common/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ const COMP: { [key: string]: `0x${string}` } = {

export async function getCompLendBalances(
ctx: BalancesContext,
assets: Contract[],
compounders: Contract[],
rewarder: Contract,
): Promise<Balance[]> {
const [userLendBalances, userBorrowBalances] = await Promise.all([
const assets = compounders.flatMap((compounder) => compounder.assets)

const [userLendBalances, userBorrowBalances, pendingCompRewards] = await Promise.all([
multicall({
ctx,
calls: assets.map((asset) => ({ target: asset.compounder, params: [ctx.address, asset.address] }) as const),
Expand All @@ -69,61 +71,43 @@ export async function getCompLendBalances(
calls: compounders.map((compounder) => ({ target: compounder.address, params: [ctx.address] }) as const),
abi: abi.borrowBalanceOf,
}),
multicall({
ctx,
calls: compounders.map(
(contract) => ({ target: rewarder.address, params: [contract.address, ctx.address] }) as const,
),
abi: abi.getRewardOwed,
}),
])

const supplyBalance: Balance[] = mapSuccessFilter(userLendBalances, (res, index) => {
const asset = assets[index] as Balance
const [balance, _reserved] = res.output

return {
chain: ctx.chain,
decimals: assets[index].decimals,
symbol: assets[index].symbol,
address: assets[index].address,
...asset,
amount: balance,
collateralFactor: assets[index].collateralFactor,
category: 'lend',
}
})

const borrowBalance: Balance[] = mapSuccessFilter(userBorrowBalances, (res, index) => {
const underlying = compounders[index].underlyings?.[0] as Contract

if (!underlying) {
return null
}

if (!underlying) return null
return {
chain: ctx.chain,
decimals: underlying.decimals,
symbol: underlying.symbol,
address: underlying.address,
...underlying,
amount: res.output,
category: 'borrow',
}
}).filter(isNotNullish) as Balance[]

return [...supplyBalance, ...borrowBalance]
}

export async function getCompRewardBalances(
ctx: BalancesContext,
rewarder: Contract,
compounders: Contract[],
): Promise<Balance[]> {
const pendingCompRewards = await multicall({
ctx,
calls: compounders.map(
(contract) => ({ target: rewarder.address, params: [contract.address, ctx.address] }) as const,
),
abi: abi.getRewardOwed,
})

return mapSuccessFilter(pendingCompRewards, (res) => ({
const rewardBalance: Balance[] = mapSuccessFilter(pendingCompRewards, (res) => ({
chain: ctx.chain,
address: COMP[ctx.chain],
decimals: 18,
symbol: 'COMP',
amount: res.output.owed,
category: 'reward',
}))

return [...supplyBalance, ...borrowBalance, ...rewardBalance]
}
9 changes: 4 additions & 5 deletions src/adapters/compound-v3/ethereum/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getAssetsContracts } from '@adapters/compound-v3/common/asset'
import { getCompLendBalances, getCompRewardBalances } from '@adapters/compound-v3/common/balance'
import { getCompLendBalances } from '@adapters/compound-v3/common/balance'
import type { BalancesContext, BaseContext, Contract, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'
import { getSingleStakeBalances } from '@lib/stake'
Expand Down Expand Up @@ -37,21 +37,20 @@ const rewarder: Contract = {
}

export const getContracts = async (ctx: BaseContext) => {
const assets = await getAssetsContracts(ctx, [cUSDCv3, cWETHv3])
const compounders = await getAssetsContracts(ctx, [cUSDCv3, cWETHv3])

return {
contracts: { compounders: [cUSDCv3, cWETHv3], assets, rewarder },
contracts: { compounders },
revalidate: 60 * 60,
}
}

const compoundBalances = async (ctx: BalancesContext, compounders: Contract[], rewarder: Contract) => {
return Promise.all([getSingleStakeBalances(ctx, compounders), getCompRewardBalances(ctx, rewarder, compounders)])
return Promise.all([getSingleStakeBalances(ctx, compounders), getCompLendBalances(ctx, compounders, rewarder)])
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
assets: (...args) => getCompLendBalances(...args, [cUSDCv3, cWETHv3]),
compounders: (...args) => compoundBalances(...args, rewarder),
})

Expand Down
9 changes: 4 additions & 5 deletions src/adapters/compound-v3/polygon/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getAssetsContracts } from '@adapters/compound-v3/common/asset'
import { getCompLendBalances, getCompRewardBalances } from '@adapters/compound-v3/common/balance'
import { getCompLendBalances } from '@adapters/compound-v3/common/balance'
import type { BalancesContext, BaseContext, Contract, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'
import { getSingleStakeBalances } from '@lib/stake'
Expand All @@ -24,21 +24,20 @@ const rewarder: Contract = {
}

export const getContracts = async (ctx: BaseContext) => {
const assets = await getAssetsContracts(ctx, [cUSDCv3])
const compounders = await getAssetsContracts(ctx, [cUSDCv3])

return {
contracts: { compounders: [cUSDCv3], assets, rewarder },
contracts: { compounders },
revalidate: 60 * 60,
}
}

const compoundBalances = async (ctx: BalancesContext, compounders: Contract[], rewarder: Contract) => {
return Promise.all([getSingleStakeBalances(ctx, compounders), getCompRewardBalances(ctx, rewarder, compounders)])
return Promise.all([getSingleStakeBalances(ctx, compounders), getCompLendBalances(ctx, compounders, rewarder)])
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
assets: (...args) => getCompLendBalances(...args, [cUSDCv3]),
compounders: (...args) => compoundBalances(...args, rewarder),
})

Expand Down
Loading