-
Notifications
You must be signed in to change notification settings - Fork 11
/
executeAirdrop.ts
110 lines (82 loc) · 3.54 KB
/
executeAirdrop.ts
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
import * as web3 from '@solana/web3.js';
import * as anchor from "@project-serum/anchor";
import * as splToken from "@solana/spl-token";
const fs = require('fs');
import { TokenInfo, readJson, writeJson } from "./prepareAirdrop";
const USE_MAINNET = false;
const MAGIC_EDEN_ADDRESS = "GUfCR9mK6azb9vcpsxgXyj7XRPAKJd4KMHTTVvtncGgp";
async function sendToken(connection: web3.Connection, dropInfo: TokenInfo, sender: web3.Keypair): Promise<string>{
if(!dropInfo.sendableAmount || !dropInfo.sendableTokenMint || !dropInfo.owner) return "";
const mint = new web3.PublicKey(dropInfo.sendableTokenMint);
const owner = new web3.PublicKey(dropInfo.owner);
const transaction = new web3.Transaction();
//new splToken.Token(connection, mint, splToken.TOKEN_PROGRAM_ID, )
const destinationAccount = await splToken.Token.getAssociatedTokenAddress(
splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
splToken.TOKEN_PROGRAM_ID, mint,
owner, false);
const sourceAccount = await splToken.Token.getAssociatedTokenAddress(
splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
splToken.TOKEN_PROGRAM_ID, mint,
sender.publicKey, false);
const destinationAccountInfo = await connection.getAccountInfo(destinationAccount);
const destTokenAccountMissing = !destinationAccountInfo;
if(destTokenAccountMissing){
console.log("creating token account...");
const createATAinstruction = splToken.Token.createAssociatedTokenAccountInstruction(
splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
splToken.TOKEN_PROGRAM_ID, mint,
destinationAccount, owner, sender.publicKey);
transaction.add(createATAinstruction);
}
const transferInstruction =
splToken.Token.createTransferInstruction(
splToken.TOKEN_PROGRAM_ID,
sourceAccount,
destinationAccount,
sender.publicKey,
[],
dropInfo.sendableAmount
)
transaction.add(transferInstruction);
console.log("Sending form", sourceAccount.toBase58(),"to", destinationAccount.toBase58());
const txid = await web3.sendAndConfirmTransaction(
connection, transaction, [sender], { commitment: 'confirmed' });
return txid;
}
export function loadWalletKey(keypairFile:string): web3.Keypair {
if (!keypairFile || keypairFile == '') {
throw new Error('Keypair is required!');
}
const loaded = web3.Keypair.fromSecretKey(
new Uint8Array(JSON.parse(fs.readFileSync(keypairFile).toString())),
);
console.log(`using wallet: ${loaded.publicKey}`);
return loaded;
}
async function main() {
const rpcHost = web3.clusterApiUrl(USE_MAINNET?"mainnet-beta":"devnet");
const c = new anchor.web3.Connection(rpcHost);
const allInfo = readJson();
const myKeypairFile = "C:\\Users\\loopc\\wkdir\\DEVkasD4qwUJQ8LS7rcJHcGDQQzrEtWgk2jB6v5FHngo.json";
const sender = loadWalletKey(myKeypairFile);
//const txid = await sendToken(c, allInfo[0], sender);
//console.log(txid);
//return;
for (let i = 0; i<allInfo.length; i++){
if(!allInfo[i].txid){
if(allInfo[i].owner === MAGIC_EDEN_ADDRESS) {
console.log("skipping MagicEden address...");
continue;
}
const txid = await sendToken(c, allInfo[i], sender);
allInfo[i].txid = txid;
writeJson(allInfo);
console.log(txid);
await new Promise(f => setTimeout(f, 500));
}
}
}
if (require.main === module) {
main();
}