Skip to content

Commit 0791ac8

Browse files
0xpelucheg1nt0ki
andauthored
[WIP] -Fix: Maker (DefiLlama#12211)
Co-authored-by: g1nt0ki <[email protected]>
1 parent d356afd commit 0791ac8

File tree

2 files changed

+118
-330
lines changed

2 files changed

+118
-330
lines changed

projects/maker-rwa/index.js

Lines changed: 59 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1,185 +1,79 @@
11
const ADDRESSES = require('../helper/coreAssets.json')
2-
// const utils = require('web3-utils');
3-
const sdk = require('@defillama/sdk');
4-
const MakerSCDConstants = require("../maker/abis/makerdao.js");
5-
const MakerMCDConstants = require("../maker/abis/maker-mcd.js");
6-
const { sumTokens2 } = require('../helper/unwrapLPs')
7-
const { getLogs } = require('../helper/cache/getLogs')
2+
const { getLogs2 } = require('../helper/cache/getLogs')
83

9-
async function getJoins(block, api) {
10-
// let rely = utils.sha3("rely(address)").substr(0, 10);
11-
// let relyTopic = utils.padRight(rely, 64);
12-
let relyTopic = '0x65fae35e00000000000000000000000000000000000000000000000000000000'
4+
const MCD_VAT = '0x35d1b3f3d7966a1dfe207aa4514c12a259a0492b'
5+
const VAT_topic = '0x65fae35e00000000000000000000000000000000000000000000000000000000'
6+
const START_VAT_BLOCK = 8928152
137

14-
let joins = [];
15-
let failed = [];
16-
const failedSet = new Set(failedJoins)
8+
const MCD_DOG = '0x135954d155898d42c90d2a57824c690e0c7bef1b'
9+
const DOG_topic = '0x4ff2caaa972a7c6629ea01fae9c93d73cc307d13ea4c369f9bbbb7f9b7e9461d'
10+
const START_DOG_BLOCK = 12317310
1711

18-
// get list of auths
19-
const auths = (
20-
await getLogs({
21-
api,
22-
target: MakerMCDConstants.VAT,
23-
fromBlock: MakerMCDConstants.STARTBLOCK,
24-
topics: [relyTopic],
25-
})
26-
).map(i => `0x${i.topics[1].substr(26)}`).filter(i => !failedSet.has(i))
27-
28-
const ilks = await api.multiCall({
29-
abi: MakerMCDConstants.ilk,
30-
calls: auths,
31-
permitFailure: true,
32-
});
12+
const abi = {
13+
ilk: 'function ilk() view returns (bytes32)',
14+
ilks: 'function ilks (bytes32) view returns (uint256 art, uint256 rate, uint256 spot, uint256 line,uint256 dust)',
15+
gem: "address:gem",
16+
dog: "address:dog",
17+
}
3318

34-
ilks.forEach((_, i) => {
35-
const token = auths[i].toLowerCase()
36-
if (_) joins.push(token)
37-
else failed.push(token)
19+
const getJoins = async (api) => {
20+
const logs = (await getLogs2({ api, target: MCD_VAT, fromBlock: START_VAT_BLOCK, topics: [VAT_topic] })).map(log => {
21+
return '0x' + log.topics[1].slice(-40);
3822
})
3923

40-
if (failed.length) sdk.log('failed', failed)
41-
42-
return joins;
24+
const ilks = await api.multiCall({ abi: abi.ilk, calls: logs, permitFailure: true })
25+
26+
return logs.map((auth, i) => {
27+
const ilk = ilks[i];
28+
if (!ilk) return null
29+
return auth.toLowerCase();
30+
}).filter(Boolean);
4331
}
4432

45-
async function tvl(api) {
46-
const block = api.block
47-
let toa = []
48-
49-
const blacklistedJoins = [
50-
'0x7b3799b30f268ba55f926d7f714a3001af89d359',
51-
'0x41ca7a7aa2be78cf7cb80c0f4a9bdfbc96e81815',
52-
]
53-
if (block > MakerMCDConstants.STARTBLOCK) {
54-
let joins = await getJoins(block, api);
55-
const dogSet = new Set(dogs)
56-
joins = joins.filter(i => !blacklistedJoins.includes(i) && !dogSet.has(i))
57-
58-
const { output: gems } = await sdk.api.abi.multiCall({
59-
abi: MakerMCDConstants.gem,
60-
block, calls: joins.map(i => ({ target: i })),
61-
permitFailure: true,
62-
})
63-
const dogCalls = dogs.map(i => ({ target: i }))
64-
65-
gems.forEach(({ success, output, input: { target } }) => {
66-
target = target.toLowerCase()
67-
if (!success) {
68-
dogCalls.push({ target })
69-
return;
70-
}
71-
72-
toa.push([output, target])
73-
})
74-
75-
const { output: dogRes } = await sdk.api.abi.multiCall({
76-
abi: MakerMCDConstants.dog,
77-
calls: dogCalls, block,
78-
permitFailure: true,
79-
})
80-
81-
const failedCalls = dogRes.filter(i => !i.success)
82-
if (failedCalls.length) {
83-
failedCalls.forEach(i => sdk.log('Failed both gem and dog calls', i.input.target))
84-
throw new Error('Failed both gem and dog calls')
85-
}
86-
}
33+
const getDogs = async (api) => {
34+
const logs = (await getLogs2({ api, target: MCD_DOG, fromBlock: START_DOG_BLOCK, topics: [DOG_topic], })).map(log => {
35+
return '0x' + log.data.slice(-40);
36+
})
8737

88-
toa = toa.filter(i => i[0].toLowerCase() !== ADDRESSES.ethereum.SAI.toLowerCase())
89-
const symbols = await api.multiCall({ abi: 'erc20:symbol', calls: toa.map(t => t[0]) })
38+
const dogs = await api.multiCall({ abi: abi.dog, calls: logs, permitFailure: true })
39+
40+
return logs.map((auth, i) => {
41+
const dog = dogs[i];
42+
if (!dog) return null
43+
return auth.toLowerCase();
44+
}).filter(Boolean);
45+
}
9046

91-
const owners = []
92-
toa.map((_, i) => {
93-
if (!symbols[i].startsWith('RWA')) return;
94-
owners.push(toa[i][1])
95-
})
96-
const ilks = await api.multiCall({ abi: 'function ilk() view returns (bytes32)', calls: owners })
97-
const res = await api.multiCall({ abi: 'function ilks (bytes32) view returns (uint256 art, uint256 rate, uint256 spot, uint256 line,uint256 dust)', calls:ilks, target:'0x35D1b3F3D7966A1DFe207aa4514C12a259A0492B' })
47+
const tvl = async (api) => {
48+
const [joins/*, dogs*/] = await Promise.all([
49+
getJoins(api),
50+
// getDogs(api)
51+
])
52+
53+
const tokens = await api.multiCall({ abi: abi.gem, calls: joins, permitFailure: true })
54+
55+
let toas = joins.map((join, i) => {
56+
const token = tokens[i];
57+
if (!token) return null
58+
return [token, join]
59+
}).filter(Boolean)
60+
61+
toas = toas.filter(i => i[0].toLowerCase() !== ADDRESSES.ethereum.SAI.toLowerCase())
62+
const symbols = await api.multiCall({ abi: 'erc20:symbol', calls: toas.map(t => t[0]) })
63+
const owners = toas.map((toa, i) => {
64+
if (!symbols[i].startsWith('RWA')) return null
65+
return toa[1]
66+
}).filter(Boolean)
67+
68+
const ilks = await api.multiCall({ abi: abi.ilk, calls: owners })
69+
const res = await api.multiCall({ abi: abi.ilks, calls: ilks, target:'0x35D1b3F3D7966A1DFe207aa4514C12a259A0492B' })
9870
res.forEach(i => api.add(ADDRESSES.ethereum.DAI, i.art))
99-
100-
return api.getBalances()
10171
}
10272

10373
module.exports = {
104-
methodology: `Counts all the tokens being used as collateral of CDPs.
105-
106-
On the technical level, we get all the collateral tokens by fetching events, get the amounts locked by calling balanceOf() directly, unwrap any uniswap LP tokens and then get the price of each token from coingecko`,
74+
methodology: `Counts all the tokens being used as collateral of CDPs. On the technical level, we get all the collateral tokens by fetching events, get the amounts locked by calling balanceOf() directly, unwrap any uniswap LP tokens and then get the price of each token from coingecko`,
10775
start: 1513566671, // 12/18/2017 @ 12:00am (UTC)
10876
ethereum: {
10977
tvl
11078
},
11179
};
112-
113-
const dogs = [
114-
'0x832dd5f17b30078a5e46fdb8130a68cbc4a74dc0',
115-
'0x9dacc11dcd0aa13386d295eaeebbd38130897e6f',
116-
'0xc67963a226eddd77b91ad8c421630a1b0adff270',
117-
'0x71eb894330e8a4b96b8d6056962e7f116f50e06f',
118-
'0xc2b12567523e3f3cbd9931492b91fe65b240bc47',
119-
'0x0227b54adbfaeec5f1ed1dfa11f54dcff9076e2c',
120-
'0x3d22e6f643e2f4c563fd9db22b229cbb0cd570fb',
121-
'0xdc90d461e148552387f3ab3ebee0bdc58aa16375',
122-
'0x006aa3eb5e666d8e006aa647d4afab212555ddea',
123-
'0xf5c8176e1eb0915359e46ded16e52c071bb435c0',
124-
'0x2bb690931407dca7ece84753ea931ffd304f0f38',
125-
'0x81c5cdf4817dbf75c7f08b8a1cdab05c9b3f70f7',
126-
'0x6aac067bb903e633a422de7be9355e62b3ce0378',
127-
'0x3713f83ee6d138ce191294c131148176015bc29a',
128-
'0x834719bea8da68c46484e001143bdde29370a6a3',
129-
'0x8723b74f598de2ea49747de5896f9034cc09349e',
130-
'0x9f6981ba5c77211a34b76c6385c0f6fa10414035',
131-
'0x93ae03815baf1f19d7f18d9116e4b637cc32a131',
132-
'0x2ac4c9b49051275acb4c43ec973082388d015d48',
133-
'0x4fc53a57262b87abda61d6d0db2be7e9be68f6b8',
134-
'0xb15afab996904170f87a64fe42db0b64a6f75d24',
135-
'0x6aa0520354d1b84e1c6abfe64a708939529b619e',
136-
'0xb0ece6f5542a4577e2f1be491a937ccbbec8479e',
137-
'0x854b252ba15eafa4d1609d3b98e00cc10084ec55',
138-
'0xe4b82be84391b9e7c56a1fc821f47569b364dd4a',
139-
'0x046b1a5718da6a226d912cfd306ba19980772908',
140-
'0x5590f23358fe17361d7e4e4f91219145d8ccfcb3',
141-
'0x0f6f88f8a4b918584e3539182793a0c276097f44',
142-
'0xfc9d6dd08bee324a5a8b557d2854b9c36c2aec5d',
143-
'0xbcb396cd139d1116bd89562b49b9d1d6c25378b0',
144-
'0xa47d68b9db0a0361284fa04ba40623fcbd1a263e',
145-
'0x66609b4799fd7ce12ba799ad01094abd13d5014d',
146-
'0x9b3310708af333f6f379fa42a5d09cbaa10ab309',
147-
'0x5322a3551bc6a1b39d5d142e5e38dc5b4bc5b3d2',
148-
'0x29342f530ed6120bdb219d602dafd584676293d1',
149-
'0x5048c5cd3102026472f8914557a1fd35c8dc6c9e',
150-
'0x49a33a28c4c7d9576ab28898f4c9ac7e52ea457a',
151-
'0xa93b98e57dde14a3e301f20933d59dc19bf8212e',
152-
'0xe30663c6f83a06edee6273d72274ae24f1084a22',
153-
'0x39f29773dcb94a32529d0612c6706c49622161d1',
154-
'0xf93cc3a50f450ed245e003bfecc8a6ec1732b0b2',
155-
'0xb55da3d3100c4ebf9de755b6ddc24bf209f6cc06',
156-
'0x1926862f899410bfc19fefb8a3c69c7aed22463a',
157-
'0x3ea60191b7d5990a3544b6ef79983fd67e85494a',
158-
'0x27ca5e525ea473ed52ea9423cd08ccc081d96a98',
159-
'0xd9e758bd239e5d568f44d0a748633f6a8d52cbbb',
160-
]
161-
162-
const failedJoins = [
163-
'0xbaa65281c2fa2baacb2cb550ba051525a480d3f4',
164-
'0x65c79fcb50ca1594b025960e539ed7a9a6d434a3',
165-
'0x19c0976f590d67707e62397c87829d896dc0f1f1',
166-
'0x197e90f9fad81970ba7976f33cbd77088e5d7cf7',
167-
'0x78f2c2af65126834c51822f56be0d7469d7a523e',
168-
'0xab14d3ce3f733cacb76ec2abe7d2fcb00c99f3d5',
169-
'0xbe8e3e3618f7474f8cb1d074a26affef007e98fb',
170-
'0x4d95a049d5b0b7d32058cd3f2163015747522e99',
171-
'0xa41b6ef151e06da0e34b009b86e828308986736d',
172-
'0xa5679c04fc3d9d8b0aab1f0ab83555b301ca70ea',
173-
'0xc7bdd1f2b16447dcf3de045c4a039a60ec2f0ba3',
174-
'0x88f88bb9e66241b73b84f3a6e197fbba487b1e30',
175-
'0xbb856d1742fd182a90239d7ae85706c2fe4e5922',
176-
'0x29cfbd381043d00a98fd9904a431015fef07af2f',
177-
'0x135954d155898d42c90d2a57824c690e0c7bef1b',
178-
'0x1eb4cf3a948e7d72a198fe073ccb8c7a948cd853',
179-
'0x2cc583c0aacdac9e23cb601fda8f1a0c56cdcb71',
180-
'0x09e05ff6142f2f9de8b6b65855a1d56b6cfe4c58',
181-
'0xa4c22f0e25c6630b2017979acf1f865e94695c4b',
182-
'0x0e2e8f1d1326a4b9633d96222ce399c708b19c28',
183-
'0x60744434d6339a6b27d73d9eda62b6f66a0a04fa',
184-
'0x12f36cdea3a28c35ac8c6cc71d9265c17c74a27f',
185-
]

0 commit comments

Comments
 (0)