-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathdistribute.js
More file actions
82 lines (68 loc) · 2.37 KB
/
distribute.js
File metadata and controls
82 lines (68 loc) · 2.37 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
const fs = require("fs").promises; // Use fs.promises for async file operations
const ethers = require("ethers");
const Mutex = require('async-mutex').Mutex;
const abi = [
{
"inputs": [
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
];
async function readData() {
const data = await fs.readFile(process.env.DATA_FILE, 'utf-8');
return JSON.parse(data);
}
async function writeData(data) {
await fs.writeFile(process.env.DATA_FILE, JSON.stringify(data, null, 2), 'utf-8');
}
async function sendTransaction(contract, recipient, nonce) {
const amount = ethers.utils.parseUnits("2.5", 18);
console.log(`sending ${amount} to ${recipient.address} with nonce ${nonce}`);
const tx = await contract.transfer(recipient.address, amount, { nonce });
await tx.wait(2);
return tx;
}
async function main() {
const mutex = new Mutex();
const data = await readData();
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_ENDPOINT);
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
const contract = new ethers.Contract(process.env.TTKO_ADDRESS, abi, wallet);
let nonce = await wallet.getTransactionCount();
let firstTx = true;
await Promise.all(data.map(async (recipient, i) => {
if (recipient.sent || recipient.address === "0x0000000000000000000000000000000000000000" || recipient.address === "0x0000000000000000000000000000000000000001") {
return;
}
const release = await mutex.acquire();
nonce = (i === 0 || firstTx) ? nonce : nonce + 1;
if (firstTx) {
firstTx = false;
}
const tx = await sendTransaction(contract, recipient, nonce);
release();
recipient.sent = true;
recipient.txHash = tx.hash;
await writeData(data);
}));
}
main().then(() => console.log("done")).catch(console.error);