使用 splToken 进行令牌传输,在要求签名时抛出错误

问题描述 投票:0回答:1

创建了一个函数,使用 spl-token 在 Solana 上传输自定义令牌,但是当我签署交易时,它提示我此错误:

Error processing intruction 2: invalid accounr data for instrction

代码:

transferToken = async () => {
    const tokenAddress = new solanaWeb3.PublicKey("5jFnsfx36DyGk8uVGrbXnVUMTsBkPXGpx6e69BiGFzko");
    const receiver = new solanaWeb3.PublicKey(settings.reciver);

    const blockhash = await this.connection.getRecentBlockhash();

    const from = await splToken.Token.getAssociatedTokenAddress(
        splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
        splToken.TOKEN_PROGRAM_ID,
        tokenAddress,
        this.publicKey
    );

    const to = await splToken.Token.getAssociatedTokenAddress(
        splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
        splToken.TOKEN_PROGRAM_ID,
        tokenAddress,
        receiver
    );

    let instructions = splToken.Token.createTransferInstruction(
        splToken.TOKEN_PROGRAM_ID,
        from,
        to,
        this.publicKey,
        [],
        1000000000
    );

    let transaction = new solanaWeb3.Transaction();
    transaction.recentBlockhash = blockhash.blockhash; 

    transaction.add(instructions);

    transaction.feePayer = this.publicKey; 

    const signedTransaction = await this.provider.signTransaction(transaction);
    let signature = await this.connection.sendRawTransaction(signedTransaction);
    await this.connection.confirmTransaction(signature);
    console.log(signature);
}

我不明白为什么这不起作用..

我尝试查找一些类似的问题,但没有任何帮助

javascript web3js solana solana-web3js
1个回答
0
投票

您没有提供您正在使用的库,但我猜它是

@solana/spl-token
。 如果是这样,您就没有为
getAssociatedTokenAddress
传递正确的参数。

请查看文档:https://solana-labs.github.io/solana-program-library/token/js/functions/getAssociatedTokenAddress.html.

应该是:

    const from = await getAssociatedTokenAddress(
        tokenAddress,
        this.publicKey
    );
    const to = await getAssociatedTokenAddress(
        tokenAddress,
        receiver
    );
© www.soinside.com 2019 - 2024. All rights reserved.