Defi

0x gas 估計有多準確?

  • September 1, 2021

我正在嘗試通過“swap/v1/quote”方法從 0x 獲取交換的氣體估計值,但在大多數情況下,欄位gasestimatedGas錯誤。

有什麼方法可以更準確地計算它嗎?

例如:

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

回复:

{
   "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"
}

Out of Gas但是即使有 250,000 的 gas,交易也異常失敗。(連結:https ://etherscan.io/tx/0xe95a629944aba94ba4e32be41467506cbb9b9cca34de38426d8bd5c8d8a9e311 )

如果為 API 呼叫提供了相同skipValidation=truetakerAddress=<address>問題。

有什麼方法可以得到更準確的結果嗎?

不同的代幣在轉移時可能會花費不同的金額,即在交換期間。0xAPI 不知道存在的每個代幣的 gas 成本。

0xAPI 會在以下情況下為您進行更準確的 gas 估算:

  1. takerAddress提供和skipValidation=false(預設)
  2. Taker必須有資金和津貼

沒有上述內容,我們無法給出準確的 gas 估計,因為交易需要在估計期間成功。當我們沒有上述條件時,我們會提供大致的 gas 成本。

eth_estimateGas此外,您可以通過使用 0xAPI 響應提供的數據對您的乙太坊節點執行 an 來自己測量氣體。

引用自:https://ethereum.stackexchange.com/questions/108171