solana web3 API:如何获取使用 splToken.createTransferInstruction() 创建的数据

solana web3 API: how to obtain data created with splToken.createTransferInstruction()

希望获得有关如何从以下代码片段中的缓冲区变量中逐步输入 splToken.createTransferInstruction() 的变量中获取数据的指导:

    var transaction = new web3.Transaction();
    transaction.add(
      splToken.createTransferInstruction(
        fromTokenAccount,
        toTokenAccount,
        senderPublicKey,
        amount,
        [],
        splToken.TOKEN_PROGRAM_ID,
      )
    );

    // Setting the variables for the transaction
    transaction.feePayer = provider.publicKey;    
    let blockhashObj = await connection.getRecentBlockhash();
    transaction.recentBlockhash = await blockhashObj.blockhash;
    // Transaction constructor initialized successfully
    if(transaction) { console.log('Txn created successfully'); }
    // Request creator to sign the transaction (allow the transaction)
    let signed = await provider.signTransaction(transaction);
    let buffer = signed.serialize();

使用 web3.Transaction.from(buffer) 我获得了一个 Transaction 对象 - 请参阅浏览器控制台中的图像:

我需要用指令[0].data 做一些事情,我想把它分解成字节长度,这样我就可以从已签名的交易中重新填充:

非常感谢!

TransactionInstruction开始,您可以使用decodeTransferInstruction取回初始参数。在您的情况下,您可以致电:

let tx = web3.Transaction.from(buffer);
let decodedIx = decodeTransferInstruction(tx.instructions[0]);
console.log(decodedIx.keys.source);
console.log(decodedIx.keys.destination);
console.log(decodedIx.keys.owner);
console.log(decodedIx.data.amount);

完整的源代码位于:https://github.com/solana-labs/solana-program-library/blob/24baf875e9e19c26d694d28c557d33848c3a9180/token/js/src/instructions/transfer.ts#L87