-
Notifications
You must be signed in to change notification settings - Fork 1
/
maker.js
248 lines (193 loc) · 10.1 KB
/
maker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
const ScoreMachine = require('./score');
const {batch} = require("./batch")
const Web3Wrap = require('./web3Wrapper')
const web3 = Web3Wrap.getWeb3()
const fourBytesLen = 10
const { vatAbi, bcdpManagerAbi, dsProxyAbi } = require("./makerAbi");
const { testAddress } = require('web3-utils');
const vatAddress = "0x35D1b3F3D7966A1DFe207aa4514C12a259A0492B"
const bcdpManagerAddress = "0x3f30c2381CD8B917Dd96EB2f1A4F96D91324BBed"
const frobSig = web3.utils.soliditySha3('frob(bytes32,address,address,address,int256,int256)').substring(0, fourBytesLen) + "00000000000000000000000000000000000000000000000000000000"
const forkSig = web3.utils.soliditySha3('fork(bytes32,address,address,int256,int256)').substring(0, fourBytesLen) + "00000000000000000000000000000000000000000000000000000000"
const grabSig = web3.utils.soliditySha3('grab(bytes32,address,address,address,int256,int256)').substring(0, fourBytesLen) + "00000000000000000000000000000000000000000000000000000000"
const ETHA = "0x4554482d41000000000000000000000000000000000000000000000000000000"
const ETHB = "0x4554482d42000000000000000000000000000000000000000000000000000000"
const ETHC = "0x4554482d43000000000000000000000000000000000000000000000000000000"
const WBTCA = "0x574254432d410000000000000000000000000000000000000000000000000000"
function minus(bn) {
return new web3.utils.toBN("-1").mul(new web3.utils.toBN(bn))
}
async function filter(startBlock, endBlock, collateralMachine, debtMachine, ethRate, artRate) {
const pastEvents = await web3.eth.getPastLogs({
address: [vatAddress],
topics: [[frobSig, grabSig, forkSig]],
fromBlock : startBlock,
toBlock : endBlock
})
for(e of pastEvents) {
const data = e.data
const sig = e.topics[0] // why? because
const importantData = "0x" + data.substring(138,data.length)
if(sig === frobSig) {
// u get more dink ether and dart dai debt. rest does not matter
const decodedData = web3.eth.abi.decodeParameters([
{'type': 'bytes32','name': 'ilk'},
{'type': 'address','name': 'u'},
{'type': 'address','name': 'v'},
{'type': 'address','name': 'w'},
{'type': 'int256','name': 'dink'},
{'type': 'int256','name': 'dart'}
], importantData)
collateralMachine.addDelta(decodedData.u, e.blockNumber,decodedData.dink, ethRate[decodedData.ilk], "maker_" + decodedData.ilk.toString())
debtMachine.addDelta(decodedData.u, e.blockNumber, decodedData.dart, artRate[decodedData.ilk], "maker_" + decodedData.ilk.toString())
if(decodedData.u.toString().toLowerCase() === "0xC10781aEFdB97AD3BE4f482a9743875CB13757C8".toLowerCase()) console.log("frob")
}
if(sig === grabSig) {
// u get more dink and dart, rest does not matter.
const decodedData = web3.eth.abi.decodeParameters([
{'type': 'bytes32','name': 'ilk'},
{'type': 'address','name': 'u'},
{'type': 'address','name': 'v'},
{'type': 'address','name': 'w'},
{'type': 'int256','name': 'dink'},
{'type': 'int256','name': 'dart'}
], importantData)
if(decodedData.u.toString().toLowerCase() === "0xC10781aEFdB97AD3BE4f482a9743875CB13757C8".toLowerCase()) console.log("grab")
collateralMachine.addDelta(decodedData.u, e.blockNumber, decodedData.dink, ethRate[decodedData.ilk], "maker_" + decodedData.ilk.toString())
debtMachine.addDelta(decodedData.u, e.blockNumber, decodedData.dart, artRate[decodedData.ilk], "maker_" + decodedData.ilk.toString())
}
if(sig === forkSig) {
// src get minus dink and dart, dest get plus dink and dart
const decodedData = web3.eth.abi.decodeParameters([
{'type': 'bytes32','name': 'ilk'},
{'type': 'address','name': 'src'},
{'type': 'address','name': 'dst'},
{'type': 'int256','name': 'dink'},
{'type': 'int256','name': 'dart'}
], importantData)
if(decodedData.dst.toString().toLowerCase() === "0xC10781aEFdB97AD3BE4f482a9743875CB13757C8".toLowerCase()) console.log("fork dst")
if(decodedData.src.toString().toLowerCase() === "0xC10781aEFdB97AD3BE4f482a9743875CB13757C8".toLowerCase()) console.log("fork dst")
collateralMachine.addDelta(decodedData.dst, e.blockNumber, decodedData.dink, ethRate[decodedData.ilk], "maker_" + decodedData.dink.toString())
debtMachine.addDelta(decodedData.dst, e.blockNumber, decodedData.dart, artRate[decodedData.ilk], "maker_" + decodedData.ilk.toString())
collateralMachine.addDelta(decodedData.src, e.blockNumber, minus(decodedData.dink), ethRate[decodedData.ilk], "maker_" + decodedData.ilk.toString())
debtMachine.addDelta(decodedData.src, e.blockNumber, minus(decodedData.dart), artRate[decodedData.ilk], "maker_" + decodedData.ilk.toString())
}
}
}
async function getUrnToUserMapping() {
const cdpManager = new web3.eth.Contract(bcdpManagerAbi, bcdpManagerAddress)
const cdpi = Number(await cdpManager.methods.cdpi().call())
const urnCalls = []
const ownCalls = []
for(let i = 1 ; i <= cdpi ; i++) {
urnCalls.push({address: bcdpManagerAddress, abi: bcdpManagerAbi, method: "urns", params: [i]})
ownCalls.push({address: bcdpManagerAddress, abi: bcdpManagerAbi, method: "owns", params: [i]})
}
const urns = await batch(urnCalls, "latest")
const owns = await batch(ownCalls, "latest")
const proxyOwnerCalls = []
for(let i = 0 ; i < cdpi ; i++) {
proxyOwnerCalls.push({address: owns[i], abi: dsProxyAbi, method: "owner", params: []})
}
const owners = await batch(proxyOwnerCalls, "latest")
const mapping = {}
for(let i = 0 ; i < urns.length ; i++) {
mapping[urns[i].toLowerCase()] = owners[i].toLowerCase()
}
return mapping
}
async function initUsersBatch(collateralMachine, debtMachine, ethRate, artRate, validIlks, block) {
//const vat = new web3.eth.Contract(vatAbi, vatAddress)
const cdpManager = new web3.eth.Contract(bcdpManagerAbi, bcdpManagerAddress)
//const block = await web3.eth.getBlockNumber() - 10 // minus 10 to avoid reorg
const cdpi = Number(await cdpManager.methods.cdpi().call())
const ilkCalls = []
const urnCalls = []
let i
for(i = 1 ; i <= cdpi ; i++) {
ilkCalls.push({address: bcdpManagerAddress, abi: bcdpManagerAbi, method: "ilks", params: [i]})
urnCalls.push({address: bcdpManagerAddress, abi: bcdpManagerAbi, method: "urns", params: [i]})
}
const ilks = await batch(ilkCalls, block)
const urns = await batch(urnCalls, block)
const balanceCalls = []
for(i = 0 ; i < cdpi ; i++) {
balanceCalls.push({address: vatAddress, abi: vatAbi, method: "urns", params: [ilks[i], urns[i]]})
}
console.log("reading balances")
const balances = await batch(balanceCalls, block)
//console.log({balances})
const debts = []
const collateral = []
const users = []
for(let i = 0 ; i < cdpi ; i++) {
const urn = urns[i]
const ilk = ilks[i]
const balance = balances[i]
const ink = balance[0]
const art = balance[1]
debts.push(art)
collateral.push(ink)
users.push(urn)
//console.log({ilk}, {validIlks}, ilk in validIlks)
if(! validIlks.includes(ilk)) continue
if((collateralMachine !== null) && (debtMachine !== null)) {
collateralMachine.setRelevantUser(urn)
debtMachine.setRelevantUser(urn)
collateralMachine.setInitialBalance(urn, block, ink, ethRate[ilk], "maker_" + ilk.toString())
debtMachine.setInitialBalance(urn, block, art, artRate[ilk], "maker_" + ilk.toString())
}
}
//console.log({balances})
//sd
const output = { "block" : block, "users" : users, "borrowBalances" : debts, "collateralBalances" : collateral }
return output
}
async function runMaker(ethRate, artRate, collateralMachine, debtMachine, startBlock) {
//const collateralMachine = new ScoreMachine(web3)
//const debtMachine = new ScoreMachine(web3)
//const startBlock = 11081908
//const ethRate = web3.utils.toWei("2200")
const block = await web3.eth.getBlockNumber() - 10 // minus 10 to avoid reorg
if(! artRate[WBTCA]) {
console.log("making a dicionary from art rate")
artRate[WBTCA] = artRate[ETHA]
}
const res = await initUsersBatch(collateralMachine, debtMachine, ethRate, artRate, [ETHA, ETHB, ETHC, WBTCA], block)
const endBlock = res.block
console.log({endBlock})
const step = 2e4
console.log("finish init users")
for(let i = startBlock ; i <= endBlock ; i += step) {
console.log({i})
const lastBlock = (i + step - 1) > endBlock ? endBlock : (i + step - 1)
await filter(i, lastBlock, collateralMachine, debtMachine, ethRate, artRate)
}
//const ilk = "0x4554482d41000000000000000000000000000000000000000000000000000000"
//balances.getCollateralBalances(balances.getAllUsers(), ilk, startBlock, endBlock, eposhEndBlock)
//collateralMachine.getUserScoreArray(startBlock, endBlock)
//debtMachine.getUserScoreArray(startBlock, endBlock)
console.log(endBlock - startBlock)
//console.log(JSON.stringify(balances))
}
async function getArtRate(block) {
const vat = new web3.eth.Contract(vatAbi, vatAddress)
const ilkInfoA = await vat.methods.ilks(ETHA).call(block)
const ilkInfoB = await vat.methods.ilks(ETHB).call(block)
const ilkInfoC = await vat.methods.ilks(ETHC).call(block)
const ilkInfoWbtcA = await vat.methods.ilks(WBTCA).call(block)
const rateBNA = new web3.utils.toBN(ilkInfoA.rate)
const rateBNB = new web3.utils.toBN(ilkInfoB.rate)
const rateBNC = new web3.utils.toBN(ilkInfoC.rate)
const rateBNW = new web3.utils.toBN(ilkInfoWbtcA.rate)
const rayToWadFactor = new web3.utils.toBN(1e9)
return { [ETHA] : rateBNA.div(rayToWadFactor), [ETHB] : rateBNB.div(rayToWadFactor), [ETHC] : rateBNC.div(rayToWadFactor), [WBTCA] : rateBNW.div(rayToWadFactor) }
}
async function getBalances(block) {
const output = await initUsersBatch(null, null, null, null, [ETHA, ETHB, ETHC, WBTCA], block)
return output
}
module.exports.runMaker = runMaker
module.exports.getArtRate = getArtRate
module.exports.getBalances = getBalances
module.exports.getUrnToUserMapping = getUrnToUserMapping