This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
/
curve.balance-fetcher.ts
88 lines (78 loc) · 3.83 KB
/
curve.balance-fetcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Inject } from '@nestjs/common';
import { APP_TOOLKIT, IAppToolkit } from '~app-toolkit/app-toolkit.interface';
import { Register } from '~app-toolkit/decorators';
import { presentBalanceFetcherResponse } from '~app-toolkit/helpers/presentation/balance-fetcher-response.present';
import { BalanceFetcher } from '~balance/balance-fetcher.interface';
import { isClaimable } from '~position/position.utils';
import { Network } from '~types/network.interface';
import { CurveChildLiquidityGauge, CurveContractFactory, CurveRewardsOnlyGauge } from '../contracts';
import { CURVE_DEFINITION } from '../curve.definition';
const network = Network.FANTOM_OPERA_MAINNET;
@Register.BalanceFetcher(CURVE_DEFINITION.id, Network.FANTOM_OPERA_MAINNET)
export class FantomCurveBalanceFetcher implements BalanceFetcher {
constructor(
@Inject(APP_TOOLKIT) private readonly appToolkit: IAppToolkit,
@Inject(CurveContractFactory) private readonly curveContractFactory: CurveContractFactory,
) {}
private async getPoolTokenBalances(address: string) {
return this.appToolkit.helpers.tokenBalanceHelper.getTokenBalances({
network,
appId: CURVE_DEFINITION.id,
groupId: CURVE_DEFINITION.groups.pool.id,
address,
});
}
private async getRewardsOnlyGaugeStakedBalances(address: string) {
return this.appToolkit.helpers.singleStakingContractPositionBalanceHelper.getBalances<CurveRewardsOnlyGauge>({
address,
appId: CURVE_DEFINITION.id,
groupId: CURVE_DEFINITION.groups.farm.id,
network,
farmFilter: v => v.dataProps.implementation === 'rewards-only-gauge',
resolveContract: ({ address, network }) => this.curveContractFactory.curveRewardsOnlyGauge({ address, network }),
resolveStakedTokenBalance: ({ contract, address, multicall }) => multicall.wrap(contract).balanceOf(address),
resolveRewardTokenBalances: ({ contract, address, multicall, contractPosition }) => {
const rewardTokens = contractPosition.tokens.filter(isClaimable);
const wrappedContract = multicall.wrap(contract);
return Promise.all(rewardTokens.map(v => wrappedContract.claimable_reward_write(address, v.address)));
},
});
}
private async getChildLiquidityGaugeStakedBalances(address: string) {
return this.appToolkit.helpers.singleStakingContractPositionBalanceHelper.getBalances<CurveChildLiquidityGauge>({
address,
appId: CURVE_DEFINITION.id,
groupId: CURVE_DEFINITION.groups.farm.id,
network,
farmFilter: v => v.dataProps.implementation === 'child-liquidity-gauge',
resolveContract: ({ address, network }) =>
this.curveContractFactory.curveChildLiquidityGauge({ address, network }),
resolveStakedTokenBalance: ({ contract, address, multicall }) => multicall.wrap(contract).balanceOf(address),
resolveRewardTokenBalances: async ({ contract, address, multicall, contractPosition }) => {
const rewardTokens = contractPosition.tokens.filter(isClaimable);
const otherRewardTokens = rewardTokens.filter(v => v.symbol !== 'CRV');
return Promise.all([
multicall.wrap(contract).callStatic.claimable_tokens(address),
...otherRewardTokens.map(v => multicall.wrap(contract).claimable_reward(address, v.address)),
]);
},
});
}
async getBalances(address: string) {
const [poolTokenBalances, rewardOnlyGaugeStakedBalances, childLiquidityGaugeStakedBalances] = await Promise.all([
this.getPoolTokenBalances(address),
this.getRewardsOnlyGaugeStakedBalances(address),
this.getChildLiquidityGaugeStakedBalances(address),
]);
return presentBalanceFetcherResponse([
{
label: 'Pools',
assets: poolTokenBalances,
},
{
label: 'Staked',
assets: [...rewardOnlyGaugeStakedBalances, ...childLiquidityGaugeStakedBalances],
},
]);
}
}