-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollector.js
174 lines (162 loc) · 5.87 KB
/
collector.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
const Web3 = require("web3");
var fs = require("fs");
const { operator, url } = require("./scriptConfig.js");
const depositAddress = "0xCffDCB12b74bE900e2020B9D96D256F1fEA96342";
const depositFactoryAddress = "0x87EFFeF56C7fF13E2463b5d4dCE81bE2340FAf8b";
const keepFactoryAddress = "0xA7d9E842EFB252389d613dA88EDa3731512e40bD";
const feeRebateTokenAddress = "0xaf3fFF06b75f99352d8C2a3C4beF1339a2f94789";
const TDTAddress = "0x10B66Bd1e3b5a936B7f8Dbc5976004311037Cdf0";
const vendingMachineAddress = "0x526c08E5532A9308b3fb33b7968eF78a5005d2AC";
const { setupLoader } = require("@openzeppelin/contract-loader");
const states = [
"START",
"AWAITING_SIGNER_SETUP",
"AWAITING_BTC_FUNDING_PROOF",
"FAILED_SETUP",
"ACTIVE",
"AWAITING_WITHDRAWAL_SIGNATURE",
"AWAITING_WITHDRAWAL_PROOF",
"REDEEMED",
"COURTESY_CALL",
"FRAUD_LIQUIDATION_IN_PROGRESS",
"LIQUIDATION_IN_PROGRESS",
"LIQUIDATED",
];
var dir = "./data";
function time() {
return new Date().toUTCString() + ":";
}
function wait(ms) {
var start = new Date().getTime();
var end = start;
while (end < start + ms) {
end = new Date().getTime();
}
}
async function populate() {
let start
if (process.argv.length === 2) {
start = 10867766
console.log("No start block provided, defaulting to 10867766 (launch day!)");
}
else{
start = Number(process.argv[2])
}
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
let nodeData = {};
const web3 = new Web3(url);
let END_BLOCK;
let BN = await web3.eth.getBlockNumber();
let remaining
for (
let START_BLOCK = start ;
START_BLOCK <= BN - 1000;
START_BLOCK += 1000
) {
// wait becuase rate limits :s
wait(500);
END_BLOCK = START_BLOCK + 1000;
console.log(`checking interval ${START_BLOCK} - ${END_BLOCK}` )
const loader = setupLoader({ provider: web3 }).web3;
const depositFactory = loader.fromArtifact(
"DepositFactory",
depositFactoryAddress
);
const ecdsaFactory = loader.fromArtifact(
"BondedECDSAKeepFactory",
keepFactoryAddress
);
const frt = loader.fromArtifact("FeeRebateToken", feeRebateTokenAddress);
const tdt = loader.fromArtifact("TBTCDepositToken", TDTAddress);
let log =`Gathering data from block range:${START_BLOCK} - ${END_BLOCK}\n`
fs.appendFileSync("data/logs.txt", `${time()} ${log}`);
fs.writeFileSync("data/blockUpdated.txt", END_BLOCK);
// Get all events emitted by BondedECDSAKeepFactory in the interval provided
let ecdsaevents = await ecdsaFactory.getPastEvents(
"BondedECDSAKeepCreated",
{
fromBlock: START_BLOCK,
toBlock: END_BLOCK, // You can also specify 'latest'
}
);
let keepAddress;
let blockNum;
let txHash;
let cloneAddress;
let TDTOwner;
let FRTExists;
let TDTredeemable = false;
let state;
let lotSize;
let events;
// itterate over the ECDSA events for the interval
for (let i = 0; i < ecdsaevents.length; i++) {
// get array ov members for the keep
let vls = ecdsaevents[i].returnValues["1"];
for (let q = 0; q < vls.length; q++) {
// if the provided operator is part of the list, look for the DepositCloneCreated
// event in the samt transaction (ensured by comparing transaction hashes) in order to get
// the Deposit address.
// We need the Deposit address to get and interact with the deposit contract instance
// The deposit address is also the TDT and FRT token ID.
if (vls[q] == operator) {
wait(700);
txHash = ecdsaevents[i].transactionHash;
blockNum = ecdsaevents[i].blockNumber;
keepAddress = ecdsaevents[i].returnValues["keepAddress"];
console.log(`opperator selected for keep ${keepAddress}`);
events = await depositFactory.getPastEvents("DepositCloneCreated", {
fromBlock: blockNum,
toBlock: blockNum,
});
for (let i = 0; i < events.length; i++) {
if (events[i].transactionHash == txHash) {
cloneAddress = events[i].returnValues["0"];
}
}
// create the deposit instance and get the State and lotSize of the Deposit
const deposit = loader.fromArtifact("Deposit", cloneAddress);
lotSize = await deposit.methods.lotSizeSatoshis().call();
state = await deposit.methods.currentState().call();
collateralizationRate = await deposit.methods.collateralizationPercentage().call()
// If the FRT does not exist, the TDT has never been used to mint TBTC, and the deposit is not redeemable
FRTExists = await frt.methods.exists(cloneAddress).call();
if (FRTExists) {
TDTOwner = await tdt.methods.ownerOf(cloneAddress).call();
// if the TDT owner is not the VendingMAchine, the Deposit is not redeemable
if (TDTOwner == vendingMachineAddress) {
TDTredeemable = true;
}
}
// Handle writing to Data folder...
let KeepData = {
keepAddress: keepAddress,
blockNum: blockNum,
cloneAddress: cloneAddress,
lotSize: lotSize,
state: states[state],
FRTExists: FRTExists,
redeemable: TDTredeemable,
TDTOwner: TDTOwner,
collateralizationRate: collateralizationRate,
};
var count = Object.keys(nodeData).length;
nodeData[count.toString()] = KeepData;
let logData = JSON.stringify(KeepData);
fs.appendFileSync("data/logs.txt", `${time()} ${logData}\n`);
}
}
}
if (events == undefined || events.length == 0) {
fs.appendFileSync(
"data/logs.txt",
`${time()} No keeps with member ${operator} in specified period\n`
);
}
}
let data = JSON.stringify(nodeData);
fs.writeFileSync("data/nodeData.json", data);
}
populate();