/
airdrop.ts
99 lines (87 loc) · 2.47 KB
/
airdrop.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
import { AnchorProvider, Program } from '@coral-xyz/anchor';
import {
Connection,
Keypair,
PublicKey,
sendAndConfirmTransaction,
} from '@solana/web3.js';
import { IDL } from '../../target/types/dca_integration';
import { getAssociatedTokenAddressSync } from '@solana/spl-token';
import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes';
const RPC = process.env.RPC || 'https://api.devnet.solana.com';
const connection = new Connection(RPC);
const escrowProgramId = new PublicKey(
'EXDCASuSBHrbJqf3tbap86YeWaGoEeCqBhGRbUSnoDqm',
);
const provider = new AnchorProvider(
connection,
{} as any,
AnchorProvider.defaultOptions(),
);
const program = new Program(IDL, escrowProgramId, provider);
const admin = Keypair.fromSecretKey(
new Uint8Array(JSON.parse(process.env.ADMIN_PRIVATE_KEY!)),
);
async function getAccountsToAirdrop() {
const offset = 169; // 8 + 8 + 32 * 4 + 8 * 3 + 1
const accountsYetToAirdrop = await program.account.escrow.all([
{
memcmp: {
offset,
bytes: bs58.encode([0]),
},
},
]);
const accountsToAirdrop = accountsYetToAirdrop.filter((escrow) => {
return escrow.account.completed;
});
return accountsToAirdrop;
}
async function airdrop() {
const accountsToAirdrop = await getAccountsToAirdrop();
console.log({ accountsToAirdrop });
try {
const res = await Promise.all(
accountsToAirdrop.map(async (escrow) => {
const tx = await program.methods
.airdrop()
.accounts({
admin: admin.publicKey,
user: escrow.account.user,
escrow: escrow.publicKey,
outputMint: escrow.account.outputMint,
adminTokenAccount: getAssociatedTokenAddressSync(
escrow.account.outputMint,
admin.publicKey,
false,
),
userTokenAccount: getAssociatedTokenAddressSync(
escrow.account.outputMint,
escrow.account.user,
false,
),
})
.transaction();
return sendAndConfirmTransaction(connection, tx, [admin], {
skipPreflight: false,
commitment: 'confirmed',
});
}),
);
console.log({ res });
} catch (err) {
throw err;
}
}
async function main() {
while (true) {
try {
await airdrop();
await new Promise((resolve) => setTimeout(resolve, 1000));
} catch (err) {
// add error handling if needed
throw err;
}
}
}
main();