forked from cocrafts/gasilon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.ts
111 lines (100 loc) · 2.79 KB
/
setup.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
111
import {
ACCOUNT_SIZE,
createAssociatedTokenAccountInstruction,
getAssociatedTokenAddressSync,
} from '@solana/spl-token';
import {
clusterApiUrl,
Connection,
Keypair,
LAMPORTS_PER_SOL,
PublicKey,
sendAndConfirmTransaction,
Transaction,
} from '@solana/web3.js';
import base58 from 'bs58';
import dotenv from 'dotenv';
import readline from 'readline-sync';
import config from './config.json';
dotenv.config();
async function main() {
if (process.env.ENVIRONMENT === 'production') {
console.log('Use mainnet beta');
} else {
console.log('Use devnet');
}
const connection = new Connection(
process.env.ENVIRONMENT === 'production'
? clusterApiUrl('mainnet-beta')
: clusterApiUrl('devnet'),
'confirmed',
);
const SOLANA_SECRET_KEY = process.env.SOLANA_SECRET_KEY;
const keypair = Keypair.fromSecretKey(base58.decode(SOLANA_SECRET_KEY));
const tokens = config.transfer.tokens.filter((token) => {
const sampleMints = [
'7aeyZfAc5nVxycY4XEfXvTZ4tsEcqPs8p3gJhEmreXoz',
'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr',
];
if (process.env.ENVIRONMENT === 'production') {
return !sampleMints.includes(token.mint);
} else {
return sampleMints.includes(token.mint);
}
});
for (let i = 0; i < tokens.length; i++) {
console.log(
`\nCheck account for owner ${keypair.publicKey.toString()} with token: \n\tName: ${
tokens[i].name
}\n\tMint: ${tokens[i].mint}`,
);
const mint = new PublicKey(tokens[i].mint);
const atAddress = getAssociatedTokenAddressSync(mint, keypair.publicKey);
const atAccount = await connection.getAccountInfo(atAddress, 'confirmed');
if (!atAccount) {
console.log('\t-> Not found associated account');
const answer = readline
.question(
`Do you want to create associated account for this token, fee: ${
(await connection.getMinimumBalanceForRentExemption(ACCOUNT_SIZE)) /
LAMPORTS_PER_SOL
} SOL - (y/n)? `,
)
.trim()
.toLowerCase();
if (answer == 'y') {
console.log('-> Create account...');
try {
const bh = await connection.getLatestBlockhash('finalized');
const blockhash = bh.blockhash;
const lastValidBlockHeight = bh.lastValidBlockHeight;
const transaction = new Transaction({
blockhash,
lastValidBlockHeight,
}).add(
createAssociatedTokenAccountInstruction(
keypair.publicKey,
atAddress,
keypair.publicKey,
mint,
),
);
const result = await sendAndConfirmTransaction(
connection,
transaction,
[keypair],
{
commitment: 'confirmed',
},
);
console.log('-> Create successfully', result);
} catch (e) {
console.log('-> Create failed', e.message);
}
}
} else {
console.log(`\tATA: ${atAddress.toBase58()} (ready, skip creation)`);
}
}
}
main();