![](/img/trans.png)
[英]how to get the the amount of the given token address in anchor solana?
[英]How can I create a associated token address in solana from connected wallet address?
我想從連接的錢包地址在 Solana 中創建一個關聯的令牌地址。 這是我目前的代碼:
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");
...
但是 tx 一直像這樣失敗: https://solscan.io/tx/ZaQtfVkNkkAweGug2JfDYz8hcFXh4jDUEJoz2Lik6jPv8huFgzpMXMwui51VJsk8yHmpcWUD6UgnqdmRhu4Covh?cluster=devnet
://solscan.io/tx/ZaQtfVkNkkAweGug2JfDYz8hcFXh4jDUEJoz2Lik6jPv8huFgzpMXMwui51VJsk8yHmpcWUD6UgnqdmRhu4Covh?cluster=devnet 我怎樣才能做到這一點?
因為使用了Token-2022,所以需要在生成associatedAccount
地址的時候指定。 否則它使用默認的令牌程序。
文檔:getAssociatedTokenAddress()
const associatedAccount = await getAssociatedTokenAddress(
mintPubkey,
wallet.publicKey,
false,
TOKEN_2022_PROGRAM_ID,
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.