-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.ts
108 lines (83 loc) · 3.09 KB
/
main.ts
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
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { getBalance, waitForBalance } from './src/utils/balance';
import { churn } from './src/utils/churn';
import { randomDelay } from './src/utils/delay';
import { createAccount, transfer, getAccounts } from './src/utils/rpc';
import { calculateAmuRange } from './src/utils/calc';
import { randomRange } from './src/utils/random';
interface Args {
'rpc-url': string;
churnRange: number[];
}
const argv = yargs(hideBin(process.argv))
.option('rpc-url', {
alias: 'rpc',
type: 'string',
description: 'The RPC URL to connect to',
default: 'http://127.0.0.1:18082/json_rpc',
})
.option('churnRange', {
alias: 'cr',
type: 'array',
description:
'Random number of times to perform churn by given range - Churn range, e.g., --churnRange, -cr 3,7',
default: [3, 7], // inclusive, exclusive
coerce: (arg) => (Array.isArray(arg) ? arg : arg.split(',').map(Number)), // check if it's an array or string
})
.help().argv as Args;
const rpcUrl = argv['rpc-url'];
const churnRange: [number, number] =
argv.churnRange.length === 2 ? (argv.churnRange as [number, number]) : [3, 7];
const delayRange: [number, number] = [30 * 60 * 1000, 60 * 60 * 1000];
async function main() {
const { balance: totalBalance } = await getBalance(rpcUrl, 0);
console.log('Total balance in account 0:', totalBalance / 1e12);
const [minRange, maxRange] = calculateAmuRange(totalBalance / 1e12).map(
(value) => value * 1e12,
);
console.log('Calculated range:', [minRange / 1e12, maxRange / 1e12]);
while (true) {
const { balance, unlockedBalance } = await getBalance(rpcUrl);
console.log('Balance:', balance / 1e12);
console.log('Unlocked:', unlockedBalance / 1e12);
if (balance < maxRange) break;
if (unlockedBalance < maxRange) {
console.log('Waiting for funds to unlock...');
await waitForBalance(rpcUrl, 0, maxRange);
console.log('Funds unlocked');
}
const address = await createAccount(rpcUrl);
console.log('Created new account with address:', address);
const amu = Math.floor(Math.random() * (maxRange - minRange) + minRange);
console.log('Sending', amu / 1e12, 'XMR to', address);
await transfer(rpcUrl, amu, address);
console.log('Transaction sent to RPC');
}
console.log('Finished distributing (out of funds)');
await randomDelay(delayRange);
console.log('Starting churn');
const accounts = await getAccounts(rpcUrl);
const accountsToChurn = accounts
.filter((acc) => acc.balance)
.map((acc) => ({
...acc,
iters: randomRange(churnRange),
}));
for (let i = 0; i < churnRange[1]; i++) {
for (const account of accountsToChurn) {
if (i >= account.iters) continue;
console.log(
'Doing churn %d/%d for account index %d',
i + 1,
account.iters,
account.account_index,
);
const oldIndex = account.account_index;
account.account_index = await churn(rpcUrl, oldIndex);
await randomDelay(delayRange);
}
}
console.log('Finished all churns');
}
main();