[英]Is there any way to get amount(number of token we are sending in transaction) after calling transaction.from() in solana
var transaction = new web3.Transaction({
feePayer: new web3.PublicKey(
'2joML3MhVLPmASMDBYuaMzsFiCtdm3aityWu1pJZ1wg8',
),
}).add(
splToken.Token.createTransferInstruction(
programId,
user1TokenAccount.address,
user2TokenAccount.address,
user1Wallet.publicKey,
[],
1,
),
); let blockhashObj = await connection.getRecentBlockhash();
transaction.recentBlockhash = await blockhashObj.blockhash;
let endocdeTransction = transaction.serialize({
requireAllSignatures: false,
verifySignatures: false,
});
let newconnectionTransction = web3.Transaction.from(newEncodedBuffer);让 newconnectionTransction = web3.Transaction.from(newEncodedBuffer);
i was getting amount in buffer data: <Buffer 03 01 00 00 00 00 00 00 00> which i was unable to decode我在缓冲区数据中获得了数量: <Buffer 03 01 00 00 00 00 00 00 00> 我无法解码
This ability is not exposed for now on the JS side.这个能力暂时没有在 JS 端暴露出来。 To do this properly, we would need to expose this struct definition: https://github.com/solana-labs/solana-program-library/blob/e8b7009cc4d8cdd87232ccfc9ce93ab203ada496/token/js/client/token.js#L1519
要正确执行此操作,我们需要公开此结构定义: https : //github.com/solana-labs/solana-program-library/blob/e8b7009cc4d8cdd87232ccfc9ce93ab203ada496/token/js/client/token.js#L1519
On your side, for testing, you can copy that struct layout, then call decode
on the transaction data to deserialize it.在您这边,为了进行测试,您可以复制该结构布局,然后对交易数据调用
decode
以对其进行反序列化。 In (untested) code, this would look like:在(未经测试的)代码中,这看起来像:
const decodedTransaction = Transaction.from(encodedTransaction);
const TransferInstructionLayout = BufferLayout.struct([
BufferLayout.u8('instruction'),
Layout.uint64('amount'),
]);
const instructionData = TransferInstructionLayout.decode(decodedTransaction.instructions[0].data);
console.log(instructionData.amount);
There may be some steps missing, but this will get you most of the way there!可能会遗漏一些步骤,但这将使您获得大部分方法! And if you want to add a PR to expose these, then everyone else can use them.
如果你想添加一个 PR 来公开这些,那么其他人都可以使用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.