Skip to content

Commit

Permalink
Check liquidity for Uniswap V3 oracle
Browse files Browse the repository at this point in the history
- If there's not enough liquidity price can get crazy
  • Loading branch information
Uxio0 committed Sep 28, 2022
1 parent 2ac06eb commit d54202c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
16 changes: 14 additions & 2 deletions gnosis/eth/oracles/uniswap_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,26 @@ def get_price(
pool_contract = self.w3.eth.contract(pool_address, abi=uniswap_v3_pool_abi)

try:
sqrt_price_x96, _, _, _, _, _, _ = pool_contract.functions.slot0().call()
(
liquidity,
(sqrt_price_x96, _, _, _, _, _, _),
) = self.ethereum_client.batch_call(
[pool_contract.functions.liquidity(), pool_contract.functions.slot0()]
)
if liquidity == 0:
error_message = (
f"Not enough liquidity on uniswap v3 for pair token_1={token_address} "
f"token_2={token_address_2}"
)
logger.warning(error_message)
raise CannotGetPriceFromOracle(error_message)
except (
ValueError,
BadFunctionCallOutput,
DecodingError,
) as e:
error_message = (
f"Cannot get uniswap v2 price for pair token_1={token_address} "
f"Cannot get uniswap v3 price for pair token_1={token_address} "
f"token_2={token_address_2}"
)
logger.warning(error_message)
Expand Down
9 changes: 9 additions & 0 deletions gnosis/eth/tests/oracles/test_uniswap_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def test_get_price(self):
):
uniswap_v3_oracle.get_price(random_token)

# Test token with no liquidity
s_usd_token_mainnet_address = "0x57Ab1ec28D129707052df4dF418D58a2D46d5f51"
with self.assertRaisesMessage(
CannotGetPriceFromOracle,
f"Not enough liquidity on uniswap v3 for pair token_1={s_usd_token_mainnet_address} "
f"token_2=0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
):
uniswap_v3_oracle.get_price(s_usd_token_mainnet_address)

def test_get_price_contract_not_deployed(self):
self.assertFalse(UniswapV3Oracle.is_available(self.ethereum_client))
with self.assertRaisesMessage(
Expand Down

0 comments on commit d54202c

Please sign in to comment.