Pārlūkot izejas kodu

fix: 使用 Byreal 返回的 priceUsd 替代 Jupiter 价格查询

Jupiter price API 返回 401,改用 calculatePosition 接口已返回的 priceUsd 字段计算持仓价值,移除 getTokenPrices 调用

Co-authored-by: Cursor <cursoragent@cursor.com>
zhangchunrui 1 mēnesi atpakaļ
vecāks
revīzija
1da848a294
2 mainītis faili ar 10 papildinājumiem un 4 dzēšanām
  1. 5 2
      src/core/sniper.js
  2. 5 2
      test-copy.js

+ 5 - 2
src/core/sniper.js

@@ -100,12 +100,15 @@ export class SniperEngine {
     logger.info(`  Estimated Total Value: ${formatUsd(calculation.estimatedValue)}`);
 
     const BUFFER_PCT = 1.2;
-    const prices = await this.swapper.getTokenPrices([tokenA.address, tokenB.address]);
 
     const needSwapUsd = async (token, valueUsd) => {
       const requiredWithBuffer = valueUsd * BUFFER_PCT;
       const balance = await this.swapper.getTokenBalance(token.address);
-      const price = prices[token.address]?.price ?? 0;
+      const price = parseFloat(token.priceUsd) || 0;
+      if (price <= 0) {
+        logger.warn(`Token ${token.symbol || token.address} priceUsd is ${token.priceUsd}, cannot calculate balance value, will swap full amount`);
+        return { skip: false, swapUsd: requiredWithBuffer, currentValueUsd: 0, requiredWithBuffer };
+      }
       const currentValueUsd = balance * price;
       if (currentValueUsd >= requiredWithBuffer) {
         return { skip: true, currentValueUsd, requiredWithBuffer };

+ 5 - 2
test-copy.js

@@ -127,12 +127,15 @@ async function testCopy() {
   const tokenA = calculation.tokenA;
   const tokenB = calculation.tokenB;
   const BUFFER_PCT = 1.2;
-  const prices = await swapper.getTokenPrices([tokenA.address, tokenB.address]);
 
   const needSwapUsd = async (token, valueUsd) => {
     const requiredWithBuffer = valueUsd * BUFFER_PCT;
     const balance = await swapper.getTokenBalance(token.address);
-    const price = prices[token.address]?.price ?? 0;
+    const price = parseFloat(token.priceUsd) || 0;
+    if (price <= 0) {
+      console.log(`  Warning: Token ${token.symbol || token.address} priceUsd is ${token.priceUsd}, will swap full amount`);
+      return { skip: false, swapUsd: requiredWithBuffer, currentValueUsd: 0, requiredWithBuffer };
+    }
     const currentValueUsd = balance * price;
     if (currentValueUsd >= requiredWithBuffer) {
       return { skip: true, currentValueUsd, requiredWithBuffer };