我正在尝试从用户钱包进行 spl 代币转账。下面的代码在开发网络上完美运行,但抛出
Transaction simulation failed: Error processing Instruction 0: incorrect program id for instruction
。它创建了一个关联的令牌帐户,但指令失败。
如果solana的代币程序id永远不会改变,程序id怎么会不正确?
let mint = new web3.PublicKey(mintAccount)
let toWallet = new web3.PublicKey("ADDRESS")
let owner = new web3.PublicKey(fromWallet.publicKey)
const createTransaction = async (mint, minerWallet, owner) => {
const transaction = new web3.Transaction()
const destinationAccount = await splToken.Token.getAssociatedTokenAddress(
splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
splToken.TOKEN_PROGRAM_ID,
mint,
toWallet,
false
)
const sourceAccount = await splToken.Token.getAssociatedTokenAddress(
splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
splToken.TOKEN_PROGRAM_ID,
mint,
owner,
false
)
console.log("sending from " + sourceAccount.toBase58() + "to " + destinationAccount.toBase58())
transaction.feePayer = fromWallet.publicKey
let blockhashObj = await connection.getRecentBlockhash();
transaction.recentBlockhash = blockhashObj.blockhash;
let signature = '';
try {
const destinationAccountInfo = await connection.getAccountInfo(destinationAccount)
const destTokenAccountMissing = !destinationAccountInfo
if (destTokenAccountMissing) {
console.log("creating token account cuz it needs one")
transaction.add(
splToken.Token.createAssociatedTokenAccountInstruction(
splToken.ASSOCIATED_TOKEN_PROGRAM_ID, // always associated token program id
splToken.TOKEN_PROGRAM_ID, // always token program id
mint, // mint (which we used to calculate ata)
destinationAccount, // the ata we calcualted early
toWallet, // token account owner (which we used to calculate ata)
fromWallet.publicKey // payer, fund account, like SystemProgram.createAccount's from
)
);
}
transaction.add(
splToken.Token.createTransferInstruction(
splToken.TOKEN_PROGRAM_ID,
sourceAccount,
destinationAccount,
fromWallet.publicKey,
[],
1
)
)
signature = await sendTransaction(transaction, connection);
console.log('info', 'Transaction sent:', signature)
await connection.confirmTransaction(signature, 'processed');
console.log('success', 'Transaction successful!', signature);
return true
} catch (error) {
console.log('error', `Transaction failed! ${error?.message}`, signature);
return false
}
}
IncorrectProgramId
表示该帐户的所有者不是预期的 spl 代币计划。
就您而言,可能是
sourceAccount
尚未在主网上创建。
确保您的
lib.rs
中的程序 ID(如果您使用的是 Anchor)正确无误,并且该程序 ID 与 Anchor.toml 中提供的程序 ID 相匹配。
还可以打印您的提供商中的程序 ID,以交叉检查您的 Typescript 前端是否确实使用了正确的程序。
如果您使用锚框架,您可以通过从存储
solana-address -k target/deploy/program-keypair.json
的目录中运行 Anchor.toml
来获取程序思想(将插入到上述所有位置!)。
我遇到同样的情况,因为我忘记将钱包切换到devnet模式,这是非常基本的问题,只需检查destinationAccount/source帐户是否已成功导出