-
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 #196 from dappradar/makerdao-fix
makerdao fix [staging]
- Loading branch information
Showing
3 changed files
with
190 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,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<Partial<ITvlReturn>> { | ||
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 }; |
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,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" | ||
} | ||
] | ||
} |
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,5 @@ | ||
{ | ||
"TUB_ADDRESS": "0x448a5065aebb8e423f0896e6c5d525c040f59af3", | ||
"DAI_ADDRESS": "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359", | ||
"WETH_ADDRESS": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" | ||
} |