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

feat(erc1155): harden script to only mint to recipients with balance 0 #306

Merged
merged 2 commits into from
Jun 26, 2023
Merged
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
44 changes: 31 additions & 13 deletions scripts/mintERC1155.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,31 @@ async function main() {

for (let i = skip; i < validRecipients.length; i = i + RECIPIENTS_CHUNK_SIZE) {
const recipientsChunk = validRecipients.slice(i, i + RECIPIENTS_CHUNK_SIZE);
const airdropTx = await erc1155.airdrop(tokenId, recipientsChunk, 1);
console.log(
`Minting token with id ${tokenId} to ${recipientsChunk.length} recipients in index range ${i} - ${
i + RECIPIENTS_CHUNK_SIZE - 1
}...`
);
console.log("Tx hash:", airdropTx.hash);
await airdropTx.wait();
console.log(`Successfully minted token to chunk:`, {
console.log(`\nProcessing recipients in index range ${i} - ${i + RECIPIENTS_CHUNK_SIZE - 1}:`, {
first: recipientsChunk[0],
last: recipientsChunk[recipientsChunk.length - 1],
numRecipients: recipientsChunk.length,
});

const balanceOfBatch = await erc1155.balanceOfBatch(recipientsChunk, Array(recipientsChunk.length).fill(tokenId));

const alreadyMintedRecipients = recipientsChunk.filter((_, i) => balanceOfBatch[i].gt(0));
const recipientsToMint = recipientsChunk.filter((_, i) => balanceOfBatch[i].eq(0));

if (alreadyMintedRecipients.length > 0) {
console.log(`Skipping ${alreadyMintedRecipients.length} already minted recipients...`);
}

if (recipientsToMint.length === 0) {
console.log(`No recipients to mint for. Skipping chunk...`);
continue;
}

console.log(`Minting token with id ${tokenId} to ${recipientsToMint.length} recipients...`);
dohaki marked this conversation as resolved.
Show resolved Hide resolved
const airdropTx = await erc1155.airdrop(tokenId, recipientsToMint, 1);
console.log("Tx hash:", airdropTx.hash);
await airdropTx.wait();
console.log(`Successfully minted token to ${recipientsToMint.length} recipients in chunk`);
}
}

Expand All @@ -54,11 +67,16 @@ async function parseAndValidateRecipients() {
const resolvedRecipients = await Promise.all(
recipientsFromFile.map(async (r) => {
if (r.toLocaleLowerCase().endsWith(".eth")) {
const resolvedAddress = await provider.resolveName(r);
if (!resolvedAddress) {
try {
const resolvedAddress = await provider.resolveName(r);
if (!resolvedAddress) {
throw new Error(`Could not resolve ENS name: ${r}`);
}
return resolvedAddress;
} catch (error) {
console.error(error);
throw new Error(`Could not resolve ENS name: ${r}`);
}
return resolvedAddress;
}
return Promise.resolve(r);
})
Expand All @@ -84,7 +102,7 @@ function getDuplicateAddresses(addresses: string[]) {
}

main().then(
() => console.log("Done"),
() => console.log("\nDone"),
(error) => {
console.log(error);
process.exitCode = 1;
Expand Down