-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtransferTokens.ts
More file actions
68 lines (51 loc) · 2.16 KB
/
Copy pathtransferTokens.ts
File metadata and controls
68 lines (51 loc) · 2.16 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
//** NOTE: THIS SCRIPT HELPS TO TRANSFER SOLANA SPL TOKEN FROM ONE ACCOUNT TO ANOTHER */
const { Connection, PublicKey, Keypair } = require('@solana/web3.js');
const { getOrCreateAssociatedTokenAccount, transfer } = require('@solana/spl-token');
import {connection} from '../lib/vars';
import { getAdminAccount,loadPublicKeysFromFile } from '../lib/helpers';
// load the stored PublicKeys for ease of use
let localKeys = loadPublicKeysFromFile();
// ensure the desired script was already run
if (!localKeys?.tokenMint)
console.warn("No local keys were found. Please run '3.createTokenWithMetadata.ts'");
// Replace these with your actual values
const DESTINATION_WALLET = '4tEfs9QjdCiZBEuZfSbshnYQZzenDpYP4GzrSiKHjiqJ';
const MINT_ADDRESS = new PublicKey(localKeys.tokenMint); // Replace with your token's mint address
const TRANSFER_AMOUNT = 26666666.64; // Amount of tokens to transfer
// Initialize connection
// Sender's wallet keypair
const adminAccountPrivKey = process.env.ADMIN_PRIV_KEY;
const adminAccount =getAdminAccount(adminAccountPrivKey);
// Receiver's public key
const receiverPubkey = new PublicKey(DESTINATION_WALLET);
// Mint address
const mintAddress = new PublicKey(MINT_ADDRESS);
(async () => {
try {
// Get or create associated token accounts for sender and receiver
const senderTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
adminAccount,
mintAddress,
adminAccount.publicKey
);
const receiverTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
adminAccount,
mintAddress,
receiverPubkey
);
// Transfer tokens
const transferSignature = await transfer(
connection,
adminAccount,
senderTokenAccount.address,
receiverTokenAccount.address,
adminAccount.publicKey,
TRANSFER_AMOUNT * 1000000 // Adjust for token decimals
);
console.log(`Transfer successful. Signature: ${transferSignature}`);
} catch (error) {
console.error("Error transferring tokens:", error);
}
})();