Skip to content

Commit

Permalink
Fix:Ipor,PricePerFullShare (#1166)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpeluche authored Jan 11, 2024
1 parent e92770c commit d890dca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
50 changes: 39 additions & 11 deletions src/adapters/ipor/ethereum/farm.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Balance, BalancesContext, Contract } from '@lib/adapter'
import { mapMultiSuccessFilter } from '@lib/array'
import type { Category } from '@lib/category'
import { abi as erc20Abi } from '@lib/erc20'
import { multicall } from '@lib/multicall'
import type { Token } from '@lib/token'
import { isNotNullish } from '@lib/type'
import { parseEther } from 'viem'

const abi = {
balanceOf: {
Expand Down Expand Up @@ -45,6 +45,25 @@ const abi = {
stateMutability: 'view',
type: 'function',
},
getBalance: {
inputs: [],
name: 'getBalance',
outputs: [
{
components: [
{ internalType: 'uint256', name: 'totalCollateralPayFixed', type: 'uint256' },
{ internalType: 'uint256', name: 'totalCollateralReceiveFixed', type: 'uint256' },
{ internalType: 'uint256', name: 'liquidityPool', type: 'uint256' },
{ internalType: 'uint256', name: 'vault', type: 'uint256' },
],
internalType: 'struct IporTypes.AmmBalancesMemory',
name: '',
type: 'tuple',
},
],
stateMutability: 'view',
type: 'function',
},
} as const

const IPOR: Token = {
Expand All @@ -59,7 +78,7 @@ export async function getIporFarmBalances(
assets: Contract[],
farmer: Contract,
): Promise<Balance[]> {
const [userBalancesRes, userPendingRewardsRes, exchangeRatesRes] = await Promise.all([
const [userBalancesRes, userPendingRewardsRes, ipTokenSupplies, storageSupplies] = await Promise.all([
multicall({
ctx,
calls: assets.map((asset) => ({ target: farmer.address, params: [ctx.address, asset.token!] }) as const),
Expand All @@ -72,30 +91,39 @@ export async function getIporFarmBalances(
}),
multicall({
ctx,
calls: assets.map((asset) => ({ target: asset.address }) as const),
abi: abi.calculateExchangeRate,
calls: assets.map((asset) => ({ target: asset.token! }) as const),
abi: erc20Abi.totalSupply,
}),
multicall({
ctx,
calls: assets.map((asset) => ({ target: asset.storage }) as const),
abi: abi.getBalance,
}),
])

return mapMultiSuccessFilter(
userBalancesRes.map((_, i) => [userBalancesRes[i], userPendingRewardsRes[i]]),
userBalancesRes.map((_, i) => [
userBalancesRes[i],
userPendingRewardsRes[i],
ipTokenSupplies[i],
storageSupplies[i],
]),

(res, index) => {
const asset = assets[index]
const pricePerFullShare = exchangeRatesRes[index].success
? exchangeRatesRes[index].output! / parseEther('1.0')
: 1n
const underlying = asset.underlyings![0] as Contract
if (!underlying || !pricePerFullShare) return null

const [{ output: amount }, { output: rewards }] = res.inputOutputPairs
const [{ output: amount }, { output: rewards }, { output: ipTokenSupply }, { output: storageSupplyRes }] =
res.inputOutputPairs
const [{ rewardsAmount }] = rewards
const storageSupply = storageSupplyRes.liquidityPool
const underlyingAmount = (amount * storageSupply) / ipTokenSupply

return {
...asset,
decimals: 18,
amount,
underlyings: [{ ...underlying, amount: amount * pricePerFullShare }],
underlyings: [{ ...underlying, amount: underlyingAmount }],
rewards: [{ ...IPOR, amount: rewardsAmount }],
category: 'farm' as Category,
}
Expand Down
3 changes: 3 additions & 0 deletions src/adapters/ipor/ethereum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ const assets: Contract[] = [
address: '0x086d4daab14741b195deE65aFF050ba184B65045',
token: '0x8537b194BFf354c4738E9F3C81d67E3371DaDAf8',
underlyings: ['0x6B175474E89094C44Da98b954EedeAC495271d0F'],
storage: '0xb99f2a02c0851efdD417bd6935d2eFcd23c56e61',
},
{
chain: 'ethereum',
address: '0xC52569b5A349A7055E9192dBdd271F1Bd8133277',
token: '0x7c0e72f431FD69560D951e4C04A4de3657621a88',
underlyings: ['0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'],
storage: '0xB3d1c1aB4D30800162da40eb18B3024154924ba5',
},
{
chain: 'ethereum',
address: '0x33C5A44fd6E76Fc2b50a9187CfeaC336A74324AC',
token: '0x9Bd2177027edEE300DC9F1fb88F24DB6e5e1edC6',
underlyings: ['0xdAC17F958D2ee523a2206206994597C13D831ec7'],
storage: '0x364f116352EB95033D73822bA81257B8c1f5B1CE',
},
]

Expand Down

0 comments on commit d890dca

Please sign in to comment.