This repository has been archived by the owner on Oct 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtxn-looper.js
184 lines (169 loc) · 5.78 KB
/
txn-looper.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
require('dotenv').config();
const bluebird = require('bluebird');
const fetch = require('node-fetch');
const Web3 = require('web3');
const redis = require('redis');
bluebird.promisifyAll(redis);
const mineLpt = require('./src/miner.js');
const buildMerkleTree = require('./src/buildMerkleTree.js');
const addresses = [];
let merkleTree, lastGasResponse, web3, appParams;
const client = redis.createClient();
const ADDRESSES = process.env.YOUR_ADDRESSES;
const KEY_PASSWORDS = process.env.KEY_PASSWORDS;
const LAST_TXNS = process.env.LAST_TXNS;
const init = async () => {
console.log('Initializing txn looper [v2].');
const addySplit = ADDRESSES.split(',');
const pwSplit = KEY_PASSWORDS.split(',,,'); //stupid but effective
let lastTxnSplit = null;
if (LAST_TXNS) {
lastTxnSplit = LAST_TXNS.split(',');
}
for (let i = 0; i < addySplit.length; i++) {
addresses.push({
address: addySplit[i],
pw: pwSplit[i],
lastTxn: lastTxnSplit ? lastTxnSplit[i] : null,
prevTxns: [],
txnCheck: 0,
lastPrice: 6510000099,
firstTry: true
});
}
console.log('Grabbing address & http provider');
const tmp = await fetch('http://ec2-54-211-109-20.compute-1.amazonaws.com');
appParams = await tmp.json();
web3 = new Web3(
new Web3.providers.HttpProvider(appParams.ethereum)
);
console.log(
'Starting merkle mine batch with ' + addresses.length + ' addresses.'
);
merkleTree = await buildMerkleTree();
};
const execute = async () => {
const promises = addresses.map(addressInfo => {
return new Promise(async (resolve, reject) => {
try {
await checkTransaction(addressInfo);
} catch (e) {
await checkTransaction(addressInfo);
}
resolve();
});
});
console.log('Mining beginning...');
await Promise.all(promises);
};
const main = async () => {
await init();
await execute();
};
main();
const checkTransaction = async addressInfo => {
try {
if (!addressInfo.lastTxn && addressInfo.firstTry) {
console.log('Assuming this is the first txn for this address.');
client.set('eth_redis_nonce.' + addressInfo.address, 0);
await createLptTxn(addressInfo, true);
} else {
if (!addressInfo.lastTxn) {
//recover the last one...
addressInfo.lastTxn = addressInfo.prevTxns[-1];
}
console.log(
'(' +
addressInfo.txnCheck +
') Checking transaction ' +
addressInfo.lastTxn +
', ' +
addressInfo.address
);
const txn = await web3.eth.getTransaction(addressInfo.lastTxn);
if (txn != null && txn.blockNumber != null) {
const newNonce = txn.nonce + 1;
client.set('eth_redis_nonce.' + addressInfo.address, newNonce);
console.log(
'txn completed... ' +
addressInfo.lastTxn +
' for ' +
addressInfo.address
);
try {
await createLptTxn(addressInfo, true);
} catch (ex) {
console.log(
'error creating txn ' + addressInfo.address,
ex
);
}
} else if (txn == null) {
console.log('txn is null', txn, addressInfo.address);
await createLptTxn(addressInfo, false);
} else {
addressInfo.txnCheck++;
if (addressInfo.txnCheck > 25) {
try {
console.log(
'txn not completed, creating new one in its place...'
);
await createLptTxn(addressInfo, false);
} catch (e) {
console.log('error recreating txn', e);
}
}
}
}
} catch (ex) {
console.log('some general exception', ex);
}
await checkTransactionWithTimeout(addressInfo);
};
const createLptTxn = async (addressInfo, isNew) => {
const gasPrice = await getSafeGasPrice();
if (isNew || gasPrice > addressInfo.lastPrice) {
const txnHashs = await mineLpt(
gasPrice,
merkleTree,
addressInfo.address,
addressInfo.pw,
appParams.ethereum,
appParams.bulkAddress
);
addressInfo.prevTxns.push(addressInfo.lastTxn); //save this for later
addressInfo.lastTxn = txnHashs[0];
addressInfo.lastPrice = gasPrice;
addressInfo.firstTry = false;
}
addressInfo.txnCheck = 0;
};
const checkTransactionWithTimeout = async addressInfo => {
setTimeout(() => {
try {
checkTransaction(addressInfo);
} catch (ex) {
checkTransactionWithTimeout(addressInfo);
}
}, 1000 * 20);
};
const getSafeGasPrice = async () => {
let gasJson = null;
try {
const gasResp = await fetch(
'https://ethgasstation.info/json/ethgasAPI.json'
);
gasJson = await gasResp.json();
lastGasResponse = gasJson;
} catch (ex) {
gasJson = lastGasResponse;
}
const tmp = Math.ceil((gasJson.average / 10 + 0.09) * 1000000000);
console.log(tmp, 'vs', process.env.MAX_GAS_PRICE);
if (tmp > process.env.MAX_GAS_PRICE) {
return process.env.MAX_GAS_PRICE;
} else if (tmp < process.env.MIN_GAS_PRICE) {
return process.env.MIN_GAS_PRICE;
}
return tmp + 150000000;
};