0

I want to calculate price impact of my order on 0x API. What is the best way to get this value?

This is an example quote to swap 10 BNB to Farm token. Pancakeswap shows 5% price impact for this swap. I think "buyTokenToEthRate" value is so wrong

{
    "chainId": 56,
    "price": "1.829479215679068851",
    "guaranteedPrice": "1.792889631365487474",
    "to": "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
    "data": "0x415565b0000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000004b5c23cac08a567ecf0c1ffca8372a45a5d337430000000000000000000000000000000000000000000000008ac7230489e80000000000000000000000000000000000000000000000000000f8d03c34b635927900000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000000000000000000000000000008ac7230489e800000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000004a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c0000000000000000000000004b5c23cac08a567ecf0c1ffca8372a45a5d3374300000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000460000000000000000000000000000000000000000000000000000000000000046000000000000000000000000000000000000000000000000000000000000004200000000000000000000000000000000000000000000000008ac7230489e8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000250616e63616b65537761705632000000000000000000000000000000000000000000000000000000100fb57ee914055e0000000000000000000000000000000000000000000000001c86c048b7d2b785000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c0000000000000000000000004b5c23cac08a567ecf0c1ffca8372a45a5d337430000000000000000000000000000000250616e63616b655377617056320000000000000000000000000000000000000000000000000000007cb52ab0af49fcea000000000000000000000000000000000000000000000000dfb16ad4b47b3fd1000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000e9e7cea3dedca5984780bafc599bd69add087d560000000000000000000000004b5c23cac08a567ecf0c1ffca8372a45a5d337430000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000003000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c0000000000000000000000004b5c23cac08a567ecf0c1ffca8372a45a5d33743000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000000000000000000000000000000000000000000000869584cd00000000000000000000000010000000000000000000000000000000000000110000000000000000000000000000000000000000000000f14489d189612fcd57",
    "value": "10000000000000000000",
    "gas": "286000",
    "estimatedGas": "286000",
    "gasPrice": "50000000000",
    "protocolFee": "0",
    "minimumProtocolFee": "0",
    "buyTokenAddress": "0x4b5c23cac08a567ecf0c1ffca8372a45a5d33743",
    "sellTokenAddress": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
    "buyAmount": "18294792156790688516",
    "sellAmount": "10000000000000000000",
    "sources": [
        { "name": "BakerySwap", "proportion": "0" },
        { "name": "Belt", "proportion": "0" },
        { "name": "DODO", "proportion": "0" },
        { "name": "DODO_V2", "proportion": "0" },
        { "name": "Ellipsis", "proportion": "0" },
        { "name": "Mooniswap", "proportion": "0" },
        { "name": "MultiHop", "proportion": "0" },
        { "name": "Nerve", "proportion": "0" },
        { "name": "PancakeSwap", "proportion": "0" },
        { "name": "PancakeSwap_V2", "proportion": "1" },
        { "name": "SushiSwap", "proportion": "0" },
        { "name": "Smoothy", "proportion": "0" },
        { "name": "ApeSwap", "proportion": "0" },
        { "name": "CafeSwap", "proportion": "0" },
        { "name": "CheeseSwap", "proportion": "0" },
        { "name": "JulSwap", "proportion": "0" },
        { "name": "LiquidityProvider", "proportion": "0" },
        { "name": "WaultSwap", "proportion": "0" },
        { "name": "FirebirdOneSwap", "proportion": "0" },
        { "name": "JetSwap", "proportion": "0" },
        { "name": "ACryptoS", "proportion": "0" }
    ],
    "orders": [
        {
            "makerToken": "0x4b5c23cac08a567ecf0c1ffca8372a45a5d33743",
            "takerToken": "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c",
            "makerAmount": "2097491529076967842",
            "takerAmount": "1157343185938416990",
            "fillData": { "tokenAddressPath": ["0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c", "0x4b5c23cac08a567ecf0c1ffca8372a45a5d33743"], "router": "0x10ed43c718714eb63d5aa57b78b54704e256024e" },
            "source": "PancakeSwap_V2",
            "sourcePathId": "0xae674b24c79c3e403a77159df86e02e5febbbc42d0e3a1d21328dc1f1c4ef891",
            "type": 0
        },
        {
            "makerToken": "0x4b5c23cac08a567ecf0c1ffca8372a45a5d33743",
            "takerToken": "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c",
            "makerAmount": "16447736737887043871",
            "takerAmount": "8986135569831165162",
            "fillData": {
                "tokenAddressPath": ["0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c", "0xe9e7cea3dedca5984780bafc599bd69add087d56", "0x4b5c23cac08a567ecf0c1ffca8372a45a5d33743"],
                "router": "0x10ed43c718714eb63d5aa57b78b54704e256024e"
            },
            "source": "PancakeSwap_V2",
            "sourcePathId": "0x9d257ae6bf6dcbc31f5fdec0bc5b3a7c97094c5dc2ba5e97a887d5dface5f455",
            "type": 0
        }
    ],
    "allowanceTarget": "0x0000000000000000000000000000000000000000",
    "sellTokenToEthRate": "1",
    "buyTokenToEthRate": "0.009119237695869904"
}
4
  • Showing a single swap transaction isn't going to get anyone to do this for you. I spent months learning how liquidity pools work, where to get the data, and how it relates to a single transaction. Look at the source code for Pancake, look at how they're calculating price impact.
    – dir
    Sep 1, 2021 at 19:34
  • Mate I know how to calculate price impact on Uniswap (Pancakeswap is just a Uniswap clone) but this 0x is a multi-DEX liquidity aggregator and not a AMM
    – M M
    Sep 1, 2021 at 19:45
  • Fair enough, apologies. Well the price impact for an 0x swap is going to be relative to the proportion of impact on each exchange it decides to use.
    – dir
    Sep 1, 2021 at 19:47
  • Yes but API doesn't provide this data. I can calculate price impact by getting 1 BNB rate first and then compare it to the price that I'm getting but this method needs two calls and isn't time efficient
    – M M
    Sep 1, 2021 at 19:50

1 Answer 1

2

There is no concept of "price impact" within API so currently the only way to do this is to (as you guessed) perform two requests: a swap/v1/price for a small fraction of the swap then a swap/v1/quote for the actual swap. I believe this is how Matcha detects extreme slippage to know when to display a warning. You can also use another service (coingecko, etc) as a price oracle and perform the A->USD->B conversion. BSC prices can be extremely volatile though so I'm not sure any of these methods would be 100% reliable.

3
  • Thankyou dear Lawrence. Actually I bumped into another problem while I was trying 0x API. Sometimes it returns wrong price. For example take a look at these two quotes. Try swapping one BNB to Alpaca with Pancakeswap UI and 0x. Right now Pancakeswap UI returns 394 Alpaca for 1 BNB but 0x rate is 372! It's about 6% price difference and gets worse if you increase swap size bsc.api.0x.org/swap/v1/…
    – M M
    Sep 1, 2021 at 20:45
  • 1
    Yeah, that's weird. Flagging this issue and will get back to you once resolved. Sep 2, 2021 at 16:02
  • 1
    0xa1faa113cbe53436df28ff0aee54275c13b40975 is ALPHA, not ALPACA 😉. You want 0x8f0528ce5ef7b51152a59745befdd91d97091d2f Sep 2, 2021 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.