diff --git a/src/factory/provider.spec.ts b/src/factory/provider.spec.ts index 3a3fb79d..98e2685a 100644 --- a/src/factory/provider.spec.ts +++ b/src/factory/provider.spec.ts @@ -30,6 +30,11 @@ describe(process.argv[6], () => { chain, block, date: '0', + autointegrationParams: { + autointegrated: 'false', + dappType: undefined, + addresses: undefined, + }, }); console.log('balances:', balances); if (showPoolBalancesFlg) console.log('poolBalances:', poolBalances); diff --git a/src/factory/providers/ethereum/maker-dao/index.ts b/src/factory/providers/ethereum/maker-dao/index.ts new file mode 100644 index 00000000..66d9c7da --- /dev/null +++ b/src/factory/providers/ethereum/maker-dao/index.ts @@ -0,0 +1,145 @@ +import BigNumber from 'bignumber.js'; +import ERC20_ABI from '../../../../constants/abi/erc20.json'; +import MakerSCDConstants from './makerdao.json'; +import MakerMCDConstants from './maker-mcd.json'; +import formatter from '../../../../util/formatter'; +import util from '../../../../util/blockchainUtil'; +import basicUtil from '../../../../util/basicUtil'; +import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl'; + +async function getJoins(block, chain, provider, web3) { + const relyTopic = + '0x65fae35e00000000000000000000000000000000000000000000000000000000'; + + // get list of auths + let offset = 10000; + let stored_log; + try { + stored_log = await basicUtil.readFromCache( + 'makerdao_log.json', + chain, + provider, + ); + } catch {} + + let i = Math.max( + MakerMCDConstants.STARTBLOCK, + Number(stored_log?.block) || 0, + ); + const logs = stored_log?.data || []; + + for (;;) { + if (i > block) { + break; + } + try { + const seperatedLogs = await web3.eth.getPastLogs({ + fromBlock: i, + toBlock: Math.min(i + offset, block), + topics: [relyTopic], + address: MakerMCDConstants.VAT, + }); + const tokens = seperatedLogs.map( + (log) => `0x${log.topics[1].substr(26)}`, + ); + tokens.forEach((token) => { + if (!logs.includes(token)) { + logs.push(token); + } + }); + i += offset; + if (offset < 20) { + offset += 2; + } else if (offset < 200) { + offset += 20; + } else if (offset < 2000) { + offset += 200; + } else { + offset += 2000; + } + } catch (e) { + if (offset > 2000) { + offset -= 2000; + } else if (offset > 200) { + offset -= 200; + } else if (offset > 20) { + offset -= 20; + } else if (offset > 2) { + offset -= 2; + } else { + break; + } + } + } + + const log_data = { + block: undefined, + data: undefined, + }; + log_data.block = Math.max(Number(stored_log.block) || 0, block); + log_data.data = []; + + const ilkResults = await util.executeCallOfMultiTargets( + logs, + MakerMCDConstants.abi, + 'ilk', + [], + block, + chain, + web3, + ); + ilkResults.forEach((result, index) => { + if (result) { + log_data.data.push(logs[index]); + } + }); + + await basicUtil.saveIntoCache(log_data, 'makerdao_log.json', chain, provider); + return log_data.data; +} + +async function tvl(params: ITvlParams): Promise> { + const { block, chain, provider, web3 } = params; + const balances = {}; + + const scdContract = new web3.eth.Contract( + ERC20_ABI, + MakerSCDConstants.WETH_ADDRESS, + ); + balances[MakerSCDConstants.WETH_ADDRESS] = new BigNumber( + await scdContract.methods + .balanceOf(MakerSCDConstants.TUB_ADDRESS) + .call(null, block), + ); + + if (block >= MakerMCDConstants.STARTBLOCK) { + let joins = await getJoins(block, chain, provider, web3); + + let gemResults = await util.executeCallOfMultiTargets( + joins, + MakerMCDConstants.abi, + 'gem', + [], + block, + chain, + web3, + ); + + joins = joins.filter((_, index) => gemResults[index]); + gemResults = gemResults.filter((_, index) => gemResults[index]); + const balanceResults = await util.getTokenBalancesOfHolders( + joins, + gemResults, + block, + chain, + web3, + ); + + formatter.sumMultiBalanceOf(balances, balanceResults); + } + + formatter.convertBalancesToFixed(balances); + return { balances }; +} + +export { tvl }; diff --git a/src/factory/providers/ethereum/maker-dao/maker-mcd.json b/src/factory/providers/ethereum/maker-dao/maker-mcd.json new file mode 100644 index 00000000..6f870322 --- /dev/null +++ b/src/factory/providers/ethereum/maker-dao/maker-mcd.json @@ -0,0 +1,40 @@ +{ + "KOVANVAT": "0xba987bdb501d131f766fee8180da5d81b34b69d9", + "KOVANPOT": "0xea190dbdc7adf265260ec4da6e9675fd4f5a78bb", + "KOVANJUG": "0xcbb7718c9f39d05aeede1c472ca8bf804b2f1ead", + "KOVANSTARTBLOCK": 14764534, + "VAT": "0x35d1b3f3d7966a1dfe207aa4514c12a259a0492b", + "POT": "0x197e90f9fad81970ba7976f33cbd77088e5d7cf7", + "JUG": "0x19c0976f590d67707e62397c87829d896dc0f1f1", + "DAI": "0x6b175474e89094c44da98b954eedeac495271d0f", + "STARTBLOCK": 8928152, + "abi": [ + { + "constant": true, + "inputs": [], + "name": "ilk", + "outputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "gem", + "outputs": [{ "internalType": "address", "name": "", "type": "address" }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "Pie", + "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], + "payable": false, + "stateMutability": "view", + "type": "function" + } + ] +} diff --git a/src/factory/providers/ethereum/maker-dao/makerdao.json b/src/factory/providers/ethereum/maker-dao/makerdao.json new file mode 100644 index 00000000..65545187 --- /dev/null +++ b/src/factory/providers/ethereum/maker-dao/makerdao.json @@ -0,0 +1,5 @@ + { + "TUB_ADDRESS": "0x448a5065aebb8e423f0896e6c5d525c040f59af3", + "DAI_ADDRESS": "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359", + "WETH_ADDRESS": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" +}