【问题标题】:How to send USDC-SPL using @solana-labs/web3.js如何使用@solana-labs/web3.js 发送 USDC-SPL
【发布时间】:2021-11-07 17:13:10
【问题描述】:

我可以使用 web3.js 发送 SOL 令牌,但无法使用 USDC-SPL 令牌发送。这是我当前发送 SOL 令牌的代码:

 // .......
 const connection = new Connection(network);
 const transaction = new Transaction()
  .add(
    SystemProgram.transfer({
      fromPubkey: publicKey,
      toPubkey: to,
      lamports: Number(amount) * 1000000000,
    })
  );
  const { blockhash } = await connection.getRecentBlockhash();
  transaction.recentBlockhash = blockhash;
  transaction.feePayer = publicKey;
  let signedTransaction: any = null;
  if ("solana" in window) {
    signedTransaction = await window.solana.signTransaction(transaction)
  } else if ("solflare" in window) {
    signedTransaction = await window.solflare.signTransaction(transaction);
  }
 // .......

我之前看到的答案来自发送自己定制令牌的人

【问题讨论】:

    标签: solana


    【解决方案1】:

    solana 上的所有 spl 代币(也包括 USDC 代币)都归 spl-token 程序所有。 因此,您可以通过将代币地址更改为“EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v”(这是 solana 主网上的 USDC spl 代币地址),以与发送您自己的定制代币相同的方式发送 USDC。 例如:

    import * as web3 from "@solana/web3.js";
    import * as splToken from "@solana/spl-token";
    
    const DEMO_WALLET_SECRET_KEY = new Uint8Array([
        ...
      ]);
    
    const SOLANA_MAINNET_USDC_PUBKEY = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
    
    (async () => {
      // Connect to cluster
      var connection = new web3.Connection(web3.clusterApiUrl("devnet"));
      // Construct wallet keypairs
      var fromWallet = web3.Keypair.fromSecretKey(DEMO_WALLET_SECRET_KEY);
      var toWallet = web3.Keypair.generate();
      
      // Construct my token class
      var USDC_pubkey = new web3.PublicKey(SOLANA_MAINNET_USDC_PUBKEY);
      var USDC_Token = new splToken.Token(
        connection,
        USDC_pubkey,
        splToken.TOKEN_PROGRAM_ID,
        fromWallet
      );
      // Create associated token accounts for my token if they don't exist yet
      var fromTokenAccount = await USDC_Token.getOrCreateAssociatedAccountInfo(
        fromWallet.publicKey
      )
      var toTokenAccount = await USDC_Token.getOrCreateAssociatedAccountInfo(
        toWallet.publicKey
      )
      // Add token transfer instructions to transaction
      var transaction = new web3.Transaction()
        .add(
          splToken.Token.createTransferInstruction(
            splToken.TOKEN_PROGRAM_ID,
            fromTokenAccount.address,
            toTokenAccount.address,
            fromWallet.publicKey,
            [],
            0
          )
        );
      // Sign transaction, broadcast, and confirm
      var signature = await web3.sendAndConfirmTransaction(
        connection,
        transaction,
        [fromWallet]
      );
      console.log("SIGNATURE", signature);
      console.log("SUCCESS");
    })();
    

    【讨论】:

    • 这使用用户的密钥来执行交易,我怎么能用 Phantom 实现同样的事情?
    • 使用Phantom等钱包,可以对当前连接账户为签名者的交易进行签名。
    • 所以这有一个问题,如果该人从未收到过 USDC 代币,这不会为接收者创建 USDC 帐户
    • 由于不同类型的代币存储在不同的关联账户上(从你的主钱包派生),如果用户没有,你应该先创建一个USDC代币的接收方的关联代币账户拥有 USDC 代币。请阅读 solana here 上的相关代币账户计划。
    猜你喜欢
    • 2021-12-28
    • 2021-12-20
    • 2021-12-09
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 2021-12-10
    • 2021-12-05
    • 2022-07-14
    相关资源
    最近更新 更多