-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import util from '../blockchainUtil'; | ||
import formatter from '../formatter'; | ||
import POOL_DATA_PROVIDER_V3_ABI from '../../constants/abi/poolDataProviderV3Abi.json'; | ||
import AAVE_ERC20_ABI from '../../constants/abi/aaveErc20Abi.json'; | ||
import Web3 from 'web3'; | ||
import { IBalances } from '../../interfaces/ITvl'; | ||
|
||
async function getAddresses(poolDataProviderV3, block, chain, web3) { | ||
const aaveTokenMarketData = await util.executeCall( | ||
poolDataProviderV3, | ||
POOL_DATA_PROVIDER_V3_ABI, | ||
'getAllATokens', | ||
[], | ||
block, | ||
chain, | ||
web3, | ||
); | ||
|
||
const aaveTokenAddresses = []; | ||
aaveTokenMarketData.map((aaveTokensData) => { | ||
if (aaveTokensData) { | ||
aaveTokenAddresses.push( | ||
typeof aaveTokensData == 'string' | ||
? aaveTokensData.substring(aaveTokensData.indexOf(',') + 1) | ||
: aaveTokensData.tokenAddress, | ||
); | ||
} | ||
}); | ||
|
||
const underlyingAddressesData = await util.executeCallOfMultiTargets( | ||
aaveTokenAddresses, | ||
AAVE_ERC20_ABI, | ||
'UNDERLYING_ASSET_ADDRESS', | ||
[], | ||
block, | ||
chain, | ||
web3, | ||
); | ||
|
||
const reserveTokenAddresses = underlyingAddressesData.map( | ||
(reserveData) => reserveData, | ||
); | ||
|
||
return [aaveTokenAddresses, reserveTokenAddresses]; | ||
} | ||
|
||
async function getTvl( | ||
poolDataProvider: string, | ||
block: number, | ||
chain: string, | ||
web3: Web3, | ||
): Promise<IBalances> { | ||
const balances = {}; | ||
|
||
const [aaveTokens, reserveTokens] = await getAddresses( | ||
poolDataProvider, | ||
block, | ||
chain, | ||
web3, | ||
); | ||
|
||
const balanceResults = await util.getTokenBalancesOfHolders( | ||
aaveTokens, | ||
reserveTokens, | ||
block, | ||
chain, | ||
web3, | ||
); | ||
|
||
formatter.sumMultiBalanceOf(balances, balanceResults); | ||
return balances; | ||
} | ||
|
||
export default { getTvl }; |