-
Notifications
You must be signed in to change notification settings - Fork 8
/
triggerProcessing.js
executable file
·266 lines (239 loc) · 8.67 KB
/
triggerProcessing.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env node
/*
* Utility that subscribes to Thresher contract events on the chain
* and calls processAll() if a bunch of blocks go by with entries
* waiting to win/lose.
*
*/
/*
* Before running, create a .env configuration file; for example:
BLOCKS_TO_WAIT=3
WALLET_SEED=brother arrow ...
INFURA_PROJECT=abcdef...
TRACE=1
* ... and send some ETH to the first address associated with the HD wallet WALLET_SEED
*/
const assert = require('assert');
const bip39 = require('bip39');
const BN = require('bn');
const HDKey = require('hdkey')
const Web3 = require('web3');
const { toWei, fromWei } = require('web3-utils');
const yargs = require('yargs');
require('dotenv').config();
let web3;
let accounts;
let thresher;
let entries = []; // Array of { blockNumber, depositor } objects
function addEntry(event) {
entries.push({blockNumber: event.blockNumber, depositor: event.returnValues.depositor});
}
function removeEntry(event) {
const i = entries.findIndex( (e) => e.depositor == event.returnValues.depositor );
if (i != -1 ) {
entries.splice(i, 1);
}
}
let processing = false; // True when waiting for processAll() to confirm
let mightNeedToppingUp = false; // True after a Win, when we should check Thresher balance
function processAll() {
processing = true
thresher.methods.processAll().send({gas: 500*1000})
.on('transactionHash', function(hash) {
})
.on('confirmation', function(confirmationNumber, receipt) {
// Allow another processAll when confirmed:
if (confirmationNumber == 2) {
processing = false;
}
})
.on('receipt', function(receipt) {
})
.on('error', function(error, receipt) {
console.log("processAll() FAILED: "+error);
process.exit(1);
})
}
let keepAlive = 0;
function topUpBalance() {
const fromAddress = thresher.options.from;
return Promise.all([ thresher.methods.maxPayout().call(),
web3.eth.getBalance(thresher.options.address),
web3.eth.getBalance(fromAddress)
])
.then(function(v) {
const maxPay = new web3.utils.BN(v[0]);
const thresherBalance = new web3.utils.BN(v[1]);
const fromBalance = new web3.utils.BN(v[2]);
const _gas = new web3.utils.BN(100*1000);
if (thresherBalance.lt(maxPay)) {
const minTopUp = new web3.utils.BN(web3.utils.toWei('0.2', 'ether'));
const topUpAmount = web3.utils.BN.max(maxPay.sub(thresherBalance), minTopUp);
if (fromBalance.lt(topUpAmount.add(_gas))) {
throw new Error(`Unable to top-up contract balance, ${fromAddress} balance: ${web3.utils.fromWei(fromBalance)}`);
}
return thresher.methods.increaseBalance().send({from: fromAddress, value: topUpAmount, gas: _gas})
.then(function() {
if (process.env.TRACE) {
console.log(`increaseBalance(${web3.utils.fromWei(topUpAmount)} ETH)`);
}
})
}
})
}
function newBlock(error, event) {
if (error) {
console.log(`Error with new block subscription: ${error}`);
process.exit(1);
}
// Keep the websocket connection alive by
// sending a getPeerCount every once in a while:
keepAlive += 1;
if (keepAlive > 30) {
keepAlive = 0;
web3.eth.net.getPeerCount()
.then( (n) => (n == 0 || console.log(`WARNING: NO PEERS`)));
}
if (processing || event.number === null) {
return;
}
// If topUpBalance() generates a transaction, we need
// it to resolve before calling processAll() generates another
// transaction. So:
let p;
if (mightNeedToppingUp) {
mightNeedToppingUp = false;
p = topUpBalance();
} else {
p = Promise.resolve();
}
p.then(function() {
let n = 0;
for (let e of entries) {
let waitUntil = e.blockNumber+Number(process.env.BLOCKS_TO_WAIT);
if (waitUntil <= event.number) {
n = n+1;
}
}
if (n == 0) {
return;
}
if (process.env.TRACE) {
console.log(`${n} entries waiting, calling processAll()`);
}
processAll();
})
.catch(console.log)
}
async function init(argv) {
web3 = new Web3()
if (argv.network == 'development') {
const eventProvider = new Web3.providers.WebsocketProvider('ws://localhost:9545');
web3.setProvider(eventProvider)
} else {
const eventProvider = new Web3.providers.WebsocketProvider(
`wss://${argv.network}.infura.io/ws/v3/${process.env.INFURA_PROJECT_ID}`);
eventProvider.on("connect", () => {
console.log("*** WebSocket Connected ***")
})
eventProvider.on("error", (e) => {
console.log("*** WebSocket Error ***")
process.exit(1);
})
eventProvider.on("end", (e) => {
console.log("*** WebSocket Ended ***")
process.exit(1);
})
eventProvider.on("close", (e) => {
console.log("*** WebSocket Closed ***")
process.exit(1);
})
eventProvider.on("timeout", (e) => {
console.log("*** WebSocket Timeout ***")
process.exit(1);
})
eventProvider.on("exit", (e) => {
console.log("*** WebSocket Exit ***")
process.exit(1);
})
eventProvider.on("ready", (e) => {
//console.log('*** WebSocket Ready ***')
})
web3.setProvider(eventProvider)
}
// I spent a lot of time trying to get truffle's HDWalletProvider to play nicely
// with WebsocketProvider, but failed. Instead, derive the first key from the
// seed phrase:
const masterPrivateKey = bip39.mnemonicToSeedSync(process.env.WALLET_SEED);
const hdfirst = HDKey.fromMasterSeed(masterPrivateKey).derive("m/44'/60'/0'/0/0");
// ... and tell web3 to use it to do stuff:
const account = web3.eth.accounts.privateKeyToAccount('0x' + hdfirst.privateKey.toString('hex'));
web3.eth.accounts.wallet.add(account);
web3.eth.defaultAccount = account.address;
// TODO: Looks like the next version of truffle might Do the Right Thing:
// https://github.com/trufflesuite/truffle/pull/2582
// ... which might let us specify the websocket provider in truffle-config.js,
// require truffle-config.js in this file and use the same provider for both
// truffle and this utility.
const balance = new web3.utils.BN(await web3.eth.getBalance(account.address));
if (balance.lt(new web3.utils.BN(toWei('0.01', 'ether')))) {
console.log(`${account.address} balance ${web3.utils.fromWei(balance)}; send it at least 0.01 ETH`)
process.exit(1);
}
console.log(`Sending transactions (paying gas) from ${account.address} (balance: ${web3.utils.fromWei(balance)} ETH)`);
const contractJson = require('./build/contracts/Thresher.json');
const netId = await web3.eth.net.getId();
if (contractJson.networks[netId]) {
const tx = await web3.eth.getTransaction(contractJson.networks[netId].transactionHash);
thresher = new web3.eth.Contract(contractJson.abi, contractJson.networks[netId].address);
thresher.options.from = account.address;
} else if (argv.thresherAddress) {
thresher = new web3.eth.Contract(contractJson.abi, argv.thresherAddress);
thresher.options.from = account.address;
} else {
console.log("Don't know where the contract is deployed on this network");
process.exit(1);
}
let currentBlock = await web3.eth.getBlockNumber();
let b = (currentBlock > 256 ? currentBlock-256 : 1);
thresher.events.Contribute({
fromBlock: b,
toBlock: 'latest'
})
.on('data', addEntry)
.on('changed', removeEntry)
thresher.events.Win({
fromBlock: b,
toBlock: 'latest'
})
.on('data', (event) => { removeEntry(event); mightNeedToppingUp = true;})
.on('changed', addEntry)
thresher.events.Lose({
fromBlock: b,
toBlock: 'latest'
})
.on('data', removeEntry)
.on('changed', addEntry)
thresher.events.TransferError({
fromBlock: b,
toBlock: 'latest'
})
.on('data', removeEntry)
.on('changed', addEntry)
web3.eth.subscribe('newBlockHeaders', newBlock);
}
const argv = yargs
.option('network', {
default: 'development',
describe: 'a network defined in truffle-config.js',
type: 'string',
})
.option('thresherAddress', {
default: null,
describe: 'Address of Thresher contract',
type: 'string',
})
.help()
.alias('help', 'h')
.argv;
init(argv);