Skip to content

Instantly share code, notes, and snippets.

@macelai
Created June 23, 2023 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macelai/9fdaf91245a59d6d5e511309f6389a58 to your computer and use it in GitHub Desktop.
Save macelai/9fdaf91245a59d6d5e511309f6389a58 to your computer and use it in GitHub Desktop.
const { ethers } = require("hardhat");
const { createReadStream } = require("fs");
const { join } = require("path");
const { parse } = require("csv-parse");
const { finished } = require("stream/promises");
const BATCH_SIZE = 100;
const processFile = async () => {
const addressesCsv = join(__dirname, "addresses.csv");
const records = [];
const parser = createReadStream(addressesCsv).pipe(parse({}));
parser.on("readable", function () {
let record;
while ((record = parser.read()) !== null) {
records.push(...record);
}
});
await finished(parser);
return records;
};
async function main() {
const abi = [
"function setAdmin(address)",
"function setEnabled(address)",
"function setNone(address)",
];
const contractAddress = "0x0200000000000000000000000000000000000002";
const signerAddress = await ethers.provider.getSigner().getAddress();
let promises = [];
const transactionCount = await ethers.provider.getTransactionCount(
signerAddress,
"latest"
);
const addresses = await processFile();
console.log(addresses);
for (let i = 0; i < addresses.length; i++) {
const account = addresses[i];
if (promises.length == BATCH_SIZE) {
await Promise.allSettled(promises);
promises = [];
}
promises.push(
(async () => {
try {
let accountNonce = transactionCount + i;
const contract = new ethers.Contract(
contractAddress,
abi,
ethers.provider.getSigner()
);
console.log(
`Adding account ${account} to access list... (${transactionCount}, ${accountNonce})`
);
const tx = await contract.setEnabled(account, {
gasLimit: 100000,
nonce: accountNonce,
});
console.log(
`Account ${account} added to access list. Tx: ${tx.hash}`
);
} catch (e) {
console.log(e.message);
}
})()
);
}
await Promise.allSettled(promises);
promises = [];
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment