0

I'm attempting to transfer tokens from one Solana address to another, while I was able to prompt the data, the amount is always blank.

With many tweaks and changes, it seems like the amount stays null. If I don't include the amount, the row doesn't appear but when I do, there's no amount to be shown.

function writeBigU_Int64LE(buf, value, offset, min, max) {
    let lo = Number(value & BigInt('0xffffffff'))
    buf[offset++] = lo
    lo = lo >> 8
    buf[offset++] = lo
    lo = lo >> 8
    buf[offset++] = lo
    lo = lo >> 8
    buf[offset++] = lo
    let hi = Number((value >> BigInt(32)) & BigInt('0xffffffff'))
    buf[offset++] = hi
    hi = hi >> 8
    buf[offset++] = hi
    hi = hi >> 8
    buf[offset++] = hi
    hi = hi >> 8
    buf[offset++] = hi
    return offset
  }

  let AMOUNT = BigInt(1000)
  const b = buffer.Buffer.alloc(10)
  b.writeUInt8(3, 0)
  writeBigU_Int64LE(b, AMOUNT, 1, BigInt(0), BigInt('0xffffffffffffffff'))

  const instruction = new solanaWeb3.TransactionInstruction({
    keys: [ 
        { pubkey: token, isSigner: false, isWritable: true },
        { pubkey: provider.publicKey, isSigner: false, isWritable: true },
        { pubkey: provider.publicKey, isSigner: true, isWritable: true },
    ],
    programId : program_id,
    data: b
  });

  let transaction = new solanaWeb3.Transaction()
  transaction.add(instruction);

  let { blockhash } = await connection.getRecentBlockhash();
  transaction.recentBlockhash = blockhash;
  transaction.feePayer = provider.publicKey;

  let signed = await provider.signTransaction(transaction, connection);
  console.log(signed);
  let signature = await connection.sendRawTransaction(signed.serialize());
  console.log(signature);
  await connection.confirmTransaction(signature);

1 Answers1

0

It looks like you're trying to transfer SPL Token amounts. To properly do a transfer, you'll have to provide the exact instruction as expected by the SPL Token program.

For a transfer, in the accounts, you need to provide the source account (an SPL token account), then the destination account (an SPL token account), then the signer (some other account). For the data, you need to provide the byte 3 (for a transfer instruction), followed by 8 bytes of a little-endian 64-bit unsigned integer, representing the amount to transfer. More info at https://github.com/solana-labs/solana-program-library/blob/36e886392b8c6619b275f6681aed6d8aae6e70f9/token/program/src/instruction.rs#L88

In general, it'll be easier to the JS package provided by Solana: https://www.npmjs.com/package/@solana/spl-token

Here's an example test for creating a new token mint and performing a transfer: https://github.com/solana-labs/solana-program-library/blob/master/token/js/examples/create_mint_and_transfer_tokens.js

Jon C
  • 7,019
  • 10
  • 17