Skip to content

Commit

Permalink
Merge pull request #218 from dappradar/base_integartion
Browse files Browse the repository at this point in the history
Base integration
  • Loading branch information
mantasfam authored Oct 16, 2023
2 parents d5fafe2 + af12356 commit 67d6241
Show file tree
Hide file tree
Showing 12 changed files with 2,888 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/factory/providers/base/balancer/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"address": "0x4158734d47fc9692176b5085e0f52ee0da5d47f1",
"name": "Balancer",
"symbol": "BAL",
"decimals": 18,
"logo": "https://user-dashboard.s3.us-east-2.amazonaws.com/token_images/prod/61/bal_small.png"
}
81 changes: 81 additions & 0 deletions src/factory/providers/base/balancer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import BigNumber from 'bignumber.js';
import { request, gql } from 'graphql-request';
import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl';
import { log } from '../../../../util/logger/logger';
import util from '../../../../util/blockchainUtil';
import formatter from '../../../../util/formatter';

const START_BLOCK = 2555244;

const THEGRAPTH_ENDPOINT =
'https://api.studio.thegraph.com/query/24660/balancer-base-v2/version/latest';
const POOL_COUNT_QUERY = gql`
query getPoolCount($block: Int!) {
balancer(id: 2, block: { number: $block }) {
poolCount
}
}
`;
const POOLS_QUERY = gql`
query getPools($block: Int!, $skip: Int!) {
pools(skip: $skip, first: 1000, block: { number: $block }) {
tokens {
address
balance
decimals
}
}
}
`;

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;
if (block < START_BLOCK) {
return {};
}

const tokenBalances = {};

try {
let poolCount;
await request(THEGRAPTH_ENDPOINT, POOL_COUNT_QUERY, {
block: block,
}).then((data) => (poolCount = data.balancer.poolCount));

for (let i = 0; i < poolCount; i += 1000) {
let pools;
await request(THEGRAPTH_ENDPOINT, POOLS_QUERY, {
block: block,
skip: i,
}).then((data) => (pools = data.pools));

pools.forEach((pool) => {
pool.tokens.forEach((token) => {
tokenBalances[token.address] = BigNumber(
tokenBalances[token.address] || 0,
).plus(BigNumber(token.balance).shiftedBy(token.decimals));
});
});
}
} catch (e) {
log.error({
message: e?.message || '',
stack: e?.stack || '',
detail: `Error: tvl of polygon/balancer`,
endpoint: 'tvl',
});
throw e;
}

const balances = await util.convertToUnderlyings(
tokenBalances,
block,
chain,
provider,
web3,
);
formatter.convertBalancesToFixed(balances);
return { balances };
}

export { tvl };
18 changes: 18 additions & 0 deletions src/factory/providers/base/beefyfinance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import beefyfinance from '../../../../util/calculators/beefyfinance';
import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl';

const START_BLOCK = 2320000;

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;

if (block < START_BLOCK) {
return { balances: {} };
}

const balances = await beefyfinance.getTvl(block, chain, provider, web3);

return { balances };
}

export { tvl };
54 changes: 54 additions & 0 deletions src/factory/providers/base/compound/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import BigNumber from 'bignumber.js';
import formatter from '../../../../util/formatter';
import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl';
import { request, gql } from 'graphql-request';

const START_BLOCK = 2650000;
const COMPOUND_V3_GRAPHQL_API =
'https://api.thegraph.com/subgraphs/name/messari/compound-v3-base';
const V3_MARKETS = gql`
query getMarkets($block: Int) {
markets(
block: { number: $block }
orderBy: totalValueLockedUSD
orderDirection: desc
) {
inputToken {
id
}
inputTokenBalance
variableBorrowedTokenBalance
stableBorrowedTokenBalance
}
}
`;

async function addV3MarketBalances(block, balances) {
const requestResult = await request(COMPOUND_V3_GRAPHQL_API, V3_MARKETS, {
block: block,
});
requestResult.markets.forEach((market) => {
balances[market.inputToken.id] = BigNumber(
balances[market.inputToken.id] || 0,
)
.plus(market.inputTokenBalance)
.minus(market.variableBorrowedTokenBalance || 0)
.minus(market.stableBorrowedTokenBalance || 0);
});
}

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block } = params;
if (block < START_BLOCK) {
return { balances: {} };
}

const balances = {};

await addV3MarketBalances(block, balances);

formatter.convertBalancesToFixed(balances);
return { balances };
}

export { tvl };
Loading

0 comments on commit 67d6241

Please sign in to comment.