Skip to content

Commit

Permalink
LpSugar: refactor safe calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
stas committed Oct 30, 2024
1 parent 20e956a commit 134e847
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
33 changes: 16 additions & 17 deletions contracts/LpSugar.vy
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def forSwaps(_limit: uint256, _offset: uint256) -> DynArray[SwapLp, lp_shared.MA
break

factory: lp_shared.IPoolFactory = lp_shared.IPoolFactory(factories[index])
if lp_shared._is_root_factory(factory.address):
if lp_shared._is_root_placeholder_factory(factory.address):
continue

nfpm: address = lp_shared._fetch_nfpm(factory.address)
Expand Down Expand Up @@ -570,8 +570,7 @@ def _positions(

factory: lp_shared.IPoolFactory = lp_shared.IPoolFactory(_factories[index])

# Skip root placeholder pools...
if lp_shared._is_root_factory(factory.address):
if lp_shared._is_root_placeholder_factory(factory.address):
continue

pools_count: uint256 = staticcall factory.allPoolsLength()
Expand Down Expand Up @@ -766,7 +765,7 @@ def positionsUnstakedConcentrated(
if nfpm.address == empty(address):
continue

if lp_shared._is_root_factory(factory.address):
if lp_shared._is_root_placeholder_factory(factory.address):
continue

# Handled in `positions()`
Expand Down Expand Up @@ -1038,7 +1037,7 @@ def _safe_balance_of(_token: address, _address: address) -> uint256:
"""
response: Bytes[32] = raw_call(
_token,
concat(method_id("balanceOf(address)"), convert(_address, bytes32)),
abi_encode(_address, method_id=method_id("balanceOf(address)")),
max_outsize=32,
gas=100000,
is_delegate_call=False,
Expand All @@ -1047,7 +1046,7 @@ def _safe_balance_of(_token: address, _address: address) -> uint256:
)[1]

if len(response) > 0:
return (convert(response, uint256))
return (abi_decode(response, uint256))

return 0

Expand All @@ -1059,20 +1058,20 @@ def _safe_decimals(_token: address) -> uint8:
@param _token The token to call
@param _address The address to get the balanceOf
"""
success: bool = False
response: Bytes[32] = b""
success, response = raw_call(
response = raw_call(
_token,
method_id("decimals()"),
max_outsize=32,
gas=50000,
is_delegate_call=False,
is_static_call=True,
revert_on_failure=False
)
)[1]

if success:
return (convert(response, uint8))
# Check response as revert_on_failure is set to False
if len(response) > 0:
return (abi_decode(response, uint8))

return 18

Expand All @@ -1083,21 +1082,21 @@ def _safe_symbol(_token: address) -> String[100]:
@notice Returns the `ERC20.symbol()` result safely
@param _token The token to call
"""
success: bool = False
# >=192 input size is required by Vyper's _abi_decode()
response: Bytes[192] = b""
success, response = raw_call(
response = raw_call(
_token,
method_id("symbol()"),
max_outsize=192,
gas=50000,
is_delegate_call=False,
is_static_call=True,
revert_on_failure=False
)
)[1]

if success:
return _abi_decode(response, String[100])
# Check response as revert_on_failure is set to False
if len(response) > 0:
return abi_decode(response, String[100])

return "-NA-"

Expand All @@ -1114,7 +1113,7 @@ def _has_userPositions(_nfpm: address) -> bool:
_nfpm,
abi_encode(
# We just need valid addresses, please ignore the values
_nfpm, _nfpm, method_id("userPositions(address,address)"),
_nfpm, _nfpm, method_id=method_id("userPositions(address,address)"),
),
max_outsize=32,
is_delegate_call=False,
Expand Down
15 changes: 8 additions & 7 deletions contracts/modules/lp_shared.vy
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def _pools(_limit: uint256, _offset: uint256)\
break

factory: IPoolFactory = IPoolFactory(factories[index])
if self._is_root_factory(factory.address):
if self._is_root_placeholder_factory(factory.address):
continue

pools_count: uint256 = staticcall factory.allPoolsLength()
Expand Down Expand Up @@ -99,21 +99,22 @@ def _pools(_limit: uint256, _offset: uint256)\

@internal
@view
def _is_root_factory(_factory: address) -> bool:
def _is_root_placeholder_factory(_factory: address) -> bool:
"""
@notice Returns true if the factory is a root pool factory and false if it is a leaf pool factory.
@notice Checks if the factory is for root placeholder pools
@param _factory The factory address
@return bool
"""
success: bool = raw_call(
response: Bytes[32] = raw_call(
_factory,
method_id("bridge()"),
max_outsize=32,
is_delegate_call=False,
is_static_call=True,
revert_on_failure=False
)[0]
)[1]

return success
return len(response) > 0

@internal
@view
Expand All @@ -135,6 +136,6 @@ def _fetch_nfpm(_factory: address) -> address:
)[1]

if len(response) > 0:
return convert(response, address)
return abi_decode(response, address)

return empty(address)

0 comments on commit 134e847

Please sign in to comment.