Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check liquidity for Uniswap V3 oracle #361

Merged
merged 2 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm wrong but could be that in the future this token have liquidity?

Copy link
Member Author

@Uxio0 Uxio0 Sep 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, then test will break and we will have to find another one or mock it 😉

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
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = safe-eth-py
version = 4.5.0
version = 4.5.1
description = Safe Ecosystem Foundation utilities for Ethereum projects
long_description = file: README.rst
long_description_content_type = text/x-rst; charset=UTF-8
Expand Down