This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Split Olympus bond balance helper for v1 and v2 bonds, keep bond…
… position fetcher generic, fix lint error (#242)
- Loading branch information
1 parent
cbde9d4
commit 0ea4021
Showing
7 changed files
with
107 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/apps/olympus/helpers/olympus.bond-v2.contract-position-balance-helper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { BigNumber } from 'ethers'; | ||
import { sumBy } from 'lodash'; | ||
|
||
import { drillBalance } from '~app-toolkit'; | ||
import { APP_TOOLKIT, IAppToolkit } from '~lib'; | ||
import { ContractPositionBalance } from '~position/position-balance.interface'; | ||
import { isClaimable, isVesting } from '~position/position.utils'; | ||
import { Network } from '~types/network.interface'; | ||
|
||
import { OlympusContractFactory } from '../contracts'; | ||
|
||
type OlympusBondV2ContractPositionBalanceHelperParams = { | ||
address: string; | ||
appId: string; | ||
groupId: string; | ||
network: Network; | ||
}; | ||
|
||
@Injectable() | ||
export class OlympusBondV2ContractPositionBalanceHelper { | ||
constructor( | ||
@Inject(APP_TOOLKIT) private readonly appToolkit: IAppToolkit, | ||
@Inject(OlympusContractFactory) private readonly contractFactory: OlympusContractFactory, | ||
) {} | ||
|
||
async getBalances(opts: OlympusBondV2ContractPositionBalanceHelperParams): Promise<ContractPositionBalance[]> { | ||
const { address, appId, groupId, network } = opts; | ||
const multicall = this.appToolkit.getMulticall(network); | ||
const contractPositions = await this.appToolkit.getAppContractPositions({ network, appId, groupIds: [groupId] }); | ||
const contractPositionBalances = await Promise.all( | ||
contractPositions.map(async contractPosition => { | ||
const contract = this.contractFactory.olympusV2BondDepository({ address: contractPosition.address, network }); | ||
const vestingToken = contractPosition.tokens.find(isVesting)!; | ||
const claimableToken = contractPosition.tokens.find(isClaimable)!; | ||
|
||
const indexes = await multicall.wrap(contract).indexesFor(address); | ||
const pendingBonds = await Promise.all( | ||
indexes.map(index => multicall.wrap(contract).pendingFor(address, index)), | ||
); | ||
|
||
const claimableBonds = pendingBonds.filter(p => p.matured_); | ||
const vestingBonds = pendingBonds.filter(p => !p.matured_); | ||
|
||
const claimableAmount = claimableBonds.reduce((acc, bond) => acc.add(bond.payout_), BigNumber.from('0')); | ||
const vestingAmount = vestingBonds.reduce((acc, bond) => { | ||
return acc.add(bond.payout_); | ||
}, BigNumber.from('0')); | ||
const claimableTokenBalance = drillBalance(claimableToken, claimableAmount.toString()); | ||
const vestingTokenBalance = drillBalance(vestingToken, vestingAmount.toString()); | ||
const tokens = [claimableTokenBalance, vestingTokenBalance].filter(v => v.balanceUSD > 0); | ||
const balanceUSD = sumBy(tokens, t => t.balanceUSD); | ||
|
||
return { ...contractPosition, tokens, balanceUSD }; | ||
}), | ||
); | ||
|
||
return contractPositionBalances; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters