Typescript
SPL Token di Web3.js

SPL Token di Web3.js

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.

Kita menetapkan jumlah "disetujui", dan delegasi hanya dapat mentransfer hingga jumlah tersebut

Revoke menghapus wewenang delegasi saat ini atas akun, mengembalikan kontrol penuh kepada pemilik akun.

Segera membatalkan delegasi yang ada dan hanya pemilik akun yang dapat mencabut delegasi (bukan delegasi itu sendiri)

Sebelum kita dapat mendelegasikan atau mencabut akun token apa pun, kita perlu sudah memiliki:

  • Akun Mint yang sudah diinisialisasi
  • Akun Token atau akun Associated Token yang sudah diinisialisasi yang akan kita kendalikan

Jumlah token yang kita setujui "dinormalisasi" untuk desimal. Ini berarti jika kita ingin menyetujui 1 token yang memiliki 6 desimal, kita perlu memasukkan 1e6 sebagai jumlahnya

Raw Instruction

Dengan menggunakan instruksi "raw" tanpa abstraksi apa pun, beginilah cara menyetujui token:

ts
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:

ts
// 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():

ts
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():

ts
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`)
Daftar Isi
Lihat Sumber
Blueshift © 2025Commit: 1e001ec