Repository navigation
Expand file tree
/
Copy pathsendEithertoken.js
More file actions
144 lines (122 loc) · 4.66 KB
/
Copy pathsendEithertoken.js
File metadata and controls
144 lines (122 loc) · 4.66 KB
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
143
144
// sendEithertoken.js
import fs from 'fs';
import path from 'path';
import {
Connection,
PublicKey,
Keypair,
clusterApiUrl,
} from '@solana/web3.js';
import {
getOrCreateAssociatedTokenAccount,
transfer,
TOKEN_2022_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID,
} from '@solana/spl-token';
// ─── Helper to load a Keypair from ~/.config/solana/<file> ─────────────
function loadKeypair(filename) {
const p = path.join(
process.env.HOME || process.env.USERPROFILE,
'.config', 'solana', filename
);
if (!fs.existsSync(p)) {
console.error(`⛔ Keypair file not found: ${p}`);
process.exit(1);
}
const secret = JSON.parse(fs.readFileSync(p, 'utf8'));
return Keypair.fromSecretKey(new Uint8Array(secret));
}
// ─── 1) Load both faucet keypairs & define both mints ────────────────────
const FAUCET_WINLEW = loadKeypair('id_winlew.json');
const FAUCET_SPLTOKEN = loadKeypair('id_spltoken.json');
const WINLEW_MINT = new PublicKey(process.env.WINLEW_MINT || 'DnrcdQVH7fdbmm4EyD7LjT9mNNozF5HuWMeKcpvjpump');
const SPLTOKEN_MINT = new PublicKey(process.env.SPLTOKEN_MINT || '6F53Ywxpmzq7F9fusLfBks6cZyhNAn7YYPMbR8qSqpqe');
const DECIMALS_WINLEW = 6;
const DECIMALS_SPLTOKEN = 6;
// ─── 2) Connect to Devnet ────────────────────────────────────────────────
const connection = new Connection(clusterApiUrl('devnet'), 'confirmed');
// ─── 3) Parse CLI args ──────────────────────────────────────────────────
// Usage: node sendEithertoken.js <recipient> <amount> [winlew|spltoken]
const [,, rawRecipient, rawAmount, rawToken = 'winlew'] = process.argv;
if (!rawRecipient || !rawAmount) {
console.error('Usage: node sendEithertoken.js <recipient_pubkey> <amount> [winlew|spltoken]');
process.exit(1);
}
// Validate recipient Pubkey
let recipient;
try {
recipient = new PublicKey(rawRecipient);
} catch (e) {
console.error('⛔ Invalid recipient pubkey:', e.message);
process.exit(1);
}
// Parse amount
const amountTokens = Number(rawAmount);
if (isNaN(amountTokens) || amountTokens <= 0) {
console.error('⛔ Invalid amount:', rawAmount);
process.exit(1);
}
// ─── 4) Select which token & faucet to use ───────────────────────────────
let faucetKeypair, mintPubkey, decimals, tokenName;
switch (rawToken.toLowerCase()) {
case 'spltoken':
faucetKeypair = FAUCET_SPLTOKEN;
mintPubkey = SPLTOKEN_MINT;
decimals = DECIMALS_SPLTOKEN;
tokenName = 'SPLTOKEN';
break;
case 'winlew':
default:
faucetKeypair = FAUCET_WINLEW;
mintPubkey = WINLEW_MINT;
decimals = DECIMALS_WINLEW;
tokenName = 'WinLEW';
}
// ─── 5) Build & send via high‑level helpers ───────────────────────────────
;(async () => {
try {
// a) Ensure the faucet’s ATA exists, _using the 2022 program_
const fromAccount = await getOrCreateAssociatedTokenAccount(
connection,
faucetKeypair, // payer & fee-payer
mintPubkey, // your Token‑2022 mint
faucetKeypair.publicKey, // owner of this ATA
false, // allowOwnerOffCurve?
'confirmed', // commitment
undefined, // confirmOptions
TOKEN_2022_PROGRAM_ID, // ← override token program
ASSOCIATED_TOKEN_PROGRAM_ID // ← override associated-token program
);
// b) Same for the recipient
const toAccount = await getOrCreateAssociatedTokenAccount(
connection,
faucetKeypair,
mintPubkey,
recipient,
false,
'confirmed',
undefined,
TOKEN_2022_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
);
// c) Transfer as before…
const rawUnits = Math.floor(amountTokens * 10 ** decimals);
const signature = await transfer(
connection,
faucetKeypair,
fromAccount.address,
toAccount.address,
faucetKeypair.publicKey,
rawUnits,
[], // multisig signers, if any
{ // override the program here too!
programId: TOKEN_2022_PROGRAM_ID
}
);
console.log(`✅ Sent ${amountTokens} ${tokenName} to ${recipient.toBase58()}`);
console.log('Signature:', signature);
} catch (err) {
console.error('❌ Error sending tokens:', err.message || err);
process.exit(1);
}
})();