使用 @Solana\web3.js 传输 SPL 令牌
Posted
技术标签:
【中文标题】使用 @Solana\\web3.js 传输 SPL 令牌【英文标题】:Transferring SPL Token using @Solana\web3.js使用 @Solana\web3.js 传输 SPL 令牌 【发布时间】:2021-12-20 12:14:49 【问题描述】:我正在尝试传输 SPL 令牌并从函数中收到错误 mintToken.getOrCreateAssociatedAccountInfo(wallet.publicKey);
错误: "无效的种子,地址必须偏离曲线"
我的钱包变量是一个 AnchorWallet
ToWallet 获取途径: var toWallet = anchor.web3.Keypair.fromSecretKey(DEMO_TO_WALLET);
try
if (wallet)
const mintPublicKey = new anchor.web3.PublicKey("Token address");
const mintToken = new Token(
props.connection,
mintPublicKey,
TOKEN_PROGRAM_ID,
toWallet
);
const fromTokenAccount = await mintToken.getOrCreateAssociatedAccountInfo(
wallet.publicKey
);
const destPublicKey = new anchor.web3.PublicKey(toWallet);
// Get the derived address of the destination wallet which will hold the custom token
const associatedDestinationTokenAddr = await Token.getAssociatedTokenAddress(
mintToken.associatedProgramId,
mintToken.programId,
mintPublicKey,
destPublicKey
);
const receiverAccount = await props.connection.getAccountInfo(associatedDestinationTokenAddr);
const instructions: anchor.web3.TransactionInstruction[] = [];
if (receiverAccount === null)
instructions.push(
Token.createAssociatedTokenAccountInstruction(
mintToken.associatedProgramId,
mintToken.programId,
mintPublicKey,
associatedDestinationTokenAddr,
destPublicKey,
wallet.publicKey
)
)
instructions.push(
Token.createTransferInstruction(
TOKEN_PROGRAM_ID,
fromTokenAccount.address,
associatedDestinationTokenAddr,
wallet.publicKey,
[],
1
)
);
const transaction = new anchor.web3.Transaction().add(...instructions);
transaction.feePayer = wallet.publicKey;
transaction.recentBlockhash = (await props.connection.getRecentBlockhash()).blockhash;
const transactionSignature = await props.connection.sendRawTransaction(
transaction.serialize(),
skipPreflight: true
);
await props.connection.confirmTransaction(transactionSignature);
【问题讨论】:
【参考方案1】:请确保wallet.publicKey
包含有效值。
console.log(wallet.publicKey);//I think this might be an empty string.
const fromTokenAccount = await mintToken.getOrCreateAssociatedAccountInfo(
wallet.publicKey
);
【讨论】:
以上是关于使用 @Solana\web3.js 传输 SPL 令牌的主要内容,如果未能解决你的问题,请参考以下文章
使用 @solana/web3.js 将元数据添加到 Solana 令牌
如何使用 @solana/web3.js 从 Solana 中的自定义令牌中删除铸币权限?
如何获取在与@solana/web3.js 的交易中转移的自定义令牌数量?