Created
September 18, 2023 21:15
-
-
Save julien51/dc08dc9115c062f053477f01e19431f4 to your computer and use it in GitHub Desktop.
grant-batch.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ethers = require('ethers') | |
const fs = require("fs") | |
const readline = require("readline") | |
const contracts = require('@unlock-protocol/contracts') | |
/** | |
* A script that airdrops NFT memberships to users | |
*/ | |
const batchSize = 200 | |
const lockAddress = '0x819525dcab3eb87c006ed23f290cc28033315fb1' | |
const rpc = "https://rpc.unlock-protocol.com/5" | |
const airdrop = async (wallet, recipients) => { | |
const lock = new ethers.Contract(lockAddress, contracts.PublicLockV13.abi, wallet) | |
const expirations = recipients.map((r) => ethers.constants.MaxUint256) | |
const managers = recipients.map((r) => wallet.address) | |
const transaction = await lock.grantKeys(recipients, expirations, managers) | |
console.log(`Transaction hash: ${transaction.hash}`) | |
await transaction.wait() | |
console.log(' -> mined!') | |
} | |
const main = async () => { | |
if (!process.env.PKEY) { | |
throw new Error("No private key provided. make sure PKEY is defined"); | |
} | |
const wallet = (new ethers.Wallet(process.env.PKEY)).connect(new ethers.providers.JsonRpcProvider(rpc)) | |
let recipients = [] | |
const readInterface = readline.createInterface({ | |
input: fs.createReadStream('recipients.txt') | |
}); | |
for await (const line of readInterface) { | |
recipients.push(line) | |
if (recipients.length == batchSize) { | |
await airdrop(wallet, recipients) | |
recipients = [] | |
} | |
} | |
} | |
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