-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepositsOnChain.js
60 lines (48 loc) · 2.3 KB
/
depositsOnChain.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
var { web3, beanstalk } = require('./utils/web3.js')
var fs = require('fs');
async function getDeposits(blockNumber) {
//check if ./data/deposits-events.json exits, if it does, return. Uncomment this to use cached version.
// if (fs.existsSync('./data/deposits-onchain.json')) {
// console.log('deposits-onchain.json already exists, skipping');
// return;
// }
const deposits = JSON.parse(await fs.readFileSync('./data/deposits-events.json'))
for (let a = 0; a < Object.keys(deposits).length; a++) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`${a}/${Object.keys(deposits).length}`);
const account = Object.keys(deposits)[a];
const calls = Object.keys(deposits[account]).reduce((acc, token) => {
return acc.concat(
Object.keys(deposits[account][token]).map((season) => {
return beanstalk.methods.getDeposit(account, token, season).encodeABI();
})
);
}, []);
const encodedFarmCall = beanstalk.methods.farm(calls).encodeABI();
const rawReturnValues = await web3.eth.call(
{
to: beanstalk.options.address,
data: encodedFarmCall,
},
blockNumber
);
const farmOutputTypes = ['uint256', 'uint256'];
const rawReturnValuesArray = web3.eth.abi.decodeParameters(['bytes[]'], rawReturnValues)[0];
const returnValues = rawReturnValuesArray.map((r) => web3.eth.abi.decodeParameters(farmOutputTypes, r));
Object.keys(deposits[account]).forEach((token) => {
Object.keys(deposits[account][token]).forEach((season) => {
const r = returnValues.shift();
deposits[account][token][season].amount = r[0];
deposits[account][token][season].bdv = r[1];
});
});
if (deposits[account].length === 0) {
deposits[account] = {};
console.log('found an account with zero deposits', account);
}
// console.log('deposits for account: ', account, deposits[account]);
}
await fs.writeFileSync(`./data/deposits-onchain.json`, JSON.stringify(deposits, null, 4));
}
exports.getDepositsOnChain = getDeposits