Skip to content

Commit

Permalink
Create aaveV3.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
mantasfam committed Jul 21, 2023
1 parent 2fc2be2 commit e220ed1
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/util/calculators/aaveV3.ts
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 };

0 comments on commit e220ed1

Please sign in to comment.