balancer / backend

MIT License
20 stars 9 forks source link

Wrong SOR return amount for Gyro pools. #929

Closed gmbronco closed 1 month ago

gmbronco commented 1 month ago

Problem

SOR returns incorrect returnAmounts when queryBatchSwap is set to false.

Example Query for USDT to Aave USDT using Gyro2 Pool

Endpoint: api-v3.balancer.fi

{
  sorGetSwapPaths(
    swapAmount: "1",
    swapType: EXACT_IN,
    tokenIn: "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9",
    tokenOut: "0xb165a74407fe1e519d6bcbdec1ed3202b35a4140",
    chain: ARBITRUM,
    queryBatchSwap: true
  ) {
    routes {
      tokenInAmount
      tokenOutAmount
      hops {
        tokenIn
        tokenInAmount
        tokenOut
        tokenOutAmount
        poolId
      }
    }
    paths {
      inputAmountRaw
      outputAmountRaw
      tokens {
        address
      }
    }
    effectivePrice
    swapType
    swapAmount
    returnAmount
  }
}

Result

{
  "data": {
    "sorGetSwapPaths": {
      "routes": [
        {
          "tokenInAmount": "1",
          "tokenOutAmount": "1",
          "hops": [
            {
              "tokenIn": "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9",
              "tokenInAmount": "1",
              "tokenOut": "0xb165a74407fe1e519d6bcbdec1ed3202b35a4140",
              "tokenOutAmount": "1",
              "poolId": "0x14abd18d1fa335e9f630a658a2799b33208763fa00020000000000000000051f"
            }
          ]
        }
      ],
      "paths": [
        {
          "inputAmountRaw": "1000000",
          "outputAmountRaw": "999981",
          "tokens": [
            {
              "address": "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9"
            },
            {
              "address": "0xb165a74407fe1e519d6bcbdec1ed3202b35a4140"
            }
          ]
        }
      ],
      "effectivePrice": "1.135246",
      "swapType": "EXACT_IN",
      "swapAmount": "1",
      "returnAmount": "0.880866"
    }
  }
}

Notes

Potential Issue

The problem might be related to this function: gyro2Math.ts#L4-L19

franzns commented 1 month ago

has this been fixed with the fix for the 2clp rates?

mkflow27 commented 1 month ago

I am still looking into it.