【问题标题】:How to transfer token using spl-token 0.1.8如何使用 spl-token 0.1.8 转移代币
【发布时间】:2022-09-23 05:35:57
【问题描述】:

我有这段代码使用 spl-token 0.2.x 传输令牌。

如何在 0.1.8 中使用相同的代码?根据我对文档的理解,两者之间没有重大变化,但旧版本使用 Token 类,但我不确定如何为 getOrCreateAssociatedTokenAccounttransfer 函数调用它。

async function transferToken(endpoint: string, fromWallet: Keypair, address_to: string, token_id: string)
{
    const connection = new Connection(endpoint);
    const toWalletPublicKey = new PublicKey(address_to);
    const mint_key = new PublicKey(token_id);

    // From
    const from = [connection, fromWallet, mint_key, fromWallet.publicKey];
    const fromTokenAccount = await getOrCreateAssociatedTokenAccount(...from);

    // To
    const to = [connection, fromWallet, mint_key, toWalletPublicKey];
    const toTokenAccount = await getOrCreateAssociatedTokenAccount(...to);

    // Transfer
    const transferParams = [connection, fromWallet, fromTokenAccount.address, toTokenAccount.address, fromWallet.publicKey, 1, []];
    return await transfer(...transferParams);  
}

这就是我传递从十六进制字符串加载的fromWallet KeyPair 的方式。

const fromWallet = Keypair.fromSecretKey(Uint8Array.from(Buffer.from(private_key, \'hex\')));

    标签: javascript typescript solana


    【解决方案1】:

    实际上,版本 2 确实有重大更改(因此是版本的主要改进),在这种情况下,它删除了 Token 类以支持您在示例中看到的那些功能。

    文档非常……糟糕,但是如果您检查the Github project one year ago,那么您可以看到该功能是如何一点一点地迁移的。

    getOrCreateAssociatedTokenAccounttransfer 要求使用类 Token 的方式如下:

    
    const token = new Token(connection, toWalletPublicKey, mint_key, fromWallet.publicKey) // Not sure about the last argument as it is the Signer
    
    /**
       * Retrieve the associated account or create one if not found.
       *
       * This account may then be used as a `transfer()` or `approve()` destination
       *
       * @param owner User account that will own the new account
       * @return The new associated account
       */
    const fromTokenAccount = token.getOrCreateAssociatedAccountInfo(fromWallet.publicKey)
    
    const toTokenAccount = token.getOrCreateAssociatedAccountInfo(toWalletPublicKey)
    
    /**
    * Transfer parameters
    * @param source Source account
    * @param destination Destination account
    * @param owner Owner of the source account
    * @param multiSigners Signing accounts if `owner` is a multiSig
    * @param amount Number of tokens to transfer
    */
    token.transfer(fromTokenAccount, toTokenAccount, fromWallet, [], 1)
    
    

    这也在here中得到部分回答

    【讨论】:

    • 好的!我认为我没有正确创建签名者。如何使用私钥执行此操作?这就是我从存储的私钥创建 KeyPair (fromWallet) 的方式。
    • Keypair.fromSecretKey(Uint8Array.from(Buffer.from(private_key, 'hex')))
    • 由于您只有一个签名者,因此您可以将签名者单独留在转移中,它应该来自fromTokenAccount 至于令牌的初始化,签名者只是您指向的KeyPar。你有什么特别的错误吗?
    • .../node_modules/@solana/web3.js/src/transaction.ts:611 const key = signer.publicKey.toString(); ^ TypeError: Cannot read properties of undefined (reading 'toString')
    • 您是否调试过您传递的签名者同时包含私钥和公钥?
    猜你喜欢
    • 2022-01-22
    • 2023-02-22
    • 1970-01-01
    • 2018-03-26
    • 2018-07-02
    • 2022-08-13
    • 2021-12-29
    • 1970-01-01
    • 2021-02-22
    相关资源
    最近更新 更多