Skip to content

Commit

Permalink
Merge pull request #181 from dappradar/autointegrationv2
Browse files Browse the repository at this point in the history
Autointegrationv2
  • Loading branch information
mantasfam committed Jul 29, 2023
2 parents 7f0fa8f + 28edb32 commit 6a617c9
Show file tree
Hide file tree
Showing 5 changed files with 635 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/factory/factory.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,37 @@ export class FactoryService implements OnModuleInit {
if (this.web3ProviderService.checkNodeUrl(req?.chain)) {
throw new RpcException('Node URL is not provided');
}
const providerService: IProvider = await import(
this.getProviderServicePath(req.chain, req.provider, 'index')
);

const block = parseInt(req.block) - basicUtil.getDelay(req.chain);
const tvlData = await providerService.tvl({
web3: await this.web3ProviderService.getWeb3(req?.chain),
chain: req?.chain,
provider: req?.provider,
block,
date: req?.date,
});
const web3 = await this.web3ProviderService.getWeb3(req?.chain);
let tvlData;
console.log('req', req);

if (
req?.autointegrationParams?.autointegrated === 'false' ||
req?.autointegrationParams?.autointegrated === undefined
) {
const providerService: IProvider = await import(
this.getProviderServicePath(req.chain, req.provider, 'index')
);
tvlData = await providerService.tvl({
web3,
chain: req?.chain,
provider: req?.provider,
block,
date: req?.date,
});
} else {
tvlData = await autointegration.tvl({
web3,
chain: req?.chain,
provider: req?.provider,
block,
date: req?.date,
dappType: req?.autointegrationParams?.dappType,
addresses: req?.autointegrationParams?.addresses,
});
}

const balances = basicUtil.checkZeroBalance(tvlData.balances);
return { balances, poolBalances: tvlData.poolBalances };
Expand Down
60 changes: 60 additions & 0 deletions src/factory/providers/autointegration/index.ts
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 };
Loading

0 comments on commit 6a617c9

Please sign in to comment.