[英]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.