简体   繁体   English

如何从连接的钱包地址在 solana 中创建关联的代币地址?

[英]How can I create a associated token address in solana from connected wallet address?

I want to create an associated token address in Solana from a connected wallet address.我想从连接的钱包地址在 Solana 中创建一个关联的令牌地址。 This is my code currently:这是我目前的代码:

import { useWallet, useConnection } from "@solana/wallet-adapter-react";

 ... 

  const wallet = useWallet();

  const { publicKey, sendTransaction } = wallet;

const associatedAccount = await getAssociatedTokenAddress(
            mintPubkey,
            wallet.publicKey
          );
          const transaction = new Transaction().add(
            createAssociatedTokenAccountInstruction(
              wallet.publicKey,
              associatedAccount,
              wallet.publicKey,
              mintPubkey,
              TOKEN_2022_PROGRAM_ID,
              ASSOCIATED_TOKEN_PROGRAM_ID
            )
          );
       signature = await sendTransaction(transaction, connection, {
          skipPreflight: true,     
        });

        await connection.confirmTransaction(signature, "confirmed");

...

But the tx is keep failing like this: https://solscan.io/tx/ZaQtfVkNkkAweGug2JfDYz8hcFXh4jDUEJoz2Lik6jPv8huFgzpMXMwui51VJsk8yHmpcWUD6UgnqdmRhu4Covh?cluster=devnet How can I achieve this?但是 tx 一直像这样失败: https://solscan.io/tx/ZaQtfVkNkkAweGug2JfDYz8hcFXh4jDUEJoz2Lik6jPv8huFgzpMXMwui51VJsk8yHmpcWUD6UgnqdmRhu4Covh?cluster=devnet ://solscan.io/tx/ZaQtfVkNkkAweGug2JfDYz8hcFXh4jDUEJoz2Lik6jPv8huFgzpMXMwui51VJsk8yHmpcWUD6UgnqdmRhu4Covh?cluster=devnet 我怎样才能做到这一点?

Because you use Token-2022, you need to specify it when generating the associatedAccount address.因为使用了Token-2022,所以需要在生成associatedAccount地址的时候指定。 Otherwise it uses the default Token Program.否则它使用默认的令牌程序。

Documentation: getAssociatedTokenAddress() 文档:getAssociatedTokenAddress()

const associatedAccount = await getAssociatedTokenAddress(
   mintPubkey,
   wallet.publicKey,
   false,
   TOKEN_2022_PROGRAM_ID,
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取anchor solana中给定令牌地址的数量? - how to get the the amount of the given token address in anchor solana? 如何获取给定 herotag 的钱包地址? - How can I get the wallet address for a given herotag? 如何在没有私钥的情况下将我的元掩码钱包中的 erc20 代币发送到其他地址? - How to send erc20 token from my metamask wallet to other address without private key? 如何以编程方式从 Solana 钱包转移 NFT - How to transfer NFTs from Solana Wallet programmatically 如何从 Solana 的助记词中获取正确的公共地址? - How to get proper public address from mnemonic phrase for Solana? 如何使用 @solana/web3.js 从 Solana 中的自定义令牌中删除铸币权限? - How do I remove the minting authority from my custom token in Solana using @solana/web3.js? 如何使用 solana web3 js 在 solana 中获取令牌的符号/名称? - how can i get the symbol/name of a token in solana using solana web3 js? 无法在 Solana-web3 中使用助记词导出 Sollet 钱包地址 - Unable to derive Sollet wallet address using mnemonic phrases in Solana-web3 给定一个钱包地址,你如何以编程方式获得它持有的 ERC 代币? - Given a Wallet Address, How do you get the ERC token it holds programatically? Solana - 如何从我的 Phantom 钱包中获取余额? - Solana - How to get the balance from my Phantom wallet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM