0

I'm trying to get gas estimations for a swap from 0x via 'swap/v1/quote' method, but fields gas and estimatedGas are wrong for the most of the cases.

Is there any way to calculate it more properly?

For example:

Request: https://api.0x.org/swap/v1/quote?sellToken=0x6B3595068778DD592e39A122f4f5a5cF09C90fE2&buyToken=0xb9871cB10738eADA636432E86FC0Cb920Dc3De24&sellAmount=1000000000000000000

Response:

{
    "chainId": 1,
    "price": "3.356694278670382019",
    "guaranteedPrice": "3.323127335883678198",
    "to": "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
    "data": "0xd9627aa400000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000002e1e1eb48ebca1f6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000006b3595068778dd592e39a122f4f5a5cf09c90fe2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000b9871cb10738eada636432e86fc0cb920dc3de24869584cd000000000000000000000000100000000000000000000000000000000000001100000000000000000000000000000000000000000000005ed51c644c612f5b7e",
    "value": "0",
    "gas": "171000",
    "estimatedGas": "171000",
    "gasPrice": "76000000000",
    "protocolFee": "0",
    "minimumProtocolFee": "0",
    "buyTokenAddress": "0xb9871cb10738eada636432e86fc0cb920dc3de24",
    "sellTokenAddress": "0x6b3595068778dd592e39a122f4f5a5cf09c90fe2",
    "buyAmount": "3356694278670382019",
    "sellAmount": "1000000000000000000",
    "sources": [
        ...
        {
            "name": "Uniswap_V2",
            "proportion": "1"
        },
        ...
    ],
    "orders": [
        {
            "makerToken": "0xb9871cb10738eada636432e86fc0cb920dc3de24",
            "takerToken": "0x6b3595068778dd592e39a122f4f5a5cf09c90fe2",
            "makerAmount": "3356694278670382019",
            "takerAmount": "1000000000000000000",
            "fillData": {
                "tokenAddressPath": [
                    "0x6b3595068778dd592e39a122f4f5a5cf09c90fe2",
                    "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
                    "0xb9871cb10738eada636432e86fc0cb920dc3de24"
                ],
                "router": "0xf164fc0ec4e93095b804a4795bbe1e041497b92a"
            },
            "source": "Uniswap_V2",
            "sourcePathId": "0xe30e17bcf3f2833e73f760a19a94f03dd79ca1f583c2893c5eb69673bad00ec9",
            "type": 0
        }
    ],
    "allowanceTarget": "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
    "sellTokenToEthRate": "277.53194847477435333",
    "buyTokenToEthRate": "940.66817609680155706"
}

But transaction was failed with an Out of Gas exception even with 250,000 of gas. (Link: https://etherscan.io/tx/0xe95a629944aba94ba4e32be41467506cbb9b9cca34de38426d8bd5c8d8a9e311)

Same issue happens in case if skipValidation=true and takerAddress=<address> provided for the API call.

Is there any way to get more accurate results?

1 Answer 1

2

Different tokens can cost different amounts when transferred, i.e during a swap. 0xAPI doesn't know the gas cost of every token in existence.

0xAPI will perform a more accurate gas estimate for you under the following circumstances:

  1. takerAddress is provided and skipValidation=false (default)
  2. Taker must have the funds and the allowance

Without the above we can't give an accurate gas estimate as the transaction needs to succeed during estimation. When we do not have the above we provide an approximate gas cost.

Also, you can measure the gas yourself by performing an eth_estimateGas to your Ethereum node with the data provided from the 0xAPI response.

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.