-
Notifications
You must be signed in to change notification settings - Fork 1
/
merkleEncode.js
151 lines (113 loc) · 5.44 KB
/
merkleEncode.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
const {batch} = require("./batch")
const { MerkleTree } = require('merkletreejs')
const keccak256 = require('keccak256')
const fs = require('fs');
const Web3Wrap = require('./web3Wrapper')
const web3 = Web3Wrap.getWeb3()
const { rewardsDistributorAbi } = require("./merkleEncodeAbi");
const assert = require("assert");
const rewardsDistributorAddress = "0x3fEf090ED8C8b1Ad29C9F745464dFeCE47053345"
const BPROAddress = "0xbbBBBBB5AA847A2003fbC6b5C16DF0Bd1E725f61"
async function encodeClaims(bproJson, prevJson, rates) {
const calls = []
let index = 0
const cycle = prevJson["cycle"] ? prevJson["cycle"] + 1 : 3
const claimJson = {"userData" :{}}
const uniqueUsers = []
let sum = new web3.utils.toBN("0")
// users array - not unique
/*
console.log({bproJson})
console.log(bproJson["bpro"])
console.log({prevJson})
console.log(prevJson["userData"]) */
const users = Object.keys(bproJson["bpro"]).concat(Object.keys(prevJson["userData"]))
for(const user of users /*in bproJson["bpro"]*/) {
let amount = new web3.utils.toBN("0")
let maker = new web3.utils.toBN("0")
const userLowerCase = user.toLocaleLowerCase()
const userCheckSum = web3.utils.toChecksumAddress(user)
if(userLowerCase in claimJson["userData"]) continue
if(bproJson["bpro"][userLowerCase]) {
amount = amount.add(bproJson["bpro"][userLowerCase]["total"])
maker = maker.add(bproJson["bpro"][userLowerCase]["maker"])
//console.log({amount}, bproJson["bpro"][user]["total"], amount.toString(10), amount.toString(16))
//return
}
if(bproJson["bpro"][userCheckSum] && ((userCheckSum !== userLowerCase))) {
amount = amount.add(bproJson["bpro"][userCheckSum]["total"])
maker = maker.add(bproJson["bpro"][userCheckSum]["maker"])
//console.log({amount}, bproJson["bpro"][user]["total"], amount.toString(10), amount.toString(16))
//return
}
if(prevJson["userData"][userLowerCase]) {
//console.log(prevJson["userData"][user]["amount"], prevJson["userData"][user]["maker"])
amount = amount.add(new web3.utils.toBN(prevJson["userData"][userLowerCase]["amount"]))
maker = maker.add(new web3.utils.toBN(prevJson["userData"][userLowerCase]["makerAmount"]))
}
if(prevJson["userData"][userCheckSum] && (userCheckSum !== userLowerCase)) {
//console.log(prevJson["userData"][user]["amount"], prevJson["userData"][user]["maker"])
amount = amount.add(new web3.utils.toBN(prevJson["userData"][userCheckSum]["amount"]))
maker = maker.add(new web3.utils.toBN(prevJson["userData"][userCheckSum]["makerAmount"]))
}
//const amount = "0x" + bproJson["bpro"][user]["total"]
//const maker = "0x" + bproJson["bpro"][user]["maker"]
//console.log(bproJson["bpro"])
//console.log({amount})
const input = [cycle, index, user, [BPROAddress], [amount.toString(10)]]
//console.log({input})
claimJson["userData"][userLowerCase] = {}
claimJson["userData"][userLowerCase]["amount"] = "0x" + amount.toString(16)
claimJson["userData"][userLowerCase]["makerAmount"] = "0x" + maker.toString(16)
claimJson["userData"][userLowerCase]["cycle"] = cycle
claimJson["userData"][userLowerCase]["index"] = index
sum = sum.add(new web3.utils.toBN(amount))
calls.push({address: rewardsDistributorAddress, abi: rewardsDistributorAbi, method: "encodeClaim", params : input})
uniqueUsers.push(userLowerCase)
index++
}
const result = await batch(calls, "latest")
const elements = []
const leaves = []
for(const r of result) {
//console.log(r[1])
elements.push(r[0])
leaves.push(r[1])
}
const merkleTree = new MerkleTree(elements, keccak256, { hashLeaves: true, sortPairs: true })
const root = merkleTree.getHexRoot()
claimJson["root"] = root
claimJson["startBlock"] = bproJson["startBlock"]
claimJson["endBlock"] = bproJson["endBlock"]
claimJson["totalClaims"] = sum.toString()
claimJson["rates"] = rates
claimJson["cycle"] = cycle
claimJson["sumDebt"] = bproJson["sumDebt"]
claimJson["sumCollat"] = bproJson["sumCollat"]
for(let i = 0 ; i < leaves.length ; i++) {
const proof = merkleTree.getHexProof(leaves[i])
const user = uniqueUsers[i]
claimJson["userData"][user]["proof"] = proof
}
return claimJson
}
async function testClaims(jsonFileName) {
const claimJson = (JSON.parse(fs.readFileSync(jsonFileName)))["userData"]
const contract = new web3.eth.Contract(rewardsDistributorAbi, rewardsDistributorAddress)
console.log(claimJson)
for(const user in claimJson) {
console.log("XXX",user)
console.log(claimJson[user])
const cycle = claimJson[user]["cycle"]
const index = claimJson[user]["index"]
const amount = claimJson[user]["amount"]
const proof = claimJson[user]["proof"]
console.log(cycle, index, amount, proof)
const res = await contract.methods.isValidClaim(cycle, index, user, [BPROAddress], [amount], proof).call()
assert(res, "invalid claim")
if(! res) return
console.log(index)
}
}
module.exports.encodeClaims = encodeClaims
module.exports.testClaims = testClaims