From 525b1cb072cac334d2627ccaa7754aac71611356 Mon Sep 17 00:00:00 2001 From: Zoomer <87513793+ethzoomer@users.noreply.github.com> Date: Tue, 10 Sep 2024 12:01:25 -0500 Subject: [PATCH 1/3] feat: cap MAX_POOLS array allocation --- contracts/LpSugar.vy | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/contracts/LpSugar.vy b/contracts/LpSugar.vy index 6edeea3..a7872cf 100644 --- a/contracts/LpSugar.vy +++ b/contracts/LpSugar.vy @@ -9,6 +9,7 @@ MAX_FACTORIES: public(constant(uint256)) = 10 MAX_POOLS: public(constant(uint256)) = 2000 +MAX_ITERATIONS: public(constant(uint256)) = 8000 MAX_TOKENS: public(constant(uint256)) = 2000 MAX_LPS: public(constant(uint256)) = 500 MAX_EPOCHS: public(constant(uint256)) = 200 @@ -265,8 +266,8 @@ def _pools(_limit: uint256, _offset: uint256)\ factories: DynArray[address, MAX_FACTORIES] = self.registry.poolFactories() factories_count: uint256 = len(factories) - placeholder: address[4] = empty(address[4]) to_skip: uint256 = _offset + visited: uint256 = 0 pools: DynArray[address[4], MAX_POOLS] = \ empty(DynArray[address[4], MAX_POOLS]) @@ -279,18 +280,19 @@ def _pools(_limit: uint256, _offset: uint256)\ pools_count: uint256 = factory.allPoolsLength() nfpm: address = self._fetch_nfpm(factory.address) - for pindex in range(0, MAX_POOLS): - if pindex >= pools_count or len(pools) >= _limit + _offset: + for pindex in range(0, MAX_ITERATIONS): + if pindex >= pools_count or visited >= _limit + _offset: break # Since the convertor pool, first pool on one of the factories... if pindex == 0 and factory.allPools(0) == self.convertor: continue + visited += 1 + # Basically skip calls for offset records... if to_skip > 0: to_skip -= 1 - pools.append(placeholder) continue pool_addr: address = factory.allPools(pindex) @@ -324,7 +326,7 @@ def forSwaps(_limit: uint256, _offset: uint256) -> DynArray[SwapLp, MAX_POOLS]: nfpm: address = self._fetch_nfpm(factory.address) pools_count: uint256 = factory.allPoolsLength() - for pindex in range(0, MAX_POOLS): + for pindex in range(0, MAX_ITERATIONS): if pindex >= pools_count: break @@ -471,7 +473,7 @@ def byIndex(_index: uint256) -> Lp: """ # Basically index is the limit and the offset is always one... # This will fire if _index is out of bounds - pool_data: address[4] = self._pools(1, _index)[_index] + pool_data: address[4] = self._pools(1, _index) pool: IPool = IPool(pool_data[1]) token0: address = pool.token0() token1: address = pool.token1() From c0c30d333df653bc493cd5bd10a21095ca545ddb Mon Sep 17 00:00:00 2001 From: Zoomer <87513793+ethzoomer@users.noreply.github.com> Date: Tue, 10 Sep 2024 13:26:34 -0500 Subject: [PATCH 2/3] fix: _pools array offsets --- contracts/LpSugar.vy | 10 +++++----- env.example | 2 +- readme.md | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/contracts/LpSugar.vy b/contracts/LpSugar.vy index a7872cf..e2e3391 100644 --- a/contracts/LpSugar.vy +++ b/contracts/LpSugar.vy @@ -396,7 +396,7 @@ def tokens(_limit: uint256, _offset: uint256, _account: address, \ col.append(self._token(_addresses[index], _account)) seen.append(_addresses[index]) - for index in range(_offset, _offset + MAX_TOKENS): + for index in range(0, MAX_TOKENS): if len(col) >= _limit or index >= pools_count: break @@ -446,7 +446,7 @@ def all(_limit: uint256, _offset: uint256) -> DynArray[Lp, MAX_LPS]: pools: DynArray[address[4], MAX_POOLS] = self._pools(_limit, _offset) pools_count: uint256 = len(pools) - for index in range(_offset, _offset + MAX_POOLS): + for index in range(0, MAX_POOLS): if len(col) == _limit or index >= pools_count: break @@ -473,7 +473,7 @@ def byIndex(_index: uint256) -> Lp: """ # Basically index is the limit and the offset is always one... # This will fire if _index is out of bounds - pool_data: address[4] = self._pools(1, _index) + pool_data: address[4] = self._pools(1, _index)[0] pool: IPool = IPool(pool_data[1]) token0: address = pool.token0() token1: address = pool.token1() @@ -956,7 +956,7 @@ def epochsLatest(_limit: uint256, _offset: uint256) \ col: DynArray[LpEpoch, MAX_POOLS] = empty(DynArray[LpEpoch, MAX_POOLS]) - for index in range(_offset, _offset + MAX_POOLS): + for index in range(0, MAX_POOLS): if counted == _limit or index >= pools_count: break @@ -1119,7 +1119,7 @@ def rewards(_limit: uint256, _offset: uint256, _venft_id: uint256) \ col: DynArray[Reward, MAX_POOLS] = empty(DynArray[Reward, MAX_POOLS]) - for pindex in range(_offset, _offset + MAX_POOLS): + for pindex in range(0, MAX_POOLS): if counted == _limit or pindex >= pools_count: break diff --git a/env.example b/env.example index 29b8782..81665a2 100644 --- a/env.example +++ b/env.example @@ -5,7 +5,7 @@ CONVERTOR_ADDRESS=0x1111111111111111111111111111111111111111 RELAY_REGISTRY_ADDRESSES=0x05e41604B9463e2224227053980dfF3f57fb6dB5,0xD308aBCe663302d3b86b36d332CEFd8A4F62C5Ed SLIPSTREAM_HELPER_ADDRESS=0x6d2D739bf37dFd93D804523c2dfA948EAf32f8E1 GOVERNOR_ADDRESS=0x94C012A23A8A65A6f40608dA30534a46a433F410 -LP_SUGAR_ADDRESS=0x68c19e13618C41158fE4bAba1B8fb3A9c74bDb0A +LP_SUGAR_ADDRESS=0x08483151e59375a03E82C180BBc4c79E5CB8C60D VE_SUGAR_ADDRESS=0x4c5d3925fe65DFeB5A079485136e4De09cb664A5 RELAY_SUGAR_ADDRESS=0x8932B5FE23C07Df06533F8f09E43e7cca6a24143 diff --git a/readme.md b/readme.md index 7c0be13..615df5a 100644 --- a/readme.md +++ b/readme.md @@ -35,7 +35,7 @@ Below is the list of datasets we support. ### Liquidity Pools Data -`LpSugar.vy` is deployed at `0x68c19e13618C41158fE4bAba1B8fb3A9c74bDb0A` +`LpSugar.vy` is deployed at `0x08483151e59375a03E82C180BBc4c79E5CB8C60D` It allows fetching on-chain pools data. The returned data/struct of type `Lp` values represent: From 9615bddebd729f1fd68e3e82c20d7efd883fff66 Mon Sep 17 00:00:00 2001 From: ethzoomer Date: Tue, 10 Sep 2024 18:15:59 -0500 Subject: [PATCH 3/3] fix: MAX_POOLS length check --- contracts/LpSugar.vy | 6 +++--- env.example | 2 +- readme.md | 2 +- tests/test_lp_sugar.py | 15 +++++++++++++++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/contracts/LpSugar.vy b/contracts/LpSugar.vy index e2e3391..776dbd6 100644 --- a/contracts/LpSugar.vy +++ b/contracts/LpSugar.vy @@ -281,7 +281,7 @@ def _pools(_limit: uint256, _offset: uint256)\ nfpm: address = self._fetch_nfpm(factory.address) for pindex in range(0, MAX_ITERATIONS): - if pindex >= pools_count or visited >= _limit + _offset: + if pindex >= pools_count or visited >= _limit + _offset or len(pools) >= MAX_POOLS: break # Since the convertor pool, first pool on one of the factories... @@ -327,7 +327,7 @@ def forSwaps(_limit: uint256, _offset: uint256) -> DynArray[SwapLp, MAX_POOLS]: pools_count: uint256 = factory.allPoolsLength() for pindex in range(0, MAX_ITERATIONS): - if pindex >= pools_count: + if pindex >= pools_count or len(pools) >= MAX_POOLS: break # If no pools to process are left... @@ -396,7 +396,7 @@ def tokens(_limit: uint256, _offset: uint256, _account: address, \ col.append(self._token(_addresses[index], _account)) seen.append(_addresses[index]) - for index in range(0, MAX_TOKENS): + for index in range(0, MAX_POOLS): if len(col) >= _limit or index >= pools_count: break diff --git a/env.example b/env.example index 81665a2..9fdc4dd 100644 --- a/env.example +++ b/env.example @@ -5,7 +5,7 @@ CONVERTOR_ADDRESS=0x1111111111111111111111111111111111111111 RELAY_REGISTRY_ADDRESSES=0x05e41604B9463e2224227053980dfF3f57fb6dB5,0xD308aBCe663302d3b86b36d332CEFd8A4F62C5Ed SLIPSTREAM_HELPER_ADDRESS=0x6d2D739bf37dFd93D804523c2dfA948EAf32f8E1 GOVERNOR_ADDRESS=0x94C012A23A8A65A6f40608dA30534a46a433F410 -LP_SUGAR_ADDRESS=0x08483151e59375a03E82C180BBc4c79E5CB8C60D +LP_SUGAR_ADDRESS=0xcDF4AA33Bafba3e5dc5B3ae54ab67324Ef956ABD VE_SUGAR_ADDRESS=0x4c5d3925fe65DFeB5A079485136e4De09cb664A5 RELAY_SUGAR_ADDRESS=0x8932B5FE23C07Df06533F8f09E43e7cca6a24143 diff --git a/readme.md b/readme.md index 615df5a..9194cb0 100644 --- a/readme.md +++ b/readme.md @@ -35,7 +35,7 @@ Below is the list of datasets we support. ### Liquidity Pools Data -`LpSugar.vy` is deployed at `0x08483151e59375a03E82C180BBc4c79E5CB8C60D` +`LpSugar.vy` is deployed at `0xcDF4AA33Bafba3e5dc5B3ae54ab67324Ef956ABD` It allows fetching on-chain pools data. The returned data/struct of type `Lp` values represent: diff --git a/tests/test_lp_sugar.py b/tests/test_lp_sugar.py index 73d2f3a..c96136a 100644 --- a/tests/test_lp_sugar.py +++ b/tests/test_lp_sugar.py @@ -119,6 +119,21 @@ def test_tokens(sugar_contract, TokenStruct, LpStruct): assert token1.token_address == first_lp.token1 +def test_tokens_large_limit(sugar_contract, TokenStruct): + many_tokens = list(map( + lambda _p: TokenStruct(*_p), + sugar_contract.tokens(2000, 1000, ADDRESS_ZERO, []) + )) + + assert many_tokens is not None + assert len(many_tokens) > 100 + + token0 = many_tokens[0] + + assert token0.symbol is not None + assert token0.decimals > 0 + + def test_all(sugar_contract, LpStruct): first_lp = LpStruct(*sugar_contract.byIndex(0)) second_lp = LpStruct(*sugar_contract.byIndex(1))