-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
319 lines (231 loc) · 11.8 KB
/
main.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const ScoreMachine = require('./score')
const Maker = require('./maker')
const Compound = require('./compound')
const MerkleEncode = require('./merkleEncode')
const Web3Wrap = require('./web3Wrapper')
const fs = require('fs');
const assert = require("assert")
const cBAT = "0x6c8c6b02e7b2be14d4fa6022dfd6d75921d90e4e"
const cCOMP = "0x70e36f6bf80a52b3b46b3af8e106cc0ed743e8e4"
const cDAI = "0x5d3a536e4d6dbd6114cc1ead35777bab948e3643"
const cETH = "0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5"
const cUNI = "0x35a18000230da775cac24873d00ff85bccded550"
const cUSDC = "0x39aa39c021dfbae8fac545936693ac917d5e7563"
const cUSDT = "0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9"
const cWBTC = "0xc11b1268c1a384e55c48c2391d8d480264a3a7f4"
const cWBTC2 = "0xccf4429db6322d5c611ee964527d42e5d685dd6a"
const cZRX = "0xb3319f5d18bc0d84dd1b4825dcde5d5f7266d407"
const start = 12304534
//const curr = 12304534
const snapshotEnd = 12347296
const ctokens = [cETH, cBAT, cCOMP, cDAI, cUNI, cUSDC, cUSDT, cWBTC, cWBTC2, cZRX]
const ETHA = "0x4554482d41000000000000000000000000000000000000000000000000000000"
const ETHB = "0x4554482d42000000000000000000000000000000000000000000000000000000"
const ETHC = "0x4554482d43000000000000000000000000000000000000000000000000000000"
const WBTCA = "0x574254432d410000000000000000000000000000000000000000000000000000"
const web3 = Web3Wrap.getWeb3();
const collateralMachine = new ScoreMachine(web3)
const debtMachine = new ScoreMachine(web3)
async function initDB(startBlock, prevJson) {
const rates = prevJson["rates"]["compRates"]
//const rates = await Compound.getRates(ctokens, blockPrice)
assert(ctokens[0] === cETH, "first ctoken should be eth")
assert(ctokens[ctokens.length - 2] === cWBTC2, "second ctoken before last should be wbtc")
const ethRate = {}
ethRate[ETHA] = ethRate[ETHB] = ethRate[ETHC] = rates["underlyingRate"][0]
const wbtcRate = rates["underlyingRate"][ctokens.length - 2]
const normWbtcRate = new web3.utils.toBN(rates["underlyingRate"][ctokens.length - 2]).div(new web3.utils.toBN(10).pow(new web3.utils.toBN(10)))
ethRate[WBTCA] = normWbtcRate.toString(16)
const artRate = prevJson["rates"]["artRate"]
//await Maker.getArtRate(blockPrice)
console.log({rates}, {artRate},{ethRate})
await Maker.runMaker(ethRate, artRate, collateralMachine, debtMachine, startBlock)
await Compound.runCompound(ctokens, rates["ctokenRate"], rates["underlyingRate"], collateralMachine, debtMachine, startBlock)
const newRates = await Compound.getRates(ctokens, "latest")
const newArtRate = await Maker.getArtRate("latest")
return { "compRates" : newRates, "artRate": newArtRate }
}
async function analyze(startBlock, endBlock) {
//const users = collateralMachine.getAllUsers()
//const allUserBalance = collateralMachine.getBalanceArray(users, startBlock, curr + 1000)
//const allUserDebt = debtMachine.getBalanceArray(users, startBlock, curr + 1000)
const collatScores = collateralMachine.getUserScoreArray(startBlock, endBlock)
const debtScores = debtMachine.getUserScoreArray(startBlock, endBlock)
const output = { "startBlock" : startBlock, "endBlock" : endBlock, "collateralScores" : collatScores, "debtScores" : debtScores}
if(!process.env.SERVERLESS){
fs.writeFileSync('./scoreRawData.json', JSON.stringify(output, null, 2) , 'utf-8')
}
return JSON.stringify(output, null, 2)
}
async function generateAdditionalBProSnapshot(scoreRawJson) {
const factor = new web3.utils.toBN("10").pow(new web3.utils.toBN("48"))
const BLOCKS_PER_YEAR = 45 * 60 * 24 * 365 / 10 // 4.5 blocks per minute
const BLOCKS_PER_MONTH = (BLOCKS_PER_YEAR / 12)
const dripPerMonth = new web3.utils.toBN(web3.utils.toWei("250000")).div(new web3.utils.toBN(6))
const dripPerBlock = dripPerMonth.div(new web3.utils.toBN(BLOCKS_PER_MONTH))
console.log({dripPerBlock})
const bproPerBlockCollat = dripPerBlock.div(new web3.utils.toBN(5))
const bproPerBlockDebt = dripPerBlock.sub(bproPerBlockCollat)
const rawScore = process.env.SERVERLESS ? scoreRawJson : fs.readFileSync('./scoreRawData.json');
const scoreJson = JSON.parse(rawScore)
let sumCollat = new web3.utils.toBN("0")
let sumDebt = new web3.utils.toBN("0")
console.log("getting maker users")
const makerUsers = await Maker.getUrnToUserMapping()
const compoundUsers = await Compound.getAvatarToUserMapping()
console.log({makerUsers})
console.log("done")
const bproJson = {}
bproJson["startBlock"] = scoreJson["startBlock"]
bproJson["endBlock"] = scoreJson["endBlock"]
bproJson["bpro"] = {}
for(const user in scoreJson["collateralScores"]) {
const score = new web3.utils.toBN("0x" + scoreJson["collateralScores"][user])
const bpro = score.mul(bproPerBlockCollat).div(factor)
let realUser
let isMaker = false
if(user in makerUsers) {
//console.log("maker user")
realUser = makerUsers[user].toLowerCase()
isMaker = true
}
else realUser = compoundUsers[user].toLowerCase()
if(! (realUser in bproJson["bpro"])) {
bproJson["bpro"][realUser] = { "total" : new web3.utils.toBN("0"), "maker" : new web3.utils.toBN("0")}
}
bproJson["bpro"][realUser]["total"] = bproJson["bpro"][realUser]["total"].add(bpro)
if(isMaker) bproJson["bpro"][realUser]["maker"] = bproJson["bpro"][realUser]["maker"].add(bpro)
sumCollat = sumCollat.add(bpro)
}
for(const user in scoreJson["debtScores"]) {
const score = new web3.utils.toBN("0x" + scoreJson["debtScores"][user])
const bpro = score.mul(bproPerBlockDebt).div(factor)
let realUser = user
let isMaker = false
if(user in makerUsers) {
//console.log("maker user")
realUser = makerUsers[user]
isMaker = true
}
else realUser = compoundUsers[user]
if(! (realUser in bproJson["bpro"])) {
bproJson["bpro"][realUser] = { "total" : new web3.utils.toBN("0"), "maker" : new web3.utils.toBN("0")}
}
bproJson["bpro"][realUser]["total"] = bproJson["bpro"][realUser]["total"].add(bpro)
if(isMaker) bproJson["bpro"][realUser]["maker"] = bproJson["bpro"][realUser]["maker"].add(bpro)
sumDebt = sumDebt.add(bpro)
}
bproJson["sumDebt"] = sumDebt
bproJson["sumCollat"] = sumCollat
if(!process.env.SERVERLESS){
fs.writeFileSync('./bproAdditional.json', JSON.stringify(bproJson, null, 2) , 'utf-8')
}
console.log({sumCollat}, {sumDebt})
return bproJson
}
async function main(startBlock, snapshotBlock, prevSnapshot) {
const newRates = await initDB(startBlock, prevSnapshot)
const analyzeRes = await analyze(startBlock, snapshotBlock)
const bproAdditionalJson = await generateAdditionalBProSnapshot(analyzeRes)
console.log("encoding snapshot")
const snapshotJson = await MerkleEncode.encodeClaims(bproAdditionalJson, prevSnapshot, newRates)
// fs.writeFileSync('./snapshotJson.json', JSON.stringify(snapshotJson, null, 2) , 'utf-8')
console.log({newRates})
return JSON.stringify(snapshotJson, null, 2)
//MerkleEncode.testClaims('./snapshotJson.json')
}
//main(start, snapshotEnd)
async function test(blockToTest, blockPrice) {
if(blockPrice === "latest") {
blockPrice = await web3.eth.getBlockNumber() - 5
}
const rates = await Compound.getRates(ctokens, blockPrice)
console.log({rates})
assert(ctokens[0] === cETH, "first ctoken should be eth")
const ethRate = new web3.utils.toBN(rates["underlyingRate"][0])
const artRate = new web3.utils.toBN(await Maker.getArtRate(blockPrice))
const debts = {}
const collat = {}
const compoundBalances = await Compound.getBalances(ctokens, blockToTest)
const compoundUsers = compoundBalances.users
for(const avatar of compoundUsers) {
debts[avatar] = new web3.utils.toBN("0")
collat[avatar] = new web3.utils.toBN("0")
for(let i = 0 ; i < ctokens.length ; i++) {
ctoken = ctokens[i]
//console.log({avatar})
const debt = new web3.utils.toBN(compoundBalances["balances"][avatar][ctoken]["debt"])
const collateral = new web3.utils.toBN(compoundBalances["balances"][avatar][ctoken]["collateral"])
const debtInUsd = debt.mul(rates["underlyingRate"][i])
const collatInUsd = collateral.mul(rates["ctokenRate"][i])
debts[avatar] = debts[avatar].add(debtInUsd)
collat[avatar] = collat[avatar].add(collatInUsd)
if(avatar === "0xf258391e3dAA6c80ec11e9808c9775574137dBd9") {
console.log("eitan",debtInUsd.toString(), {ctoken},{debt},rates["underlyingRate"][i].toString(), rates["ctokenRate"][i].toString())
}
}
//console.log(debts[avatar], collat[avatar])
//if(avatar === "0xf258391e3dAA6c80ec11e9808c9775574137dBd9") assert(false)
}
const makerBalances = await Maker.getBalances(blockToTest)
// user to usd value
const makerUsers = makerBalances.users
const makerDebts = makerBalances.borrowBalances
const makerCollt = makerBalances.collateralBalances
for(let i = 0 ; i < makerUsers.length ; i++) {
debts[makerUsers[i]] = (new web3.utils.toBN(makerDebts[i])).mul(artRate)
collat[makerUsers[i]] = (new web3.utils.toBN(makerCollt[i])).mul(ethRate)
}
console.log("init db")
await initDB(start, blockPrice)
const endBlock = await web3.eth.getBlockNumber()
for(let user in collat) {
const realCollat = collat[user]
const collatArray = collateralMachine.getBalanceArray([user], start, endBlock)
const collateral = collatArray[blockToTest - start]
console.log({collateral}, {realCollat}, {blockToTest}, {user})
assert(prettyClose(collateral, realCollat), user.toString())
//assert.equal(collateral.toString(), realCollat.toString(), user.toString())
}
for(let user in debts) {
//if(user !== "0xf258391e3dAA6c80ec11e9808c9775574137dBd9") continue
//user = "0xC10781aEFdB97AD3BE4f482a9743875CB13757C8"
//console.log(user)
const realDebt = debts[user]
const debtArray = debtMachine.getBalanceArray([user], start, endBlock)
const debt = debtArray[blockToTest - start]
console.log({realDebt}, {debt}, {blockToTest}, {user})
//console.log(JSON.stringify(debtArray))
assert(prettyClose(debt, realDebt), user.toString())
//assert.equal(debt.toString(), realDebt.toString(), user.toString())
}
console.log("succ")
return
console.log("testing user 123")
const makerUser = makerUsers[123]
console.log({makerUser})
console.log(collat[makerUser])
const userBalanceArray = collateralMachine.getBalanceArray([makerUser],start, endBlock)
const index = blockToTest - start
console.log({userBalanceArray})
console.log(userBalanceArray[index])
//console.log(JSON.stringify(makerBalances))
//console.log(JSON.stringify(debts))
//console.log(JSON.stringify(collat))
}
//test(curr, "latest")
//Compound.getDebt(ctokens, "0x56D419Da994b1d12d9BF83563541d2E96742C329", 12328798)
function prettyClose(num1, num2) {
const bigNum1 = new web3.utils.toBN(num1)
const bigNum2 = new web3.utils.toBN(num2)
const _1000 = new web3.utils.toBN(1000)
const _1001 = new web3.utils.toBN(1001)
const _1e18 = new web3.utils.toBN(web3.utils.toWei("1"))
const _1e36 = _1e18.mul(_1e18)
const small = _1e36.mul(new web3.utils.toBN(10))
if(bigNum1.lt(small) && bigNum2.lt(small)) return true
if(bigNum1.mul(_1000).gt(bigNum2.mul(_1001))) return false
if(bigNum2.mul(_1000).gt(bigNum1.mul(_1001))) return false
return true
}
module.exports.main = main