-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiff.js
More file actions
75 lines (59 loc) · 3.05 KB
/
diff.js
File metadata and controls
75 lines (59 loc) · 3.05 KB
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
const { MerkleTree } = require('merkletreejs')
const keccak256 = require('keccak256')
const fs = require('fs');
const axios = require('axios')
const assert = require('assert')
const Web3Wrap = require('./web3Wrapper')
const web3 = Web3Wrap.getWeb3()
function verifyMerkleRoot(snaphotJson) {
const BPROAddress = "0xbbBBBBB5AA847A2003fbC6b5C16DF0Bd1E725f61"
const elems = []
for(const user in snaphotJson.userData) {
const userInfo = snaphotJson.userData[user]
const data = web3.eth.abi.encodeParameters(['uint256', 'uint256', 'address', 'address[]', 'uint256[]'],
[userInfo.cycle, userInfo.index, user, [BPROAddress], [userInfo.amount]])
elems.push(data)
}
const merkleTree = new MerkleTree(elems, keccak256, { hashLeaves: true, sortPairs: true })
const root = merkleTree.getHexRoot()
return root.toLowerCase() === snaphotJson.root.toLowerCase()
}
async function diff(ipfsHash, snapshotFileName) {
const url = "https://cloudflare-ipfs.com/ipfs/" + ipfsHash
console.log(url)
const ipfsSnapshot = (await axios.get(url)).data
const localSnapshot = JSON.parse(fs.readFileSync(snapshotFileName))
const ipfsUsers = Object.keys(ipfsSnapshot.userData)
const localUsers = Object.keys(localSnapshot.userData)
//assert.equal(ipfsUsers.length, localUsers.length, "user length missmatch")
//assert.deepEqual(ipfsUsers, localUsers, "user missmatch")
const toBN = web3.utils.toBN
const smallThreshold = toBN(100000000000000000) // 0.1 BPRO
const tinyThreshold = toBN(10000000000000000) // 0.01 BPRO
for(const user of ipfsUsers) {
if(! localSnapshot.userData[user]) {
console.log("new user ", user)
const ipfsAmount = toBN(ipfsSnapshot.userData[user].amount)
assert(ipfsAmount.lt(smallThreshold), "user on ipfs but not on local")
}
}
for(const user of localUsers) {
if(! ipfsSnapshot.userData[user]) {
console.log("new user ", user, "amount ", web3.utils.fromWei(localSnapshot.userData[user].amount))
const localAmount = toBN(localSnapshot.userData[user].amount)
assert(localAmount.lt(smallThreshold), "user on local but not on ipfs")
}
}
for(const user of ipfsUsers) {
const ipfsAmount = toBN(ipfsSnapshot.userData[user].amount)
const localAmount = toBN(localSnapshot.userData[user].amount)
if(ipfsAmount.lt(tinyThreshold) && localAmount.lt(tinyThreshold)) continue // diff does not matter
let multiplier = toBN(1001)
if(ipfsAmount.lt(smallThreshold) && localAmount.lt(smallThreshold)) multiplier = toBN(1200) // 20% diff
assert(ipfsAmount.mul(multiplier).gt(localAmount.mul(toBN(1000))), "ifps amount too big " + user.toString())
assert(localAmount.mul(multiplier).gt(ipfsAmount.mul(toBN(1000))), "local amount too big " + user.toString())
}
assert(verifyMerkleRoot(ipfsSnapshot), "ipfs root invalid")
console.log("OK")
}
diff(process.argv[2],"./snapshot.json")