网站搜索

typescript - 代币交换错误 0x26 InvalidSplTokenProgram raydium-sdk-V2-demo


我正在尝试使用 Raydium SDK 和 GPT 在 Solana 上执行代币交换,但在尝试发送交易时总是遇到错误。该错误与 InvalidSplTokenProgram 有关,并且模拟失败,日志输出如下:

SendTransactionError: Simulation failed. 
Message: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x26.
Logs:
[ 
  "Program routeUGWgWzqBWFcrCfv8tritsqukccJPu3q5GPP3xS invoke [1]",
  "Program log: process_swap_base_in_with_user_account:RouteSwapBaseInArgs { amount_in: 10000000, minimum_amount_out: 11756 }",
  "Program 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8 invoke [2]",
  "Program log: Error: InvalidSplTokenProgram",
  "Program 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8 failed: custom program error: 0x26",
  "Program routeUGWgWzqBWFcrCfv8tritsqukccJPu3q5GPP3xS failed: custom program error: 0x26"
]

我采取的步骤:

  1. 我配置了交换的输入和输出铸币地址(SOL 到代币)。
  2. 我成功地从交换服务和资金池服务请求到了数据,收到了预期的路由和资金池数据。
  3. 我创建了交易,并使用 Raydium SDK 的 swapBaseInAutoAccount() 函数添加了交换指令。
  4. 交易模拟因上述错误而失败。

这是我代码的简化版本:

import {
  ALL_PROGRAM_ID,
  API_URLS,
  ApiSwapV1Out,
  getATAAddress,
  swapBaseInAutoAccount
} from '@raydium-io/raydium-sdk-v2';
import { NATIVE_MINT, TOKEN_2022_PROGRAM_ID } from '@solana/spl-token';
import { PublicKey, Transaction } from '@solana/web3.js';
import axios from 'axios';
import { connection, owner } from '../config';

const TOKEN_MINT_ADDRESS = "BfuPnXM7Qs46ZLuptSAHhmnoZ7sa8cpnfJnhTj81fKTg";

export const apiSwap = async () => {
  try {
    const inputMint = NATIVE_MINT.toBase58();
    const outputMint = TOKEN_MINT_ADDRESS;

    const amount = 10000000; // 0.01 SOL in Lamports
    const slippage = 0.5;
    const txVersion: 'LEGACY' | 'VO' = 'LEGACY';

    const { data: swapResponse } = await axios.get<ApiSwapV1Out>(`${API_URLS.SWAP_HOST}/compute/swap-base-out?inputMint=${inputMint}&outputMint=${outputMint}&amount=${amount}&slippageBps=${slippage * 100}&txVersion=${txVersion}`);
    const res = await axios.get(API_URLS.BASE_HOST + API_URLS.POOL_KEY_BY_ID + `?ids=${swapResponse.data.routePlan.map((r) => r.poolId).join(',')}`);

    const inputAccount = (await getATAAddress(owner.publicKey, new PublicKey(inputMint), TOKEN_2022_PROGRAM_ID)).publicKey;

// I created the output ata manually with solana spl-token cli 
    const outputAccount = new PublicKey("7TMMYtCcT4qSrfFZWfC2G7CQZ9Q5a1XxZNsvuNk7TESD");

    const tx = new Transaction();

    const ins = swapBaseInAutoAccount({
      programId: ALL_PROGRAM_ID.Router,
      wallet: owner.publicKey,
      amount: new BN(amount),
      inputAccount,
      outputAccount,
      routeInfo: swapResponse,
      poolKeys: res.data.data,
    });

    let { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash('finalized');
    tx.add(ins);
    tx.feePayer = owner.publicKey;
    tx.recentBlockhash = blockhash;

    tx.sign(owner);

    const signature = await connection.sendTransaction(tx, [owner], { skipPreflight: false, preflightCommitment: 'finalized' });
    await connection.confirmTransaction({ signature, blockhash, lastValidBlockHeight }, 'finalized');
  } catch (error) {
    console.error('Error executing the swap:', error);
  }
};

apiSwap();

错误详情: 错误类型:SendTransactionError 模拟失败:处理指令 0 时出错:自定义程序错误:0x26 程序日志:错误:InvalidSplTokenProgram

我已经检查了输入,包括铸币地址、输入和输出账户以及交换服务的响应。一切看起来都正确。

我尝试过的操作:我验证了铸币地址对于代币程序是有效的。我确认了代币账户是为正确的铸币类型创建的。我检查了输入和输出代币的关联代币账户(ATA)是否正确。

问题:InvalidSplTokenProgram 错误是什么意思,我该如何解决?这可能是代币程序版本问题或配置不匹配吗?在进行此交换交易时,我是否忽略了任何步骤或配置?

答案#1

尝试使用 TOKEN_PROGRAM_ID 而不是 TOKEN_2022_PROGRAM_ID,从我所看到的,你的代币附加在旧的上 https://solscan.io/token/BfuPnXM7Qs46ZLuptSAHhmnoZ7sa8cpnfJnhTj81fKTg