-
Notifications
You must be signed in to change notification settings - Fork 4
/
mainnet.js
77 lines (64 loc) · 2.49 KB
/
mainnet.js
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
const web3 = require('@solana/web3.js');
const splToken = require('@solana/spl-token');
(async () => {
//Create connection to mainnet
const connection = new web3.Connection(web3.clusterApiUrl('mainnet-beta'));
//Generate keypair from Uint8Array(Make sure it's right format! i.e. [123,123,123...])
let secretKey = Uint8Array.from('YOUR KEY HERE');
const myKeypair = web3.Keypair.fromSecretKey(secretKey);
const tokenMintAddress = new web3.PublicKey('TOKEN MINT ADDRES');
const nftReciver = new web3.PublicKey('WALLET ADDRES TO SEND');
let my_token_account = await splToken.getOrCreateAssociatedTokenAccount(
connection,
payer = myKeypair,
mint = tokenMintAddress,
owner = myKeypair.publicKey,
commitment = 'finalized',
allowOwnerOffCurve = false,
confirmOptions = null,
programId = splToken.TOKEN_PROGRAM_ID,
associatedTokenProgramId = splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
)
let reciver_token_account = await splToken.getOrCreateAssociatedTokenAccount(
connection,
payer = myKeypair,
mint = tokenMintAddress,
owner = nftReciver,
commitment = 'finalized',
allowOwnerOffCurve = false,
confirmOptions = null,
programId = splToken.TOKEN_PROGRAM_ID,
associatedTokenProgramId = splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
);
console.log('My token account public address: ' + my_token_account.address.toBase58());
console.log('Reciver token account public address: ' + reciver_token_account.address.toBase58());
try {
await transfer_tokens(
wallet = myKeypair,
connection,
amount = 1,
reciver_token_account = reciver_token_account,
from_token_account = my_token_account,
)
} catch (error) {
console.log(error)
}
console.log('Done!');
})();
async function transfer_tokens(wallet, connection, amount, reciver_token_account, from_token_account) {
//if trx takes more when 60 sec to complete you will receive error here
const transfer_trx = await splToken.transfer(
connection,
payer = wallet,
source = from_token_account.address,
destination = reciver_token_account.address,
owner = wallet,
amount = amount,
multiSigners = [wallet],
confirmOptions = false,
programId = splToken.TOKEN_PROGRAM_ID,
)
console.log(transfer_trx)
console.log("Transcation signature", transfer_trx);
console.log("Success!(we assume)");
}