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);
i was getting amount in buffer data: <Buffer 03 01 00 00 00 00 00 00 00> which i was unable to decode
This ability is not exposed for now on the JS side. 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
On your side, for testing, you can copy that struct layout, then call decode on the transaction data to deserialize it. 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.