-
Notifications
You must be signed in to change notification settings - Fork 11
/
eoa.ts
113 lines (90 loc) · 3.27 KB
/
eoa.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
109
110
111
112
113
import { BigNumber } from '@ethersproject/bignumber';
import {
JsonRpcProvider,
Provider,
TransactionRequest,
} from '@ethersproject/providers';
import { parseUnits } from '@ethersproject/units';
import { Wallet } from '@ethersproject/wallet';
import { SingleBar } from 'cli-progress';
import Logger from '../logger/logger';
import { senderAccount } from './signer';
class EOARuntime {
mnemonic: string;
url: string;
provider: Provider;
gasEstimation: BigNumber = BigNumber.from(0);
gasPrice: BigNumber = BigNumber.from(0);
// The default value for the E0A to E0A transfers
// is 0.0001 native currency
defaultValue: BigNumber = parseUnits('0.0001');
constructor(mnemonic: string, url: string) {
this.mnemonic = mnemonic;
this.provider = new JsonRpcProvider(url);
this.url = url;
}
async EstimateBaseTx(): Promise<BigNumber> {
// EOA to EOA transfers are simple value transfers between accounts
this.gasEstimation = await this.provider.estimateGas({
from: Wallet.fromMnemonic(this.mnemonic, `m/44'/60'/0'/0/0`)
.address,
to: Wallet.fromMnemonic(this.mnemonic, `m/44'/60'/0'/0/1`).address,
value: this.defaultValue,
});
return this.gasEstimation;
}
GetValue(): BigNumber {
return this.defaultValue;
}
async GetGasPrice(): Promise<BigNumber> {
this.gasPrice = await this.provider.getGasPrice();
return this.gasPrice;
}
async ConstructTransactions(
accounts: senderAccount[],
numTx: number
): Promise<TransactionRequest[]> {
const queryWallet = Wallet.fromMnemonic(
this.mnemonic,
`m/44'/60'/0'/0/0`
).connect(this.provider);
const chainID = await queryWallet.getChainId();
const gasPrice = this.gasPrice;
Logger.info(`Chain ID: ${chainID}`);
Logger.info(`Avg. gas price: ${gasPrice.toHexString()}`);
const constructBar = new SingleBar({
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true,
});
Logger.info('\nConstructing value transfer transactions...');
constructBar.start(numTx, 0, {
speed: 'N/A',
});
const transactions: TransactionRequest[] = [];
for (let i = 0; i < numTx; i++) {
const senderIndex = i % accounts.length;
const receiverIndex = (i + 1) % accounts.length;
const sender = accounts[senderIndex];
const receiver = accounts[receiverIndex];
transactions.push({
from: sender.getAddress(),
chainId: chainID,
to: receiver.getAddress(),
gasPrice: gasPrice,
gasLimit: this.gasEstimation,
value: this.defaultValue,
nonce: sender.getNonce(),
});
sender.incrNonce();
constructBar.increment();
}
constructBar.stop();
Logger.success(`Successfully constructed ${numTx} transactions`);
return transactions;
}
GetStartMessage(): string {
return '\n⚡️ EOA to EOA transfers initialized ️⚡️\n';
}
}
export default EOARuntime;