-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
444 lines (366 loc) · 15.1 KB
/
app.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
local Parity node:
* update Parity (see: https://wiki.parity.io/Setup#one-line-binary-installer ) :
* bash <(curl https://get.parity.io -L)
* curl --data '{"method":"parity_chainStatus","params":[],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST localhost:8545
* parity --min-peers=50 --cache-size=8192 --chain=ropsten
* geth attach ~/.local/share/io.parity.ethereum/jsonrpc.ipc
* */
'use strict';
const appVersion = "1.1.0";
/* ------ Initialize required packages ---------------- */
// https://log4js-node.github.io/log4js-node/
// https://github.com/log4js-node/log4js-node
let log4js = require('log4js');
// https://www.npmjs.com/package/jsonfile
const jsonfile = require('jsonfile');
// https://github.com/axios/axios
const axios = require('axios');
// https://github.com/ethereumjs/ethereumjs-tx
// const Tx = require('ethereumjs-tx');
const Tx = require('ethereumjs-tx').Transaction;
// https://web3js.readthedocs.io/en/v1.2.1/web3.html#id4
const Web3 = require('web3');
/* -------- Set up logs ---------------------------------*/
log4js.configure({
appenders: {
everything: {
// type: 'file',
type: 'dateFile', // see: https://log4js-node.github.io/log4js-node/dateFile.html
filename: './logs/all-the-logs.log',
// pattern: '.yyyy-MM-dd-hh', // new file every hour
// compress: true, // store in .gz file
},
// see: https://log4js-node.github.io/log4js-node/faq.html
errorsAppender: {
type: 'dateFile',
filename: './logs/errors.log',
},
errors: {
type: 'logLevelFilter',
appender: 'errorsAppender',
level: 'error'
},
out: {
type: 'stdout'
} // > https://log4js-node.github.io/log4js-node/stdout.html
},
categories: {
default: {
appenders: ['everything', 'errors', 'out'],
level: 'debug' // < 'debug'
}
}
});
const logger = log4js.getLogger();
/*
// check logger output:
logger.trace('Entering cheese testing');
logger.debug('Got cheese.');
logger.info('Cheese is Comté.');
logger.warn('Cheese is quite smelly.');
logger.error('Cheese is too ripe!');
logger.fatal('Cheese was breeding ground for listeria.');
// check on https://regex101.com/
const regexForLog = new RegExp('^(\\[)(\\d\\d\\d\\d\-\\d\\d-\\d\\d)(T)(\\d\\d:\\d\\d:\\d\\d\\.\\d\\d\\d)(]\\s\\[)([A-Z]+)(]\\s)([a-z]+)(\\s-\\s)(.+)$');
*/
/* ------- Initialize Web3js -------------------------- */
// see: ./config.Example.json
const config = jsonfile.readFileSync("./config.json");
const networkId = "3"; // TODO: Ropsten
/* Infura: */
// const infuraWebsocket = new Web3.providers.WebsocketProvider('wss://' + config.infuraHost[networkId] + '/ws/v3/' + config.infuraProjectId);
// const web3 = new Web3(infuraWebsocket);
const infuraHttp = new Web3.providers.HttpProvider('https://' + config.infuraHost[networkId] + '/v3/' + config.infuraProjectId);
const web3 = new Web3(infuraHttp);
/* Local: */
// const net = require('net');
// const os = require('os');
// const localHttp = new Web3.providers.HttpProvider('http://localhost:8545');
// const localWebsocket = new Web3.providers.WebsocketProvider('ws://localhost:8546');
// const localParityIPC = new Web3.providers.IpcProvider(os.homedir() + '/.local/share/io.parity.ethereum/jsonrpc.ipc', net);
const privateKey = Buffer.from(config.botAccountPrivateKey, 'hex');
const botAddress = config.botAccountAddress;
if (!web3.utils.isAddress(botAddress)) {
logger.error("Address for bot", botAddress, "is not a valid Ethereum address");
}
// nonce
// we also calculate nonce locally to prevent failing transactions when Infura provides nonce already used
let txCounterLocal = 0; // < should be a number
logger.debug("======== App ver. " + appVersion + " started ========");
logger.debug("NodeJS version: ", process.version);
logger.debug("Web3.version:", web3.version, ", web3 current provider:", web3.currentProvider.constructor.name);
logger.debug("Network:", config.networkName[networkId]);
let factoryContract;
const factoryContractData = {
address: config.contractAddress[networkId],
};
let cryptosharesContractsAbi;
// Interval to check smart contract state.
// This should be big enough to make sure all transactions sent will be mined before we start the next check.
const intervalInSeconds = 120;
const intervalInMilliseconds = intervalInSeconds * 1000;
// Commission bot will take from the fee set for tx.
// The rest will be used for gas.
const botCommission = 0.1;
// This is the coefficient we use to calculate tx fee for transaction.
const txFeeFactor = 1 - botCommission;
// Minimal gas price at which the transaction makes sense.
const minGasPriceForTxInWei = 5000000000; // 5 Gwei
// const minGasPriceForTxInWei = 1; // for test nets
web3.eth.net.isListening() // here web3 should establish a connection
.then((result) => {
logger.debug("Web3 is connected: " + web3.currentProvider.connected + ". Node is listening:", result);
return web3.eth.getBalance(botAddress);
})
.then((result) => {
logger.debug("Bot address:", botAddress);
logger.debug(
"Current balance: ",
result,
"Wei",
"(" + web3.utils.fromWei(result) + " ETH)"
);
// get contract ABI from etherscan
// (works if smart contract code was verified on etherscan)
return axios.get(
config.etherscanApiLink[networkId]
+ "api?module=contract&action=getabi&address="
+ factoryContractData.address + "&apikey"
+ config.etherscanApiKeyToken
);
})
.then((result) => {
if (result.data.status === "0") {
throw result.data.result;
}
factoryContractData.abi = JSON.parse(result.data.result);
// https://web3js.readthedocs.io/en/v1.2.1/web3-eth-contract.html#new-contract
factoryContract = new web3.eth.Contract(
factoryContractData.abi,
factoryContractData.address
);
logger.debug("Factory contract:", factoryContract._address);
logger.debug("Will check factory ledger every " + intervalInSeconds + " seconds");
setInterval(
main,
intervalInMilliseconds
);
})
.catch((error) => {
logger.error(error);
});
function main() {
// number of contracts deployed by the factory contract
let cryptosharesContractsCounter;
factoryContract.methods.cryptoSharesContractsCounter().call()
.then((result) => {
result = parseInt(result);
if (result > 0) {
cryptosharesContractsCounter = result;
if (!cryptosharesContractsAbi) {
// get data of the first cryptoshares contract from the ledger:
return factoryContract.methods.cryptoSharesContractsLedger(1).call();
} else {
contractsLoop(cryptosharesContractsCounter);
}
}
})
.then((result) => {
if (result) {
// Get cryptoshares contract ABI (the same for all contracts deployed from the factory contract) from
// the etherscan.io This works if cryptoshares smart contract code was verified on etherscan.
return axios.get(
config.etherscanApiLink[networkId]
+ "api?module=contract&action=getabi&address="
+ result.contractAddress + "&apikey"
+ config.etherscanApiKeyToken
);
}
})
.then((result) => {
if (result) {
cryptosharesContractsAbi = JSON.parse(result.data.result);
contractsLoop(cryptosharesContractsCounter);
}
})
.catch((error) => {
logger.error("main() ERROR");
logger.error(error);
});
}
function contractsLoop(cryptoSharesContractsCounter) {
logger.debug(cryptoSharesContractsCounter + " contracts on the ledger");
// run payDividends() func for each cryptoshares contract already deployed
for (let i = 1; i <= cryptoSharesContractsCounter; i++) {
payDividends(i);
}
}
function payDividends(contractNumber) {
// contract instance (web3)
let contract;
let dividendsRoundNumber;
factoryContract.methods.cryptoSharesContractsLedger(contractNumber.toString()).call()
.then((result) => {
contract = new web3.eth.Contract(
cryptosharesContractsAbi,
result.contractAddress
);
return contract.methods.dividendsRoundsCounter().call();
})
.then((result) => {
dividendsRoundNumber = parseInt(result);
if (dividendsRoundNumber > 0) {
return contract.methods.dividendsRound(result).call();
} else {
logger.debug(
"Contract #",
contractNumber,
"(" + contract._address + ")",
"round counter: ",
dividendsRoundNumber
);
}
})
.then((result) => {
if (result && dividendsRoundNumber > 0) {
// logger.debug("Contract", contract._address, "Round #", dividendsRoundsCounter);
// logger.debug(result);
if (result.roundIsRunning) {
result.allRegisteredShareholders = parseInt(result.allRegisteredShareholders);
result.shareholdersCounter = parseInt(result.shareholdersCounter);
result.weiForTxFees = parseInt(result.weiForTxFees);
const numberOfTransactionsToSend
= result.allRegisteredShareholders - result.shareholdersCounter;
const feeForTxInWei
= Math.floor(
(result.weiForTxFees / result.allRegisteredShareholders) * txFeeFactor
);
logger.debug(
"Contract #",
contractNumber, "(" + contract._address + ")",
"running round :",
dividendsRoundNumber,
", number of tx to send:",
numberOfTransactionsToSend,
", fee for tx:",
web3.utils.fromWei(feeForTxInWei.toString()),
"ETH",
"(" + feeForTxInWei + ") Wei"
);
if (numberOfTransactionsToSend > 0 && feeForTxInWei > 0) {
for (let j = 1; j <= numberOfTransactionsToSend; j++) {
payDividendsToNext(contract._address, contractNumber, feeForTxInWei);
}
} else {
logger.debug(
"Fee for tx in contract #",
contractNumber,
"is to small to send transactions"
);
}
} else {
logger.debug(
"Contract #",
contractNumber, "(" + contract._address + ")",
"last round : ",
dividendsRoundNumber
);
}
}
})
.catch((error) => {
logger.error("payDividends to contractNumber #", contractNumber, "ERROR:");
logger.error(error);
});
}
function payDividendsToNext(contractAddress, contractNumber, feeForTxInWei) {
if (!cryptosharesContractsAbi) {
logger.error("cryptosharesContractsAbi contract ABI is not defined");
return;
}
const contract = new web3.eth.Contract(
cryptosharesContractsAbi,
contractAddress
);
let gasPrice = 0;
let gasEstimate;
let botBalance;
const payDividendsToNextTxObject = contract.methods.payDividendsToNext();
web3.eth.getBalance(botAddress)
.then((result) => {
logger.debug(
"Current balance: ",
result,
"Wei",
"(" + web3.utils.fromWei(result) + ") ETH"
);
botBalance = parseInt(result);
return payDividendsToNextTxObject.estimateGas();
})
.then((result) => {
gasEstimate = parseInt(result);
gasPrice = Math.floor(feeForTxInWei / gasEstimate);
if (gasPrice > minGasPriceForTxInWei && (gasPrice * gasEstimate) <= botBalance) {
return web3.eth.getTransactionCount(botAddress);
} else if ((gasPrice * gasEstimate) > botBalance) {
logger.error(
"Not enough ETH for tx. Current balance:",
botBalance,
"Wei needed:",
(gasPrice * gasEstimate)
)
}
})
.then((result) => {
if (result) {
let nonceFromInfura = parseInt(result);
logger.debug("Nonce from Infura:", nonceFromInfura, "(", result, "); local nonce:", txCounterLocal);
// ensure that local nonce is not less than nonce from Infura
if (txCounterLocal < nonceFromInfura) {
txCounterLocal = nonceFromInfura;
}
const txData = {
nonce: web3.utils.toHex(txCounterLocal),
gasLimit: web3.utils.toHex(gasEstimate),
// gasPrice: web3.utils.toHex(10e9), // 10 Gwei
gasPrice: web3.utils.toHex(gasPrice),
to: contract._address,
from: botAddress,
// value: web3.utils.toHex(web3.utils.toWei('123', 'wei'))
data: payDividendsToNextTxObject.encodeABI()
};
// see:
// https://web3js.readthedocs.io/en/v1.2.1/web3-eth.html#id67
// // {'chain': 'ropsten'}
let opts;
if (networkId !== "1") {
opts = {};
opts.chain = config.networkName[networkId].toLowerCase();
}
const transaction = new Tx(txData, opts);
transaction.sign(privateKey);
const serializedTx = transaction.serialize();
const rawTx = '0x' + serializedTx.toString('hex');
txCounterLocal++;
return web3.eth.sendSignedTransaction(rawTx);
}
})
.then((result) => {
if (result) {
// console.log("tx receipt:");
// console.log(result);
logger.info(
"Tx sent to contract #",
contractNumber,
", tx hash:",
result.transactionHash
);
// logger.info(result);
}
})
.catch((error) => {
logger.error("payDividendsToNext (contract #", contractNumber, ") ERROR:");
logger.error(error);
});
}