I'm currently trying to retrieve the price impact of a swap on a UniswapV3 pool. Uniswap itself seems to suggest the use of its quoter contract.
I see that the quoter contract has the "quoting" functions as write functions so I suppose a simulation of those is necessary to get the value over sending a transaction for every result needed. Unfortunately I seem unable to accomplish a simulation using Web3py.
My current code looks like this:
async def get_uniswap_v3_buy_impact(self, dex: Dex, amount_in_against: Decimal) -> Decimal:
quoter = self.contracts.quoter_contract
try:
res = await quoter.functions.quoteExactInputSingle(
dex.token.address,
dex.token_against.address,
dex.fee_tier,
int(amount_in_against * 10 ** dex.token_against.decimals),
0
).call()
print(res)
except Exception as e:
print(e)
return Decimal(0)
An error of execution reverted is always received with no data in the payload. I tried to integrate Web3py OffChainLookup by setting ccip_read_enabled=True in .call() but the result stays the same. This python library seems to do what I do as well. I'm sure I'm missing something here.
Updated code:
async def get_uniswap_v3_buy_impact(self, dex: Dex, amount_in_against: Decimal):
# get quoter contract
quoter = self.contracts.quoter_contract
# get and encode path
path = [dex.token_against.address, dex.fee_tier, dex.token.address]
path = "0x" + ''.join(item[2:] if isinstance(item, str) else f"{item:06X}" for item in path)
try:
res = await quoter.functions.quoteExactInput(
self.w3.to_bytes(hexstr=HexStr(path)),
int(amount_in_against * 10 ** dex.token_against.decimals)
).call()
print(res)
except Exception as ocl:
print(ocl)
return Decimal(0)