0

我正在尝试将代币从一个 Solana 地址转移到另一个地址,虽然我能够提示数据,但金额始终为空白。

通过许多调整和更改,金额似乎保持为空。如果我不包括金额,则不会出现该行,但是当我这样做时,就不会显示任何金额。

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);
4

1 回答 1

0

您似乎正在尝试转移 SPL 代币金额。要正确进行转账,您必须提供 SPL 代币程序所期望的准确指令。

对于转账,在账户中,您需要提供源账户(一个 SPL 代币账户),然后是目标账户(一个 SPL 代币账户),然后是签名者(某个其他账户)。对于数据,您需要提供字节3(用于传输指令),后跟 8 个字节的 little-endian 64 位无符号整数,表示要传输的数量。更多信息请访问https://github.com/solana-labs/solana-program-library/blob/36e886392b8c6619b275f6681aed6d8aae6e70f9/token/program/src/instruction.rs#L88

一般来说,使用 Solana 提供的 JS 包会更方便:https ://www.npmjs.com/package/@solana/spl-token

以下是创建新代币铸币厂和执行转移的示例测试:https ://github.com/solana-labs/solana-program-library/blob/master/token/js/examples/create_mint_and_transfer_tokens.js

于 2021-09-07T20:54:32.423 回答