-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
278 lines (249 loc) · 9.14 KB
/
index.js
File metadata and controls
278 lines (249 loc) · 9.14 KB
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
require("dotenv").config();
const { initializeDatabase } = require("./lib/setup");
const fetch = (...args) =>
import("node-fetch").then(({ default: fetch }) => fetch(...args));
const WALLET_COUNT = parseInt(process.env.WALLET_COUNT);
const FAUCET_URL = process.env.FAUCET_URL;
const FAUCET_USERNAME = process.env.FAUCET_USERNAME;
const FAUCET_PASSWORD = process.env.FAUCET_PASSWORD;
const FUND_AMOUNT = parseInt(process.env.FUND_AMOUNT);
const MINIMUM_BALANCE = parseInt(process.env.MINIMUM_BALANCE);
const TRANSFER_AMOUNT_LIMIT = parseFloat(process.env.TRANSFER_AMOUNT_LIMIT);
const NO_OF_TXN = parseInt(process.env.NO_OF_TXN);
const TIME_OUT = parseInt(process.env.TIME_OUT);
const TXNS_PER_MIN = parseInt(process.env.TXNS_PER_MIN);
function getRandomValue() {
return (
((Math.random() * MINIMUM_BALANCE) + TRANSFER_AMOUNT_LIMIT).toFixed(8)
);
}
class TxnFloader {
wallets = [];
total_allowed_txns;
lastTime;
constructor() {
this.total_allowed_txns = TXNS_PER_MIN;
this.lastTime = new Date();
console.log(`${new Date().toUTCString()} --- ⏲️ Timer updated, Txn Sent=${TXNS_PER_MIN - this.total_allowed_txns}`);
this.addOneMin();
}
async addOneMin() {
await new Promise((resolve) => setTimeout(resolve, 60 * 1000));
this.lastTime = new Date();
console.log(`${new Date().toUTCString()} --- ⏲️ Timer updated, Txn Sent=${TXNS_PER_MIN - this.total_allowed_txns}`);
this.total_allowed_txns = TXNS_PER_MIN;
this.addOneMin();
}
async callRpc(
method,
params,
wallet = null,
url = null,
uname = null,
pwd = null
) {
try {
let rpcUrl = url ? url : FAUCET_URL;
const username = uname ? uname : FAUCET_USERNAME;
const password = pwd ? pwd : FAUCET_PASSWORD;
if (wallet !== undefined && wallet !== null && wallet !== "") {
rpcUrl = rpcUrl + "wallet/" + wallet;
}
const body = JSON.stringify({
jsonrpc: "2.0",
method: method,
params: params,
id: 1,
});
const response = await fetch(rpcUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization:
"Basic " +
Buffer.from(username + ":" + password).toString("base64"),
},
body: body,
});
const result = await response.json();
console.log(`${new Date().toUTCString()} --- method=${method} result=${JSON.stringify(result)}`);
return result;
} catch (e) {
console.log(e);
}
}
async createAndFundWallets() {
this.wallets = await this.initiateWallets();
await new Promise((resolve) => setTimeout(resolve, TIME_OUT));
await this.fundWallets();
await new Promise((resolve) => setTimeout(resolve, TIME_OUT));
await this.floadTransactions();
await new Promise((resolve) => setTimeout(resolve, TIME_OUT));
}
async floadTransactions() {
for (let sender of this.wallets) {
for (let receiver of this.wallets) {
if (sender === receiver || !sender.startsWith('wallet') || !receiver.startsWith('wallet')) continue;
let i = 0;
let no_fund = 0;
while (i < NO_OF_TXN) {
const balances = await this.getWalletBalances(sender);
if (balances.mine) {
if (balances.mine.trusted < MINIMUM_BALANCE) {
console.log(
`${new Date().toUTCString()} --- Sender: ${sender} balance is less than MINIMUM_BALANCE QRN. Skipping transaction...`
);
await this.fund();
no_fund = 1;
break;
}
console.log(`${new Date().toUTCString()} --- Sender ${sender} balance: ${balances.mine.trusted}`);
const receiverAddress = await this.getWalletAddress(receiver);
console.log(`${new Date().toUTCString()} --- Receiver ${receiver} address: ${receiverAddress}`);
const amount = getRandomValue();
const fund_transfered = await this.transferFunds(
sender,
receiverAddress,
amount.toString()
);
if (fund_transfered !== undefined)
console.log(`${new Date().toUTCString()} --- ✅ txid: `, fund_transfered);
else console.log(`${new Date().toUTCString()} --- ❌ Transaction failed`);
await new Promise((resolve) => setTimeout(resolve, TIME_OUT));
}
i++;
}
if (no_fund == 1) break;
}
}
}
async fundWallets() {
for (let wallet of this.wallets) {
const balance = await this.getWalletBalance(wallet);
console.log(`${new Date().toUTCString()} --- Wallet: ${wallet}, Balance: ${balance}`);
if (balance < MINIMUM_BALANCE) {
console.log(`${new Date().toUTCString()} --- Funding wallet: ${wallet}`);
const address = await this.getWalletAddress(wallet);
console.log(`${new Date().toUTCString()} --- Generated address: ${address}`);
console.log(await this.fundWalletAddress(address, FUND_AMOUNT));
}
}
}
async fund(wallet) {
const balances = await this.getWalletBalances(wallet);
if (balances.mine) {
if (!(balances.mine.trusted < MINIMUM_BALANCE || balances.mine.untrusted_pending < MINIMUM_BALANCE || balances.mine.immature < MINIMUM_BALANCE)) {
console.error(
`💰💸 Funding ${wallet} due to insufficient funds. ⚠️ Balance: ${balances.mine.trusted}`
);
if (balances.mine.untrusted_pending === 0) {
const address = await this.getWalletAddress(wallet);
await this.transferFunds(address, FUND_AMOUNT);
} else {
console.log(
`${new Date().toUTCString()} --- ${wallet}: waiting for ${balances.mine.untrusted_pending} QRN to be confirmed...`
);
}
}
}
}
async fundWalletAddress(address, amount) {
const fund = await this.callRpc(
"sendtoaddress",
[address, amount],
'faucet',
FAUCET_URL,
FAUCET_USERNAME,
FAUCET_PASSWORD
);
console.log(`${new Date().toUTCString()} --- Funded wallet: ${address} with ${amount} QRN`);
return fund;
}
async transferFunds(sender, address, amount) {
try {
this.total_allowed_txns--;
if ((new Date() - this.lastTime) < 60000 && this.total_allowed_txns <= 0) {
console.log(`${new Date().toUTCString()} --- Waiting for the next minute to allow more transactions...`);
await new Promise((resolve) => setTimeout(resolve, 60000 - (new Date() - this.lastTime)));
}
const res = await this.callRpc(
"sendtoaddress",
[address, amount],
sender
);
console.log(
`${new Date().toUTCString()} --- 💸 ${sender} transferred 🤑 [${amount}]QRN to address ${address}, txid: ${res.result}`
);
if (res.error) {
console.error(
`${new Date().toUTCString()} --- 💸 ${sender} failed to transfer 🤑 [${amount}]QRN to address ${address}, error: ${res.error.message}`
);
if (res.error.code === -6) {
await this.fund(sender);
}
}
return res.result;
} catch (e) {
console.log(e);
}
}
async getWalletAddress(wallet) {
const res = await this.callRpc("getnewaddress", [], wallet);
return res.result;
}
async initiateWallets() {
const result = await txnFloader.getWallets();
if (result && result.length >= WALLET_COUNT) {
console.log(`${new Date().toUTCString()} --- Available Wallets:, ${JSON.stringify(result)}`);
return result;
} else {
console.log(`${new Date().toUTCString()} --- No wallets available. Creating ${WALLET_COUNT} new wallets`);
let idx = result.length < WALLET_COUNT ? result.length : WALLET_COUNT;
for (let i = idx; i < WALLET_COUNT; i++) {
const wallet = await this.createNewWallet(`wallet_${i + 1}`);
console.log(`${new Date().toUTCString()} --- Created wallet: wallet_${i + 1} wallet=${JSON.stringify(wallet)}`);
await new Promise((resolve) => setTimeout(resolve, TIME_OUT));
}
console.log(`${new Date().toUTCString()} --- Checking for wallets one more time...`);
return this.initiateWallets();
}
}
async getWalletBalance(walletName) {
const res = await this.callRpc("getbalance", [], walletName);
return res.result;
}
async getWalletBalances(walletName) {
const res = await this.callRpc("getbalances", [], walletName);
return res.result;
}
async createNewWallet(walletName) {
const res = await this.callRpc(
"createwallet",
[walletName, false, false, "", false, false, true],
null
);
const balance = await this.getWalletBalance(walletName);
console.log(`${new Date().toUTCString()} --- ${walletName} balance: `, balance);
return res.result;
}
async getWallets() {
try {
const res = await this.callRpc("listwallets", [], null);
return res.result;
} catch (err) {
console.log(err);
}
}
async start() {
while (true) {
await this.createAndFundWallets();
}
}
}
throw new Error(`
Deprecated Error:
This script has been deprecated Please run node app.js
`);
const txnFloader = new TxnFloader();
initializeDatabase();
txnFloader.start();