Răsfoiți Sursa

fix: query copied position instead of parent position

- onchain-monitor: Find and query the target's copied position from Byreal API
- sniper: Use target's totalDeposit for copy amount, parent detail for copy API
- Fix bug where copy amount was based on parent position value instead of target's actual deposit

The correct flow:
1. Detect transaction and extract parent position address
2. Wait 60s for Byreal API to sync
3. Query target wallet positions to find the copied position
4. Get copied position details (target's actual deposit amount)
5. Get parent position details (for NFT mint and copy API)
6. Execute copy using target's deposit amount as base
lushdog@outlook.com 1 lună în urmă
părinte
comite
a6575f9ff7
3 a modificat fișierele cu 47 adăugiri și 18 ștergeri
  1. 22 9
      src/core/sniper.js
  2. 0 1
      src/services/byreal.js
  3. 25 8
      src/services/onchain-monitor.js

+ 22 - 9
src/core/sniper.js

@@ -247,13 +247,24 @@ export class SniperEngine {
       return;
       return;
     }
     }
 
 
-    logger.info(`Position details from Byreal API:`);
+    logger.info(`Target copied position details from Byreal API:`);
     logger.info(`  Pool: ${poolInfo.mintA?.symbol || '?'}/${poolInfo.mintB?.symbol || '?'}`);
     logger.info(`  Pool: ${poolInfo.mintA?.symbol || '?'}/${poolInfo.mintB?.symbol || '?'}`);
-    logger.info(`  NFT Mint: ${positionDetail.nftMintAddress || positionDetail.nftMint}`);
-    logger.info(`  Total Deposit: $${positionDetail.totalDeposit || 0}`);
-    logger.info(`  Liquidity: ${positionDetail.liquidity}`);
-    logger.info(`  Price Range: ${positionDetail.priceLower} - ${positionDetail.priceUpper}`);
-    logger.info(`  Current Price: ${positionDetail.currentPrice}`);
+    logger.info(`  Target NFT Mint: ${positionDetail.nftMintAddress || positionDetail.nftMint}`);
+    logger.info(`  Target Total Deposit: $${positionDetail.totalDeposit || 0}`);
+    logger.info(`  Parent Position: ${positionDetail.parentPositionAddress || parentPositionAddress}`);
+
+    // Get parent position detail for copy operation
+    logger.info(`Fetching parent position detail for copy operation...`);
+    const parentDetail = await ByrealAPI.getPositionDetail(parentPositionAddress);
+    
+    if (!parentDetail) {
+      logger.error(`Failed to fetch parent position detail: ${parentPositionAddress}`);
+      return;
+    }
+
+    logger.info(`Parent position details:`);
+    logger.info(`  Parent NFT Mint: ${parentDetail.nftMintAddress || parentDetail.nftMint}`);
+    logger.info(`  Parent Total Deposit: $${parentDetail.totalDeposit || 0}`);
 
 
     // Check 3: API check via CHECK_COPY_URL (as fallback)
     // Check 3: API check via CHECK_COPY_URL (as fallback)
     logger.info(`Checking copy status via API...`);
     logger.info(`Checking copy status via API...`);
@@ -274,13 +285,15 @@ export class SniperEngine {
 
 
     logger.success('Parent position ready to copy!');
     logger.success('Parent position ready to copy!');
 
 
-    // Execute copy using position detail from Byreal API
+    // Execute copy using:
+    // - parentDetail: for NFT mint and calculate API
+    // - positionDetail.totalDeposit: for target's deposit amount (copy amount base)
     const success = await this.executeCopy(
     const success = await this.executeCopy(
-      positionDetail,
+      parentDetail,
       poolInfo,
       poolInfo,
       parentPositionAddress,
       parentPositionAddress,
       targetPositionAddress,
       targetPositionAddress,
-      positionDetail.totalDeposit || 0 // Use totalDeposit from Byreal API
+      positionDetail.totalDeposit || 0 // Use TARGET's totalDeposit from Byreal API
     );
     );
 
 
     if (success) {
     if (success) {

+ 0 - 1
src/services/byreal.js

@@ -18,7 +18,6 @@ export class ByrealAPI {
       if (response.data.retCode !== 0) {
       if (response.data.retCode !== 0) {
         throw new Error(response.data.retMsg || 'Failed to fetch positions');
         throw new Error(response.data.retMsg || 'Failed to fetch positions');
       }
       }
-
       return response.data.result?.data || { positions: [], poolMap: {} };
       return response.data.result?.data || { positions: [], poolMap: {} };
     } catch (error) {
     } catch (error) {
       logger.error('Error fetching target positions:', error.message);
       logger.error('Error fetching target positions:', error.message);

+ 25 - 8
src/services/onchain-monitor.js

@@ -160,27 +160,44 @@ export class OnchainMonitor {
       logger.info(`Waiting 60 seconds for Byreal API to sync...`);
       logger.info(`Waiting 60 seconds for Byreal API to sync...`);
       await sleep(60000);
       await sleep(60000);
 
 
-      // Fetch position details from Byreal API
-      logger.info(`Fetching position details from Byreal API...`);
-      const positionDetail = await ByrealAPI.getPositionDetail(parentPosition);
+      // Fetch target wallet's positions to find the copied position
+      logger.info(`Fetching target wallet positions to find copied position...`);
+      const { positions } = await ByrealAPI.fetchTargetPositions(CONFIG.TARGET_WALLET);
+      
+      // Find the position that references this parent position
+      const copiedPosition = positions.find(p => 
+        p.parentPositionAddress === parentPosition
+      );
+
+      if (!copiedPosition) {
+        logger.error(`Could not find copied position for parent ${parentPosition} in target wallet`);
+        return;
+      }
+
+      logger.info(`Found copied position: ${copiedPosition.positionAddress}`);
+
+      // Fetch the copied position details (this is the position created by target wallet)
+      logger.info(`Fetching copied position details from Byreal API...`);
+      const positionDetail = await ByrealAPI.getPositionDetail(copiedPosition.positionAddress);
 
 
       if (!positionDetail) {
       if (!positionDetail) {
-        logger.error(`Failed to fetch position detail from Byreal API: ${parentPosition}`);
+        logger.error(`Failed to fetch copied position detail from Byreal API: ${copiedPosition.positionAddress}`);
         return;
         return;
       }
       }
 
 
-      logger.success(`Position details fetched successfully!`);
+      logger.success(`Copied position details fetched successfully!`);
       logger.info(`  Pool: ${positionDetail.pool?.mintA?.symbol}/${positionDetail.pool?.mintB?.symbol}`);
       logger.info(`  Pool: ${positionDetail.pool?.mintA?.symbol}/${positionDetail.pool?.mintB?.symbol}`);
       logger.info(`  NFT Mint: ${positionDetail.nftMintAddress || positionDetail.nftMint}`);
       logger.info(`  NFT Mint: ${positionDetail.nftMintAddress || positionDetail.nftMint}`);
-      logger.info(`  USD Value: $${positionDetail.totalDeposit || positionDetail.totalUsdValue || positionDetail.liquidityUsd || 0}`);
+      logger.info(`  Total Deposit (Target's amount): $${positionDetail.totalDeposit || positionDetail.totalUsdValue || positionDetail.liquidityUsd || 0}`);
+      logger.info(`  Parent Position: ${positionDetail.parentPositionAddress}`);
 
 
       // Call the callback with position info from Byreal API
       // Call the callback with position info from Byreal API
       if (this.callback) {
       if (this.callback) {
         await this.callback({
         await this.callback({
           parentPositionAddress: parentPosition,
           parentPositionAddress: parentPosition,
-          targetPositionAddress: signature,
+          targetPositionAddress: copiedPosition.positionAddress, // Target's copied position
           transactionSignature: signature,
           transactionSignature: signature,
-          positionDetail: positionDetail // Full position detail from Byreal API
+          positionDetail: positionDetail // Full position detail from Byreal API (TARGET's position)
         });
         });
       }
       }