0x

如何計算對 0x API 的價格影響

  • October 31, 2021

我想計算我的訂單對 0x API 的價格影響。獲得此值的最佳方法是什麼?

這是將 10 BNB 兌換為 Farm 代幣的範例報價。Pancakeswap 顯示此掉期對價格的影響為 5%。我認為“buyTokenToEthRate”的價值是錯誤的

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

API 中沒有“價格影響”的概念,因此目前唯一的方法是(如您所料)執行兩個請求:一個swap/v1/price用於一小部分交換,然後一個swap/v1/quote用於實際交換。我相信這就是 Matcha 檢測極端滑點以了解何時顯示警告的方式。您還可以使用其他服務(coingecko 等)作為價格預言機並執行 A->USD->B 轉換。BSC 價格可能非常不穩定,所以我不確定這些方法中的任何一種是否 100% 可靠。

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