-
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.
Merge pull request #218 from dappradar/base_integartion
Base integration
- Loading branch information
Showing
12 changed files
with
2,888 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,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" | ||
} |
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,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 }; |
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,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 }; |
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,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 }; |
Oops, something went wrong.