Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update distribute.js #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 51 additions & 36 deletions ttko-distribution/distribute.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,82 @@
const fs = require("fs");
const fs = require("fs").promises; // Use fs.promises for async file operations
const ethers = require("ethers");
var Mutex = require('async-mutex').Mutex;
const Mutex = require('async-mutex').Mutex;

const abi = [
{
"inputs": [
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
{
"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 = JSON.parse(fs.readFileSync(process.env.DATA_FILE));
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 nonce = await wallet.getTransactionCount();
let firstTx = true;

await Promise.all(data.map(async (recipient, i) => {
if(recipient.sent) return;
if(recipient.address == "0x0000000000000000000000000000000000000000") return;
if(recipient.address == "0x0000000000000000000000000000000000000001") return;

const amount = ethers.utils.parseUnits("2.5", 18);
if (recipient.sent || recipient.address === "0x0000000000000000000000000000000000000000" || recipient.address === "0x0000000000000000000000000000000000000001") {
return;
}

const release = await mutex.acquire();
nonce = i == 0 || firstTx ? nonce : nonce + 1;
if(firstTx) {
nonce = (i === 0 || firstTx) ? nonce : nonce + 1;

if (firstTx) {
firstTx = false;
}
console.log(`sending ${amount} to ${recipient.address} with nonce ${nonce}`)
const tx = await contract.transfer(recipient.address, amount, {
nonce
});
release();

const tx = await sendTransaction(contract, recipient, nonce);
release();

await tx.wait(2);
recipient.sent = true;
recipient.txHash = tx.hash;
fs.writeFileSync(process.env.DATA_FILE, JSON.stringify(data));
await writeData(data);
}));
}

main().then(() => console.log("done")).catch(console.error);