-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (121 loc) · 5 KB
/
index.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// import "dotenv/config";
import bs58 from "bs58";
import fs from "fs";
import { PublicKey, Keypair, Connection, Transaction, sendAndConfirmTransaction } from "@solana/web3.js";
import { Metadata } from "@metaplex-foundation/mpl-token-metadata";
import { programs, actions, NodeWallet } from "@metaplex/js";
import { TOKEN_PROGRAM_ID, createBurnCheckedInstruction, createCloseAccountInstruction, getOrCreateAssociatedTokenAccount, createMintToInstruction } from "@solana/spl-token"
import { createBurnNftInstruction } from "@metaplex-foundation/mpl-token-metadata";
import { Metaplex, keypairIdentity, BundlrStorageDriver, toMetaplexFile } from "@metaplex-foundation/js";
import "dotenv/config";
const SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID = new PublicKey(
'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL',
);
const METAPLEX_TOKEN_METADATA_PROGRAM_ID = new PublicKey(
"metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"
);
const getTokenWallet = async function (
wallet,
mint,
) {
return (
await PublicKey.findProgramAddress(
[wallet.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), mint.toBuffer()],
SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID,
)
)[0];
};
async function burnNFT(connection, treasuryKeypair, nftObj) {
try {
const wallet = new NodeWallet(treasuryKeypair);
const tokenWallet = await getTokenWallet(wallet.publicKey, nftObj.mintAddress);
console.log("Token Wallet:", tokenWallet.toString(), "Treasury:", treasuryKeypair.publicKey.toString(), "Token:", nftObj.mintAddress.toString(), "Metadata:", nftObj.metadataAddress.toString(), "Edition:", nftObj.edition.address.toString());
const { blockhash } = await (connection.getLatestBlockhash('finalized'))
const burnAndClose = new Transaction({
recentBlockhash: blockhash,
// The buyer pays the transaction fee
feePayer: treasuryKeypair.publicKey,
})
const burnNFTIx = createBurnNftInstruction(
{
metadata: nftObj.metadataAddress,
owner: wallet.publicKey,
mint: nftObj.mintAddress,
tokenAccount: tokenWallet,
masterEditionAccount: nftObj.edition.address,
splTokenProgram: TOKEN_PROGRAM_ID,
}
);
burnAndClose.add(burnNFTIx);
const burnAndCloseTx = await sendAndConfirmTransaction(connection, burnAndClose, [treasuryKeypair]);
const returnArrayPacket = {
Success: true,
burnNFT: burnAndCloseTx
}
return returnArrayPacket;
}
catch (err) {
console.error(err);
const returnArrayPacket = {
Success: false,
burnNFT: ""
}
return returnArrayPacket;
}
}
async function runCriticalTX(connection, treasuryKeypair, nftsToUpdate) {
const metaplex = Metaplex.make(connection);
const nftsToUpdateArray = nftsToUpdate.map(nft => new PublicKey(nft));
const lazyNfts = await metaplex.nfts().findAllByMintList(nftsToUpdateArray).run();
const nfts = await Promise.all(lazyNfts.map(async nft => {
try {
const activeNft = await metaplex.nfts().loadNft(nft).run();
return activeNft;
} catch (error) {
}
}));
console.log("NFT Burn List:", JSON.stringify(nfts.map((nft)=>{
return {
name:nft.name,
mintHash:nft.mintAddress.toString()
}
}), null, 2));
let receivableImploded = nfts;
let returnObj;
while (receivableImploded.length > 0) {
const failedArray = new Array;
let delay = 0;
await Promise.all(receivableImploded.map(async (nftObj) => {
await timeout((delay++) * 15);
const returnArrayPacket = await burnNFT(connection, treasuryKeypair, nftObj);
if (returnArrayPacket.Success) {
console.log("Successfully burned NFT");
returnObj = returnArrayPacket.burnNFT;
}
else if (!returnArrayPacket.Success) {
failedArray.push(nftObj);
console.log("Failed to burn");
}
}));
receivableImploded = failedArray;
console.log("Failed to burn:", receivableImploded.length);
await timeout(10000);
}
console.log("Succeeded, generated:", returnObj);
}
function timeout(ms) {
// console.log("Wait:", ms);
return new Promise(resolve => setTimeout(resolve, ms));
}
async function updateNFTS() {
const connection = new Connection(process.env.RPC, {
confirmTransactionInitialTimeout: 60000,
commitment: "confirmed"
})
const treasuryPrivateKey = process.env.TREASURY
const treasuryKeypair = Keypair.fromSecretKey(bs58.decode(treasuryPrivateKey))
console.log("Burning NFTs Owned by Public Key:", treasuryKeypair.publicKey.toString());
const hashList = JSON.parse(fs.readFileSync("./hashList.json").toString());
await runCriticalTX(connection, treasuryKeypair, hashList);
}
updateNFTS();