paraswap/paraswap-dex-lib

Issues With BalancerV2 Liquidity Bootstrapping Pool Amounts

nlordell opened this issue · 2 comments

I wanted to report an issue we’ve been having with routes including a specific token 0x5c1892bb9e42b4bf89e5b0b21542e2f8622a0bb8. It looks like its using liquidity from the Balancer V2 “Liquidity Bootstrapping Pool” id 0xe9f7be5f534057b7ed5be82553e4da3c7aaac8b40002000000000000000001f9.

I noticed that the price route that is computed usually expects a much higher USDC return over what it is actually receiving. Just speculating, but could it be related to the fact that this pool has a dynamic fee or that the pool has gradual weight updates?

AFAICT, all price routes involving the above LBP are having issues.

Resources:

JS script that causes issue with ParaSwap API
const priceQuery = await fetch(
  "https://api.paraswap.io/prices/?srcToken=0x5C1892Bb9e42B4bf89e5b0B21542e2F8622a0bB8&destToken=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE&amount=5000000000000000000000&srcDecimals=18&destDecimals=18&side=SELL&network=1&otherExchangePrices=true&partner=paraswap.io&userAddress=0xdBC682D0eC19bd276Fd6C2af4E0189B584BB31E1",
);
const { priceRoute } = await priceQuery.json();
console.log("route:", JSON.stringify(priceRoute, undefined, "  "));

const transactionQuery = await fetch("https://api.paraswap.io/transactions/1/?ignoreChecks=true", {
  method: "POST",
  headers: {
    "content-type": "application/json",
  },
  body: JSON.stringify({
    srcToken: "0x5C1892Bb9e42B4bf89e5b0B21542e2F8622a0bB8",
    destToken: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
    srcAmount: "5000000000000000000000",
    destAmount: "1",
    userAddress: "0x30123e49FE5f703b6675943c1520a422D1705DB6",
    priceRoute,
  }),
});
const transaction = await transactionQuery.json();

console.log(`block: ${priceRoute.blockNumber}`);
console.log(`from: ${transaction.from}`);
console.log(`to: ${transaction.to}`);
console.log(`data: ${transaction.data}`);
Example invocation of script 👆 with issue
route: {
  "blockNumber": 14809912,
  "network": 1,
  "srcToken": "0x5c1892bb9e42b4bf89e5b0b21542e2f8622a0bb8",
  "srcDecimals": 18,
  "srcAmount": "5000000000000000000000",
  "destToken": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
  "destDecimals": 18,
  "destAmount": "25844237307996337",
  "bestRoute": [
    {
      "percent": 100,
      "swaps": [
        {
          "srcToken": "0x5c1892bb9e42b4bf89e5b0b21542e2f8622a0bb8",
          "srcDecimals": 18,
          "destToken": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
          "destDecimals": 6,
          "swapExchanges": [
            {
              "exchange": "BalancerV2",
              "srcAmount": "5000000000000000000000",
              "destAmount": "47226070",
              "percent": 100,
              "poolAddresses": [
                "0xe9f7be5f534057b7ed5be82553e4da3c7aaac8b4"
              ],
              "data": {
                "swaps": [
                  {
                    "poolId": "0xe9f7be5f534057b7ed5be82553e4da3c7aaac8b40002000000000000000001f9",
                    "amount": "5000000000000000000000"
                  }
                ],
                "gasUSD": "4.876344"
              }
            }
          ]
        },
        {
          "srcToken": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
          "srcDecimals": 6,
          "destToken": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
          "destDecimals": 18,
          "swapExchanges": [
            {
              "exchange": "BalancerV2",
              "srcAmount": "47226070",
              "destAmount": "25844237307996337",
              "percent": 100,
              "poolAddresses": [
                "0xe7b1d394f3b40abeaa0b64a545dbcf89da1ecb3f"
              ],
              "data": {
                "swaps": [
                  {
                    "poolId": "0xe7b1d394f3b40abeaa0b64a545dbcf89da1ecb3f00010000000000000000009a",
                    "amount": "47226070"
                  }
                ],
                "gasUSD": "4.876344"
              }
            }
          ]
        }
      ]
    }
  ],
  "gasCostUSD": "12.155099",
  "gasCost": "373900",
  "others": [],
  "side": "SELL",
  "tokenTransferProxy": "0x216b4b4ba9f3e719726886d34a177484278bfcae",
  "contractAddress": "0xDEF171Fe48CF0115B1d80b88dc8eAB59176FEe57",
  "contractMethod": "multiSwap",
  "partnerFee": 0,
  "srcUSD": "49.0849782376",
  "destUSD": "52.5105798047",
  "partner": "paraswap.io",
  "maxImpactReached": false,
  "hmac": "9657fc26ecd1d5e1ab2044dfed9cd98fd8ff5ca4"
}
block: 14809912
from: 0xba12222222228d8ba445958a75a0704d566bf2c8
to: 0xDEF171Fe48CF0115B1d80b88dc8eAB59176FEe57
data: 0xa94e78ef00000000000000000000000000000000000000000000000000000000000000200000000000000000000000005c1892bb9e42b4bf89e5b0b21542e2f8622a0bb800000000000000000000000000000000000000000000010f0cf064dd592000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000005bd132683be8b1000000000000000000000000dbc682d0ec19bd276fd6c2af4e0189b584bb31e10000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000bc00000000000000000000000000000000000000000000000000000000062879efae8920f60d81211ec9579bb691cb2090c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000540000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000fc2ba6e830a04c25e207b8214b26d8c713f6881f000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000009000000000000000000000000ba12222222228d8ba445958a75a0704d566bf2c8000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000def171fe48cf0115b1d80b88dc8eab59176fee570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000def171fe48cf0115b1d80b88dc8eab59176fee5700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000260ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020e9f7be5f534057b7ed5be82553e4da3c7aaac8b40002000000000000000001f90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000010f0cf064dd5920000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000005c1892bb9e42b4bf89e5b0b21542e2f8622a0bb8000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000000000027fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000fc2ba6e830a04c25e207b8214b26d8c713f6881f000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000009000000000000000000000000ba12222222228d8ba445958a75a0704d566bf2c8000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000def171fe48cf0115b1d80b88dc8eab59176fee570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000def171fe48cf0115b1d80b88dc8eab59176fee5700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000260ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020e7b1d394f3b40abeaa0b64a545dbcf89da1ecb3f00010000000000000000009a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000002d09cd600000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000027fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000

When simulating with Tenderly (with a fork where I set required allowances), I see that the actual swapped amount is:

image

Despite the route reporting that:

{
  "exchange": "BalancerV2",
  "srcAmount": "5000000000000000000000",
  "destAmount": "47226070",
  "percent": 100,
  "poolAddresses": [
    "0xe9f7be5f534057b7ed5be82553e4da3c7aaac8b4"
  ],
  "data": {
    "swaps": [
      {
        "poolId": "0xe9f7be5f534057b7ed5be82553e4da3c7aaac8b40002000000000000000001f9",
        "amount": "5000000000000000000000"
      }
    ],
    "gasUSD": "4.876344"
  }
}

Which is a 5% difference.

Looking over the code, it looks like weights for LBPs are computed with eth_calls. So it shouldn't matter that the weights are stale on the Subgraph:

Example: image image

@shresthagrawal - Actually I think this might be a more general issue with Balancer weighted pools. We had a price route including this pool where the computed output amount was way off the actual output amount:

Specifically, the price route expects 18092826159713190 ETH from the swap:

              {
                "exchange":"BalancerV2",
                "srcAmount":"31030276",
                "destAmount":"18092826159713190",
                "percent":16,
                "poolAddresses":[
                  "0xe7b1d394f3b40abeaa0b64a545dbcf89da1ecb3f"
                ],
                "data":{
                  "swaps":[
                    {
                      "poolId":"0xe7b1d394f3b40abeaa0b64a545dbcf89da1ecb3f00010000000000000000009a",
                      "amount":"31030276"
                    }
                  ],
                  "gasUSD":"4.422375"
                }
              }

However, when simulating - the output amount is ~22% less and only 14161251454814329 ETH is swapped out:

{
  "[FUNCTION]": "batchSwap",
  "[OPCODE]": "CALL",
  "from": {
    "address": "0xfc2ba6e830a04c25e207b8214b26d8c713f6881f",
    "balance": "0"
  },
  "to": {
    "address": "0xba12222222228d8ba445958a75a0704d566bf2c8",
    "balance": "0"
  },
  "value": "0",
  "input": {
    "kind": "0",
    "swaps": [
      {
        "poolId": "0xe7b1d394f3b40abeaa0b64a545dbcf89da1ecb3f00010000000000000000009a",
        "assetInIndex": "0",
        "assetOutIndex": "1",
        "amount": "31030276"
      }
    ],
    "assets": [
      "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
      "0x0000000000000000000000000000000000000000"
    ],
    "funds": {
      "sender": "0xdef171fe48cf0115b1d80b88dc8eab59176fee57",
      "recipient": "0xdef171fe48cf0115b1d80b88dc8eab59176fee57"
    },
    "limits": [
      "57896044618658097711785492504343953926634992332820282019728792003956564819967",
      "57896044618658097711785492504343953926634992332820282019728792003956564819967"
    ],
    "deadline": "115792089237316195423570985008687907853269984665640564039457584007913129639935"
  },
  "output": {
    "assetDeltas": [
      "31030276",
      "-14161251454814329"
    ]
  },
  "gas": {
    "gas_left": 7181076,
    "gas_used": 105680,
    "total_gas_used": 818924
  }
}

This is for a weighted pool trading USDC-WETH so it seems that the issue is not limited to LBPs.