-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (73 loc) · 2.62 KB
/
index.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
require('dotenv').config();
const fs = require('fs');
const Eth = require('web3-eth');
const Utils = require('web3-utils');
const eth = new Eth(process.env.WEB3_PROVIDER);
const DEPOSIT_MADE_TOPIC = Utils.sha3('DepositMade(address,uint256,uint256)');
const BUMP_PURCHASED_TOPIC = Utils.sha3('BumpPurchased(address,uint256,uint256)');
const REWARD_ISSUED_TOPIC = Utils.sha3('RewardIssued(address,uint256,uint256)');
function getEvents(topic) {
return eth.getPastLogs({
address: process.env.BUMP_MARKET_CONTRACT,
topics: [topic],
fromBlock: process.env.FROM_BLOCK,
toBlock: process.env.TO_BLOCK || 'latest'
})
}
function getChecksumAddress(paddedAddress) {
return Utils.toChecksumAddress('0x' + paddedAddress.substr(26));
}
function parseDepositData(data) {
const numberOne = data.substr(0, 66);
const numberTwo = data.substr(66);
return [
Utils.fromWei(Utils.hexToNumberString(numberOne), 'mwei'), // USDC has 6 decimal points, hence use 'mwei' as unit
Utils.hexToNumber('0x' + numberTwo)
];
}
function parseBumpTokenData(data) {
const numberOne = data.substr(0, 66);
const numberTwo = data.substr(66);
return [
Utils.fromWei(Utils.hexToNumberString(numberOne)),
Utils.hexToNumber('0x' + numberTwo) / 10000
];
}
function generateCSVFile(deposits, purchases, rewards) {
const outputFile = process.env.OUTPUT_FILE;
const headers = [
'Block Number',
'Depositor Address',
'Deposit Amount',
'Interest Rate',
'Purchased Bump Tokens',
'Bump Token Price',
'Rewarded Bump Tokens',
'Transaction Hash',
];
let content = headers.join(',');
for(let i = 0; i < deposits.length; i++) {
let dataItems = [];
dataItems.push(deposits[i].blockNumber);
dataItems.push(getChecksumAddress(deposits[i].topics[1]));
parseDepositData(deposits[i].data).forEach(n => dataItems.push(n));
parseBumpTokenData(purchases[i].data).forEach(n => dataItems.push(n));
dataItems.push(parseBumpTokenData(rewards[i].data)[0]);
dataItems.push(rewards[i].transactionHash);
content = content.concat('\n' + dataItems.join(','));
}
fs.writeFile(outputFile, content, err => {
if (err) {
console.log('Error writing to csv file', err);
} else {
console.log(`saved as ${outputFile}`);
}
});
}
Promise.all([
getEvents(DEPOSIT_MADE_TOPIC),
getEvents(BUMP_PURCHASED_TOPIC),
getEvents(REWARD_ISSUED_TOPIC),
]).then(([deposits, purchases, rewards]) => {
generateCSVFile(deposits, purchases, rewards);
});