Skip to content

Commit

Permalink
migrate to new sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
callmephilip committed Mar 20, 2024
1 parent 098f6a2 commit 5628481
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 41 deletions.
94 changes: 67 additions & 27 deletions bots/abi/lp_sugar.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"inputs": [
{ "name": "_voter", "type": "address" },
{ "name": "_registry", "type": "address" },
{ "name": "_convertor", "type": "address" }
{ "name": "_convertor", "type": "address" },
{ "name": "_nfpm", "type": "address" },
{ "name": "_slipstream_helper", "type": "address" }
],
"outputs": []
},
Expand All @@ -23,10 +25,11 @@
"type": "tuple[]",
"components": [
{ "name": "lp", "type": "address" },
{ "name": "stable", "type": "bool" },
{ "name": "type", "type": "int24" },
{ "name": "token0", "type": "address" },
{ "name": "token1", "type": "address" },
{ "name": "factory", "type": "address" }
{ "name": "factory", "type": "address" },
{ "name": "pool_fee", "type": "uint256" }
]
}
]
Expand All @@ -38,7 +41,8 @@
"inputs": [
{ "name": "_limit", "type": "uint256" },
{ "name": "_offset", "type": "uint256" },
{ "name": "_account", "type": "address" }
{ "name": "_account", "type": "address" },
{ "name": "_addresses", "type": "address[]" }
],
"outputs": [
{
Expand All @@ -60,8 +64,7 @@
"name": "all",
"inputs": [
{ "name": "_limit", "type": "uint256" },
{ "name": "_offset", "type": "uint256" },
{ "name": "_account", "type": "address" }
{ "name": "_offset", "type": "uint256" }
],
"outputs": [
{
Expand All @@ -71,26 +74,26 @@
{ "name": "lp", "type": "address" },
{ "name": "symbol", "type": "string" },
{ "name": "decimals", "type": "uint8" },
{ "name": "stable", "type": "bool" },
{ "name": "total_supply", "type": "uint256" },
{ "name": "liquidity", "type": "uint256" },
{ "name": "type", "type": "int24" },
{ "name": "tick", "type": "int24" },
{ "name": "sqrt_ratio", "type": "uint160" },
{ "name": "token0", "type": "address" },
{ "name": "reserve0", "type": "uint256" },
{ "name": "claimable0", "type": "uint256" },
{ "name": "staked0", "type": "uint256" },
{ "name": "token1", "type": "address" },
{ "name": "reserve1", "type": "uint256" },
{ "name": "claimable1", "type": "uint256" },
{ "name": "staked1", "type": "uint256" },
{ "name": "gauge", "type": "address" },
{ "name": "gauge_total_supply", "type": "uint256" },
{ "name": "gauge_liquidity", "type": "uint256" },
{ "name": "gauge_alive", "type": "bool" },
{ "name": "fee", "type": "address" },
{ "name": "bribe", "type": "address" },
{ "name": "factory", "type": "address" },
{ "name": "emissions", "type": "uint256" },
{ "name": "emissions_token", "type": "address" },
{ "name": "account_balance", "type": "uint256" },
{ "name": "account_earned", "type": "uint256" },
{ "name": "account_staked", "type": "uint256" },
{ "name": "pool_fee", "type": "uint256" },
{ "name": "unstaked_fee", "type": "uint256" },
{ "name": "token0_fees", "type": "uint256" },
{ "name": "token1_fees", "type": "uint256" }
]
Expand All @@ -101,10 +104,7 @@
"stateMutability": "view",
"type": "function",
"name": "byIndex",
"inputs": [
{ "name": "_index", "type": "uint256" },
{ "name": "_account", "type": "address" }
],
"inputs": [{ "name": "_index", "type": "uint256" }],
"outputs": [
{
"name": "",
Expand All @@ -113,32 +113,65 @@
{ "name": "lp", "type": "address" },
{ "name": "symbol", "type": "string" },
{ "name": "decimals", "type": "uint8" },
{ "name": "stable", "type": "bool" },
{ "name": "total_supply", "type": "uint256" },
{ "name": "liquidity", "type": "uint256" },
{ "name": "type", "type": "int24" },
{ "name": "tick", "type": "int24" },
{ "name": "sqrt_ratio", "type": "uint160" },
{ "name": "token0", "type": "address" },
{ "name": "reserve0", "type": "uint256" },
{ "name": "claimable0", "type": "uint256" },
{ "name": "staked0", "type": "uint256" },
{ "name": "token1", "type": "address" },
{ "name": "reserve1", "type": "uint256" },
{ "name": "claimable1", "type": "uint256" },
{ "name": "staked1", "type": "uint256" },
{ "name": "gauge", "type": "address" },
{ "name": "gauge_total_supply", "type": "uint256" },
{ "name": "gauge_liquidity", "type": "uint256" },
{ "name": "gauge_alive", "type": "bool" },
{ "name": "fee", "type": "address" },
{ "name": "bribe", "type": "address" },
{ "name": "factory", "type": "address" },
{ "name": "emissions", "type": "uint256" },
{ "name": "emissions_token", "type": "address" },
{ "name": "account_balance", "type": "uint256" },
{ "name": "account_earned", "type": "uint256" },
{ "name": "account_staked", "type": "uint256" },
{ "name": "pool_fee", "type": "uint256" },
{ "name": "unstaked_fee", "type": "uint256" },
{ "name": "token0_fees", "type": "uint256" },
{ "name": "token1_fees", "type": "uint256" }
]
}
]
},
{
"stateMutability": "view",
"type": "function",
"name": "positions",
"inputs": [
{ "name": "_limit", "type": "uint256" },
{ "name": "_offset", "type": "uint256" },
{ "name": "_account", "type": "address" }
],
"outputs": [
{
"name": "",
"type": "tuple[]",
"components": [
{ "name": "id", "type": "uint256" },
{ "name": "lp", "type": "address" },
{ "name": "liquidity", "type": "uint256" },
{ "name": "staked", "type": "uint256" },
{ "name": "amount0", "type": "uint256" },
{ "name": "amount1", "type": "uint256" },
{ "name": "staked0", "type": "uint256" },
{ "name": "staked1", "type": "uint256" },
{ "name": "unstaked_earned0", "type": "uint256" },
{ "name": "unstaked_earned1", "type": "uint256" },
{ "name": "emissions_earned", "type": "uint256" },
{ "name": "tick_lower", "type": "int24" },
{ "name": "tick_upper", "type": "int24" },
{ "name": "sqrt_ratio_lower", "type": "uint160" },
{ "name": "sqrt_ratio_upper", "type": "uint160" }
]
}
]
},
{
"stateMutability": "view",
"type": "function",
Expand Down Expand Up @@ -285,7 +318,14 @@
{
"stateMutability": "view",
"type": "function",
"name": "v1_factory",
"name": "nfpm",
"inputs": [],
"outputs": [{ "name": "", "type": "address" }]
},
{
"stateMutability": "view",
"type": "function",
"name": "cl_helper",
"inputs": [],
"outputs": [{ "name": "", "type": "address" }]
}
Expand Down
69 changes: 55 additions & 14 deletions bots/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ORACLE_PRICES_CACHE_MINUTES,
PRICE_BATCH_SIZE,
GOOD_ENOUGH_PAGINATION_LIMIT,
POOL_PAGE_SIZE
)
from .helpers import cache_in_seconds, normalize_address, chunk

Expand Down Expand Up @@ -62,7 +63,7 @@ async def get_all_listed_tokens(cls) -> List["Token"]:
async def get_all_tokens(cls) -> List["Token"]:
sugar = w3.eth.contract(address=LP_SUGAR_ADDRESS, abi=LP_SUGAR_ABI)
tokens = await sugar.functions.tokens(
GOOD_ENOUGH_PAGINATION_LIMIT, 0, ADDRESS_ZERO
GOOD_ENOUGH_PAGINATION_LIMIT, 0, ADDRESS_ZERO, []
).call()
return list(map(lambda t: Token.from_tuple(t), tokens))

Expand Down Expand Up @@ -217,29 +218,57 @@ class LiquidityPool:
def from_tuple(
cls, t: Tuple, tokens: Dict[str, Token], prices: Dict[str, Price]
) -> "LiquidityPool":
token0 = normalize_address(t[5])
token1 = normalize_address(t[8])
token0 = normalize_address(t[7])
token1 = normalize_address(t[10])
token0_fees = t[23]
token1_fees = t[24]
emissions_token = normalize_address(t[18])
emissions = t[17]
emissions_token = normalize_address(t[20])
emissions = t[19]

seconds_in_a_week = 7 * 24 * 60 * 60

# Sugar.all returns a tuple with the following structure:
# { "name": "lp", "type": "address" }, <== 0
# { "name": "symbol", "type": "string" }, <== 1
# { "name": "decimals", "type": "uint8" }, <== 2
# { "name": "liquidity", "type": "uint256" }, <== 3
# { "name": "type", "type": "int24" }, <== 4
# { "name": "tick", "type": "int24" }, <== 5
# { "name": "sqrt_ratio", "type": "uint160" }, <== 6
# { "name": "token0", "type": "address" }, <== 7
# { "name": "reserve0", "type": "uint256" }, <== 8
# { "name": "staked0", "type": "uint256" }, <== 9
# { "name": "token1", "type": "address" }, <== 10
# { "name": "reserve1", "type": "uint256" }, <== 11
# { "name": "staked1", "type": "uint256" }, <== 12
# { "name": "gauge", "type": "address" }, <== 13
# { "name": "gauge_liquidity", "type": "uint256" }, <== 14
# { "name": "gauge_alive", "type": "bool" }, <== 15
# { "name": "fee", "type": "address" }, <== 16
# { "name": "bribe", "type": "address" }, <== 17
# { "name": "factory", "type": "address" }, <== 18
# { "name": "emissions", "type": "uint256" }, <== 19
# { "name": "emissions_token", "type": "address" }, <== 20
# { "name": "pool_fee", "type": "uint256" }, <== 21
# { "name": "unstaked_fee", "type": "uint256" }, <== 22
# { "name": "token0_fees", "type": "uint256" }, <== 23
# { "name": "token1_fees", "type": "uint256" } <== 24

return LiquidityPool(
lp=normalize_address(t[0]),
symbol=t[1],
is_stable=t[3],
total_supply=t[4],
# stable pools have type set to 0
is_stable=t[4] == 0,
total_supply=t[3],
decimals=t[2],
token0=tokens.get(token0),
reserve0=Amount.build(token0, t[6], tokens, prices),
reserve0=Amount.build(token0, t[8], tokens, prices),
token1=tokens.get(token1),
reserve1=Amount.build(token1, t[9], tokens, prices),
reserve1=Amount.build(token1, t[11], tokens, prices),
token0_fees=Amount.build(token0, token0_fees, tokens, prices),
token1_fees=Amount.build(token1, token1_fees, tokens, prices),
pool_fee=t[22],
gauge_total_supply=t[12],
pool_fee=t[21],
gauge_total_supply=t[14],
emissions_token=tokens.get(emissions_token),
emissions=Amount.build(emissions_token, emissions, tokens, prices),
weekly_emissions=Amount.build(
Expand All @@ -257,9 +286,21 @@ async def get_pools(cls) -> List["LiquidityPool"]:
prices = {price.token.token_address: price for price in prices}

sugar = w3.eth.contract(address=LP_SUGAR_ADDRESS, abi=LP_SUGAR_ABI)
pools = await sugar.functions.all(
GOOD_ENOUGH_PAGINATION_LIMIT, 0, ADDRESS_ZERO
).call()
pools = []
pool_offset = 0

while True:
pools_batch = await sugar.functions.all(
POOL_PAGE_SIZE, pool_offset
).call()

pools += pools_batch

if len(pools_batch) < POOL_PAGE_SIZE:
break
else:
pool_offset += POOL_PAGE_SIZE

return list(
filter(
lambda p: p is not None,
Expand Down
3 changes: 3 additions & 0 deletions bots/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@
# default pagination limit for api calls
GOOD_ENOUGH_PAGINATION_LIMIT = 2000

# pagination limit for pools
POOL_PAGE_SIZE = 500

# image shown on discord embeds for pool stats
UI_POOL_STATS_THUMBNAIL = os.environ["UI_POOL_STATS_THUMBNAIL"]

0 comments on commit 5628481

Please sign in to comment.