forked from thirdweb-example/airdrop-nfts-to-holders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairdrop.mjs
53 lines (43 loc) · 1.28 KB
/
airdrop.mjs
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
import { ThirdwebSDK } from "@thirdweb-dev/sdk";
import { config } from "dotenv";
import fs from "fs";
import path from "path";
config();
(async () => {
// TODO: Replace this with your network
const sdk = new ThirdwebSDK("goerli", {
secretKey: process.env.TW_SECRET_KEY
});
// TODO: Replace this with your smart contract address
const contract = await sdk.getContract(
"0x08d4CC2968cB82153Bb70229fDb40c78fDF825e8"
);
if (!contract) {
return console.log("Contract not found");
}
const nfts = await contract?.erc721.getAll();
if (!nfts) {
return console.log("No NFTs found");
}
const csv = nfts?.reduce((acc, nft) => {
const address = nft.owner;
const quantity = acc[address] ? acc[address] + 1 : 1;
return { ...acc, [address]: quantity };
}, {});
const filteredCsv = Object.keys(csv).reduce((acc, key) => {
if (key !== "0x0000000000000000000000000000000000000000") {
return {
...acc,
[key]: csv[key],
};
}
return acc;
}, {});
const csvString =
"address,quantity\r" +
Object.entries(filteredCsv)
.map(([address, quantity]) => `${address},${quantity}`)
.join("\r");
fs.writeFileSync(path.join(path.dirname("."), "nfts.csv"), csvString);
console.log("Generated nfts.csv");
})();