Instruction chuyển token
Chuyển token từ account này sang account khác. Đây là thao tác cơ bản để gửi token giữa các người dùng.
Trước khi chúng ta có thể chuyển bất kỳ token nào, chúng ta sẽ cần phải có:
- Account
Mint
đã được khởi tạo. - Account
Token
nguồn hoặc accountAssociated Token
đã có tối thiểu số lượng chúng ta muốn chuyển. - Account
Token
đích hoặc accountAssociated Token
sẽ nhận token từ accountToken
nguồn.
Instruction thô
Bằng cách chỉ sử dụng instruction "thô" mà không có bất kỳ abstraction nào, đây là cách chuyển token sẽ trông như thế nào:
const destination = Keypair.generate();
const destinationTokenAccount = await getAssociatedTokenAddress(
mint.publicKey,
destination.publicKey,
);
// Create ATA creation instruction
const createAtaInstruction = createAssociatedTokenAccountIdempotentInstruction(
keypair.publicKey, // payer
destinationTokenAccount, // associated token account address
destination.publicKey, // owner
mint.publicKey, // mint
);
// Transfer tokens to ATA
const transferInstruction = createTransferInstruction(
sourceTokenAccount, // source token account pubkey
destinationTokenAccount, // destination token account pubkey
keypair.publicKey, // owner of the source token account
1e6, // amount
);
const transaction = new Transaction().add(
createAtaInstruction,
transferInstruction,
);
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair]);
console.log(`Token accounts created and tokens transferred! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);
Instruction được trừu tượng hóa
Đây là cách các instruction tương tự sẽ trông như thế nào được trừu tượng hóa với instruction transfer()
:
const destination = Keypair.generate();
const destinationAta = await getOrCreateAssociatedTokenAccount(
connection,
keypair,
mint,
destination.publicKey
);
console.log(`This is your ATA: ${ata.address}!`)
let tx = await transfer(
connection, // connection
keypair, // payer
sourceAta, // source token account
destinationAta, // destination token account
keypair.publicKey, // owner of the source token account
1e6, // amount to transfer
);
console.log(`Succesfully Transferred!. Transaction Here: https://explorer.solana.com/tx/${tx}?cluster=devnet`)