-
Notifications
You must be signed in to change notification settings - Fork 23
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 #181 from dappradar/autointegrationv2
Autointegrationv2
- Loading branch information
Showing
5 changed files
with
635 additions
and
10 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
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,60 @@ | ||
import { AbiItem } from 'web3-utils'; | ||
import uniswapV2 from './uniswapV2'; | ||
import formatter from '../../../util/formatter'; | ||
import BigNumber from 'bignumber.js'; | ||
import { | ||
ITvlAutointegrationParams, | ||
ITvlReturn, | ||
} from '../../../interfaces/ITvl'; | ||
import FACTORY_ABI from '../../../constants/abi/factory.json'; | ||
|
||
const UNISWAP_V2 = 'uniswapV2'; | ||
const SUPPORTED_DAPP_TYPES = [UNISWAP_V2]; | ||
|
||
async function tvl( | ||
params: ITvlAutointegrationParams, | ||
): Promise<Partial<ITvlReturn>> { | ||
const { block, chain, provider, web3, dappType, addresses } = params; | ||
|
||
if (!SUPPORTED_DAPP_TYPES.includes(dappType)) { | ||
console.log(`Dapp type ${dappType} is not supported`); | ||
return { balances: {} }; | ||
} | ||
console.log(`Dapp identified as ${dappType} type`); | ||
|
||
if (dappType === UNISWAP_V2) { | ||
const result = { balances: {}, poolBalances: {} }; | ||
|
||
for (const address of addresses.split(',')) { | ||
const contract = new web3.eth.Contract(FACTORY_ABI as AbiItem[], address); | ||
|
||
let usePoolMethodsFlg = false; | ||
try { | ||
await contract.methods.allPairsLength().call(null, block); | ||
} catch { | ||
usePoolMethodsFlg = true; | ||
} | ||
|
||
const { balances, poolBalances } = await uniswapV2.getTvl( | ||
address, | ||
block, | ||
chain, | ||
provider, | ||
web3, | ||
usePoolMethodsFlg, | ||
); | ||
|
||
for (const token in balances) { | ||
result.balances[token] = BigNumber(result.balances[token] || 0).plus( | ||
balances[token], | ||
); | ||
} | ||
Object.assign(result.poolBalances, poolBalances); | ||
} | ||
|
||
formatter.convertBalancesToFixed(result.balances); | ||
return result; | ||
} | ||
} | ||
|
||
export { tvl }; |
Oops, something went wrong.