Skip to content

Commit

Permalink
Merge pull request DefiLlama#233 from DefiLlama/fix_aave3
Browse files Browse the repository at this point in the history
Fix aave-v3
  • Loading branch information
vrtnd authored Aug 18, 2022
2 parents 1ce2111 + a0bc54c commit dea33cc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/adaptors/aave-v3/abi.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 37 additions & 5 deletions src/adaptors/aave-v3/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const superagent = require('superagent');
const { request, gql } = require('graphql-request');
const sdk = require('@defillama/sdk');

const utils = require('../utils');
const { aTokenAbi } = require('./abi');

const SECONDS_PER_YEAR = 31536000;

Expand Down Expand Up @@ -46,6 +48,7 @@ const query = gql`
id
name
aToken {
id
rewards {
id
emissionsPerSecond
Expand Down Expand Up @@ -73,6 +76,34 @@ const apy = async () => {
(await request(url, query)).reserves,
])
);
const totalSupply = await Promise.all(
data.map(async ([chain, reserves]) =>
(
await sdk.api.abi.multiCall({
chain: chain === 'avalanche' ? 'avax' : chain,
abi: aTokenAbi.find(({ name }) => name === 'totalSupply'),
calls: reserves.map((reserve) => ({
target: reserve.aToken.id,
})),
})
).output.map(({ output }) => output)
)
);

const underlyingBalances = await Promise.all(
data.map(async ([chain, reserves]) =>
(
await sdk.api.abi.multiCall({
chain: chain === 'avalanche' ? 'avax' : chain,
abi: aTokenAbi.find(({ name }) => name === 'balanceOf'),
calls: reserves.map((reserve, i) => ({
target: reserve.aToken.underlyingAssetAddress,
params: [reserve.aToken.id],
})),
})
).output.map(({ output }) => output)
)
);

const underlyingTokens = data.map(([chain, reserves]) =>
reserves.map(
Expand All @@ -95,15 +126,16 @@ const apy = async () => {
underlyingTokens.flat().concat(rewardTokens.flat(Infinity))
);

const pools = data.map(([chain, markets]) => {
const chainPools = markets.map((pool) => {
const pools = data.map(([chain, markets], i) => {
const chainPools = markets.map((pool, idx) => {
const supply = totalSupply[i][idx];
const currentSupply = underlyingBalances[i][idx];
const totalSupplyUsd =
(pool.totalLiquidity / 10 ** pool.aToken.underlyingAssetDecimals) *
(supply / 10 ** pool.aToken.underlyingAssetDecimals) *
(pricesByAddress[pool.aToken.underlyingAssetAddress] ||
pricesBySymbol[pool.symbol]);
const tvlUsd =
((pool.totalLiquidity - pool.totalCurrentVariableDebt) /
10 ** pool.aToken.underlyingAssetDecimals) *
(currentSupply / 10 ** pool.aToken.underlyingAssetDecimals) *
(pricesByAddress[pool.aToken.underlyingAssetAddress] ||
pricesBySymbol[pool.symbol]);
const { rewards } = pool.aToken;
Expand Down

0 comments on commit dea33cc

Please sign in to comment.