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 做一些事情,我想把它分解成字节长度,这样我就可以从已签名的交易中重新填充:
- fromTokenAccount,
- toTokenAccount,
- senderPublicKey,
- 数量,
- [], // 这是变量还是零字节?
- splToken.TOKEN_PROGRAM_ID,
来自上面的 splToken.createTransferInstruction()。
非常感谢!
从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);
希望获得有关如何从以下代码片段中的缓冲区变量中逐步输入 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 做一些事情,我想把它分解成字节长度,这样我就可以从已签名的交易中重新填充:
- fromTokenAccount,
- toTokenAccount,
- senderPublicKey,
- 数量,
- [], // 这是变量还是零字节?
- splToken.TOKEN_PROGRAM_ID, 来自上面的 splToken.createTransferInstruction()。
非常感谢!
从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);