Instruksi Approve dan Revoke
Approve memberikan delegasi wewenang untuk mentransfer sejumlah token tertentu atas nama pemilik akun. Ini memungkinkan transfer token secara terprogram tanpa memberikan kontrol penuh atas akun.
Revoke menghapus wewenang delegasi saat ini atas akun, mengembalikan kontrol penuh kepada pemilik akun.
Sebelum kita dapat mendelegasikan atau mencabut akun token apa pun, kita perlu sudah memiliki:
- Akun
Mint
yang sudah diinisialisasi - Akun
Token
atau akunAssociated Token
yang sudah diinisialisasi yang akan kita kendalikan
Raw Instruction
Dengan menggunakan instruksi "raw" tanpa abstraksi apa pun, beginilah cara menyetujui token:
const tokenAccount = Keypair.generate();
const tokenAccount = await getAssociatedTokenAddress(
mint.publicKey,
tokenAccount.publicKey,
);
// Create ATA creation instruction
const createAtaInstruction = createAssociatedTokenAccountIdempotentInstruction(
keypair.publicKey, // payer
tokenAccount, // associated token account address
destination.publicKey, // owner
mint.publicKey, // mint
);
// Delegate an ATA
const approveInstruction = createApproveInstruction(
tokenAccount // account
delegate.publicKey, // delegate
keypair.publickey // owner
1e6, // amount of tokens
);
const transaction = new Transaction().add(
createAtaInstruction,
approveInstruction,
);
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair]);
console.log(`Token accounts created and delegated! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);
Dengan menggunakan instruksi "raw" tanpa abstraksi apa pun, beginilah cara mencabut token:
// Revoke the delegate of an ATA
const revokeInstruction = createRevokeInstruction(
tokenAccount // account
keypair.publickey // owner
);
const transaction = new Transaction().add(revokeInstruction);
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair]);
console.log(`Token account delegate revoked! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);
Abstracted Instruction
Beginilah instruksi yang sama akan terlihat jika diabstraksikan dengan instruksi approve()
:
const tokenAccount = Keypair.generate();
const ata = await getOrCreateAssociatedTokenAccount(
connection,
keypair,
mint,
tokenAccount.publicKey
);
console.log(`This is your ATA: ${ata.address}!`)
let tx = await approve(
connection,
keypair,
ata.address, // token Account
delegate-publicKey, // delegate
keypair.publicKey, // owner
1e6, // amount
);
console.log(`Succesfully Delegated!. Transaction Here: https://explorer.solana.com/tx/${tx}?cluster=devnet`)
Beginilah instruksi yang sama akan terlihat jika diabstraksikan dengan instruksi revoke()
:
let tx = await revoke(
connection,
keypair,
ata.address, // token Account
keypair.publicKey, // owner
);
console.log(`Succesfully Revoked Delegate!. Transaction Here: https://explorer.solana.com/tx/${tx}?cluster=devnet`)