簡體   English   中英

如何使用@Solana\web3.js 轉移自定義 SPL 代幣 (USDC)(金額問題)

[英]How to transfer custom SPL Token (USDC) using @Solana\web3.js (amount issues)

我一直在嘗試使用 @Solana\web3.js 傳輸自定義 SPL 令牌,但在創建指令時遇到問題。 我使用 Token.createTransferInstruction 方法創建了指令,但是在創建指令時,我的錢包會收到添加的所有信息,除了我要發送的代幣數量。 PS 我還添加了最近的 blockhash 並設置了 feepayer,所以這些應該不是問題。

var transaction = new web3.Transaction().add(
    splToken.Token.createTransferInstruction(
        splToken.TOKEN_PROGRAM_ID,
        fromTokenAcc.address,
        toTokenAcc.address,
        sender,
        [],
        1
    )
)

如上所示,據我所知,我使用了正確的參數,但是當我將交易發送到我的錢包時,金額沒有轉移。

我本來打算添加直接圖片,但 StackOverflow 說我至少需要 10 個代表才能發布圖片,抱歉:/

https://i.imgur.com/q7gzwR1.png

我還查看了指令添加到交易中的傳輸數據,它使用 3 作為傳輸指令,但是 1 應該是金額嗎?

https://i.imgur.com/XpB34nC.png

任何幫助將非常感激。 謝謝你!

Solana USDC 與其他非原生代幣一樣,也是一種 SPL 代幣,以下是如何使用最新版本的 solana/web3.js 和 spl-token 轉移區塊鏈上現有的 spl-token;

import { Keypair, PublicKey, LAMPORTS_PER_SOL } from "@solana/web3.js";
import { transfer, getOrCreateAssociatedTokenAccount } from "@solana/spl-token";
import base58 from "bs58";

export async function TransferToken(
  senderMasterKey,
  tokenAddress,
  recipientAdd,
  amt,
  connectionCluster
) {
  const TokenAddress = new PublicKey(tokenAddress);
  const recipientAddress = new PublicKey(recipientAdd);
  const senderKeypair = Keypair.fromSecretKey(base58.decode(senderMasterKey));
  const addRecipientToAcct = await getOrCreateAssociatedTokenAccount(
    connectionCluster,
    senderKeypair,
    TokenAddress,
    recipientAddress
  );
  const addSenderToAcct = await getOrCreateAssociatedTokenAccount(
    connectionCluster,
    senderKeypair,
    TokenAddress,
    senderKeypair.publicKey
  );
  const tranferToken = await transfer(
    connectionCluster,
    senderKeypair,
    addSenderToAcct.address,
    addRecipientToAcct.address,
    senderKeypair.publicKey,
    amt * 100000
  );
  return tranferToken;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM