Skip to content

Commit

Permalink
LpSugar: cleanups and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
stas committed Dec 13, 2024
1 parent 75e131e commit 9de4871
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
27 changes: 6 additions & 21 deletions contracts/LpSugar.vy
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@ struct AlmManagedPositionInfo:

# Our contracts / Interfaces

interface IERC20:
def decimals() -> uint8: view
def symbol() -> String[100]: view
def balanceOf(_account: address) -> uint256: view

interface IPool:
def token0() -> address: view
def token1() -> address: view
Expand All @@ -148,7 +143,6 @@ interface IPool:
def index0() -> uint256: view
def index1() -> uint256: view
def totalSupply() -> uint256: view
def symbol() -> String[100]: view
def decimals() -> uint8: view
def stable() -> bool: view
def balanceOf(_account: address) -> uint256: view
Expand Down Expand Up @@ -348,7 +342,6 @@ def tokens(_limit: uint256, _offset: uint256, _account: address, \
@internal
@view
def _token(_address: address, _account: address) -> Token:
token: IERC20 = IERC20(_address)
bal: uint256 = empty(uint256)

if _account != empty(address):
Expand Down Expand Up @@ -433,8 +426,6 @@ def _v2_lp(_data: address[4], _token0: address, _token1: address) -> Lp:
is_stable: bool = staticcall pool.stable()
pool_fee: uint256 = staticcall lp_shared.IPoolFactory(_data[0]).getFee(pool.address, is_stable)
pool_fees: address = staticcall pool.poolFees()
token0: IERC20 = IERC20(_token0)
token1: IERC20 = IERC20(_token1)
token0_fees: uint256 = self._safe_balance_of(_token0, pool_fees)
token1_fees: uint256 = self._safe_balance_of(_token1, pool_fees)
gauge_alive: bool = staticcall lp_shared.voter.isAlive(gauge.address)
Expand Down Expand Up @@ -473,11 +464,11 @@ def _v2_lp(_data: address[4], _token0: address, _token1: address) -> Lp:
tick: 0,
sqrt_ratio: 0,

token0: token0.address,
token0: _token0,
reserve0: reserve0,
staked0: staked0,

token1: token1.address,
token1: _token1,
reserve1: reserve1,
staked1: staked1,

Expand All @@ -500,9 +491,7 @@ def _v2_lp(_data: address[4], _token0: address, _token1: address) -> Lp:
nfpm: empty(address),
alm: empty(address),

root: lp_shared._root_lp_address(
_data[0], token0.address, token1.address, type
)
root: lp_shared._root_lp_address(_data[0], _token0, _token1, type)
})

@external
Expand Down Expand Up @@ -958,8 +947,6 @@ def _cl_lp(_data: address[4], _token0: address, _token1: address) -> Lp:
fee_voting_reward: address = empty(address)
emissions: uint256 = 0
emissions_token: address = empty(address)
token0: IERC20 = IERC20(_token0)
token1: IERC20 = IERC20(_token1)
staked0: uint256 = 0
staked1: uint256 = 0
tick_spacing: int24 = staticcall pool.tickSpacing()
Expand Down Expand Up @@ -1006,11 +993,11 @@ def _cl_lp(_data: address[4], _token0: address, _token1: address) -> Lp:
tick: slot.tick,
sqrt_ratio: slot.sqrtPriceX96,

token0: token0.address,
token0: _token0,
reserve0: self._safe_balance_of(_token0, pool.address),
staked0: staked0,

token1: token1.address,
token1: _token1,
reserve1: self._safe_balance_of(_token1, pool.address),
staked1: staked1,

Expand All @@ -1033,9 +1020,7 @@ def _cl_lp(_data: address[4], _token0: address, _token1: address) -> Lp:
nfpm: _data[3],
alm: alm_addresses[1],

root: lp_shared._root_lp_address(
_data[0], token0.address, token1.address, tick_spacing
),
root: lp_shared._root_lp_address(_data[0], _token0, _token1, tick_spacing),
})

@internal
Expand Down
24 changes: 24 additions & 0 deletions contracts/test.vy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# To test on OP Mainnet, pass these token addresses:
# * 0x4200000000000000000000000000000000000006 (weth)
# * 0x045D841ba37E180bC9b9D4da718E14b9ED7925d6 (garbaged unicode)

@external
@view
def safe_symbol(_token: address) -> String[10]:
response: Bytes[100] = raw_call(
_token,
method_id("symbol()"),
max_outsize=100,
gas=50000,
is_delegate_call=False,
is_static_call=True,
revert_on_failure=False,
)[1]

response_len: uint256 = len(response)

# Min bytes to use abi_decode()
if response_len > 0 and response_len <= 96:
return abi_decode(response, String[10])

return "-???-"
2 changes: 1 addition & 1 deletion env.optimism
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GOVERNOR_10=0x1F82e10D58aEf03DeA2e478029fB0387A1cbE989
TEST_ADDRESS_10=0x892ff98a46e5bd141e2d12618f4b2fe6284debac
TEST_ALM_ADDRESS_10=0x892ff98a46e5bd141e2d12618f4b2fe6284debac

LP_SUGAR_ADDRESS_10=0x54F8968CC76ECB17018E44049FdcC14ff833fa67
LP_SUGAR_ADDRESS_10=0x4edB8d7DEdf4C5Bc63fe2704Cc83166895905719
REWARDS_SUGAR_ADDRESS_10=0x62CCFB2496f49A80B0184AD720379B529E9152fB
VE_SUGAR_ADDRESS_10=0x94f913362b232e31daB49a1aFB775cfd25DaA6a1
RELAY_SUGAR_ADDRESS_10=0xb8307e5842B9aeE75C704183F0355076aa74b4e2
32 changes: 32 additions & 0 deletions tests/test_lp_sugar.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,38 @@ def test_tokens(sugar_contract, TokenStruct, LpStruct):
assert token1.token_address == first_lp.token1


@pytest.mark.skipif(int(CHAIN_ID) not in [10], reason="Only OP")
def test_tokens_long_symbol(sugar_contract, TokenStruct):
tokens = list(map(
lambda _p: TokenStruct(*_p),
sugar_contract.tokens(1, 995, ADDRESS_ZERO, [])
))

assert tokens is not None
assert len(tokens) > 1

token = tokens[0]

assert token.symbol is not None
assert token.symbol == '-???-'


@pytest.mark.skipif(int(CHAIN_ID) not in [10], reason="Only OP")
def test_all_long_symbol(sugar_contract, LpStruct):
pools = list(map(
lambda _p: LpStruct(*_p),
sugar_contract.all(1, 995)
))

assert pools is not None
assert len(pools) == 1

pool = pools[0]

assert pool.symbol is not None
assert pool.symbol == '-???-'


def test_all(sugar_contract, LpStruct):
first_lp = LpStruct(*sugar_contract.byIndex(0))
second_lp = LpStruct(*sugar_contract.byIndex(1))
Expand Down

0 comments on commit 9de4871

Please sign in to comment.