首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UniswapV3:商函数quoteExactInput & quoteExactOutput不像预期的那样工作

UniswapV3:商函数quoteExactInput & quoteExactOutput不像预期的那样工作
EN

Ethereum用户
提问于 2021-10-06 16:18:43
回答 2查看 1.2K关注 0票数 0

当我试图计算以下路径的输出量时,函数QuoteExactInput给了我错误的结果:('WETH',3000,'cDAI',500,'DAI')

它实际上还"85.86221207戴为一文“什么是绝对错误的。

函数QuoteExactOutput抛出一个错误..。

我不明白,因为代码非常适合反向路径('DAI',500,'cDAI',3000,'WETH')。

请复制并粘贴下面的代码并试用:

代码语言:javascript
复制
from web3 import Web3, HTTPProvider
from eth_abi.packed import encode_single_packed, encode_abi_packed
import json

w3 = Web3(HTTPProvider("https://mainnet.infura.io/v3/1c74d7c6bf64453dadf097e9b1363763"))
w3.eth.handleRevert = True

quoter_contract_address = "0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6"

    
def load_abi(filename): 
    abi_file = open(filename)
    abi_json = abi_file.read()
    abi_file.close()
    abi = json.loads(abi_json)
    return abi
quoter_abi = load_abi('./abis/IQuoter.json')

quoter_contract =w3.eth.contract(address=w3.toChecksumAddress(quoter_contract_address), abi=quoter_abi)

weth_token = { 
    "address" : "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", 
    "symbol" : "WETH", 
    "name" : "Wrapped Ether",
    "decimals" : 18 
    }
dai_token = { 
    "address" :"0x6B175474E89094C44Da98b954EedeAC495271d0F", 
    "symbol" : "DAI", 
    "name" : "DAI", 
    "decimals" : 18 
    }
cdai_token = { 
    "address" : "0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643", 
    "symbol" : "cDAI", 
    "name" : "Compound DAI", 
    "decimals" : 8 
    }

#This works
print()
print("This works")

#Route: ('DAI', 500, 'cDAI', 3000, 'WETH')
route_types = ['address', 'uint24', 'address', 'uint24', 'address']
route_tuple = (str(dai_token["address"]), int(500), str(cdai_token["address"]), int(3000), str(weth_token["address"]))
route_str = (str(dai_token["symbol"]), int(500), str(cdai_token["symbol"]), int(3000), str(weth_token["symbol"]))

print(f"Route: {route_str} ")

#Test QuoteExactInput with 1 DAI Input
input_token_amount = int(1 * (10 ** int(dai_token["decimals"])))
       
route_encoded = encode_abi_packed(route_types, route_tuple)  

amount_out = quoter_contract.functions.quoteExactInput(route_encoded, int(input_token_amount)).call()
amount_out = amount_out / (10 ** int(weth_token["decimals"]))

print("Output Amount: {:.8f} WETH for 1 DAI".format(amount_out))

#Test QuoteExactOutput with 1 DAI Output
output_token_amount = int(1 * (10 ** int(dai_token["decimals"])))
       
route_encoded = encode_abi_packed(route_types, route_tuple)  

amount_in = quoter_contract.functions.quoteExactOutput(route_encoded, int(output_token_amount)).call()
amount_in = amount_in / (10 ** int(weth_token["decimals"]))

print("Input Amount: {:.8f} WETH for 1 DAI".format(amount_in))





print()
print("This cause error")

#Route: ('WETH', 3000, 'cDAI', 500, 'DAI')
route_types = ['address', 'uint24', 'address', 'uint24', 'address']
route_tuple = (str(weth_token["address"]), int(3000), str(cdai_token["address"]), int(500), str(dai_token["address"]))
route_str = (str(weth_token["symbol"]), int(3000), str(cdai_token["symbol"]), int(500), str(dai_token["symbol"]))

route_tuple = (str(weth_token["address"]), int(3000), str(cdai_token["address"]), int(500), str(dai_token["address"]))
route_str = (str(weth_token["symbol"]), int(3000), str(cdai_token["symbol"]), int(500), str(dai_token["symbol"]))

print(f"Route: {route_str} ")

#Test QuoteExactInput with 1 WETH Input
input_token_amount = int(1 * (10 ** int(weth_token["decimals"])))
       
route_encoded = encode_abi_packed(route_types, route_tuple)  

amount_out = quoter_contract.functions.quoteExactInput(route_encoded, int(input_token_amount)).call()
amount_out = amount_out / (10 ** int(dai_token["decimals"]))

print("Output Amount: {:.8f} DAI for 1 WETH".format(amount_out))



#Test QuoteExactOutput with 1 WETH Output
output_token_amount = int(1 * (10 ** int(weth_token["decimals"])))
       
route_encoded = encode_abi_packed(route_types, route_tuple)  

amount_in = quoter_contract.functions.quoteExactOutput(route_encoded, int(output_token_amount)).call()
amount_in = amount_in / (10 ** int(dai_token["decimals"]))

print("Input Amount: {:.8f} DAI for 1 WETH".format(amount_in))

有谁知道问题可能是什么吗?

EN

回答 2

Ethereum用户

发布于 2022-10-12 07:52:00

这两个结果差异如此之大的原因是因为滑动点。weth/cdai存在巨大的价格波动,特别是当您想要交换1 eth时。

票数 1
EN

Ethereum用户

发布于 2022-12-16 09:25:19

这里也是一样的!

我的编码功能:

代码语言:javascript
复制
    function encodePath(path, fees) {
        if (path.length != fees.length + 1) {
          throw new Error('path/fee lengths do not match')
        }
        
      
        let encoded = '0x'
        for (let i = 0; i < fees.length; i++) {
          // 20 byte encoding of the address
          encoded += String(path[i]).slice(2)
          // 3 byte encoding of the fee
          encoded += String(ethers.utils.hexlify(fees[i])).slice(2).padStart(6, '0')
        }
        // encode the final token
        encoded += path[path.length - 1].slice(2)
      
        return encoded.toLowerCase()
      }

调用exactInput:

代码语言:javascript
复制
export const quoteExactInput = async (amount: any, chainId: number, encoded: string) =>{
  try {
    const quoter = new ethers.Contract(
      QUOTER_ADDRESS[chainId],
      Quoter.abi,
      setProvider(chainId)
    )
    const result = await quoter.callStatic.quoteExactInput(encoded, amount)
    return {error: false, result: result}
  } catch (error) {
    console.log(error, 'for quoteExactInput')
    return {error: true, result: error}
  }
}

export const getSmallerPriceImpact = async (amount: any, chainId: number) => {
    let output = []
    // const data = await axios.get('http://localhost:5000/router/0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6/0xD87Ba7A50B2E7E660f678A895E4B72E7CB4CCd9C/0x8eB105CFc7ec7ebF126586689683a9104E6ec91b/5')
    console.log(data, 'encodedArr')
      for(let i = 0; i < data.data.length; i++){
        console.log(data.data[i].encoded, 'encoded')
        const result = await quoteExactInput(amount, chainId, data.data[i].encoded)
        output.push(result)
      }
    return output
}

我的编码:

代码语言:javascript
复制
0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6000bb807865c6e87b9f70255377e024ace6630c1eaa37f000bb8d87ba7a50b2e7e660f678a895e4b72e7cb4ccd9c

我所犯的错误:

代码语言:javascript
复制
Error: call revert exception; VM Exception while processing transaction: reverted with reason string "Unexpected error" [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (method="quoteExactInput(bytes,uint256)", data="0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010556e6578706563746564206572726f7200000000000000000000000000000000", errorArgs=["Unexpected error"], errorName="Error", errorSignature="Error(string)", reason="Unexpected error", code=CALL_EXCEPTION, version=abi/5.7.0)
    at Logger.makeError (http://localhost:3000/static/js/vendors~main.chunk.js:12074:19)
    at Logger.throwError (http://localhost:3000/static/js/vendors~main.chunk.js:12083:16)
    at Interface.decodeFunctionResult (http://localhost:3000/static/js/vendors~main.chunk.js:2963:19)
    at Object.<anonymous> (http://localhost:3000/static/js/vendors~main.chunk.js:8868:40)
    at Generator.next (<anonymous>)
    at fulfilled (http://localhost:3000/static/js/vendors~main.chunk.js:8552:24)
代码语言:javascript
复制
"call revert exception; VM Exception while processing transaction: reverted with reason string "Unexpected error" [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (method="quoteExactInput(bytes,uint256)", data="0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010556e6578706563746564206572726f7200000000000000000000000000000000", errorArgs=["Unexpected error"], errorName="Error", errorSignature="Error(string)", reason="Unexpected error", code=CALL_EXCEPTION, version=abi/5.7.0)"

有什么暗示吗?

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/111151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档