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

Fix Review #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 14 additions & 8 deletions gl-sherlock/contracts/api.vy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import params as Params
import pools as Pools
import fees as Fees
import positions as Positions
import ERC20Plus as ERC20Plus

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

Copy link
Contributor

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94


########################################################################
# This is the entry-point contract.
Expand Down Expand Up @@ -55,10 +56,10 @@ def CONTEXT(
quote_token: address,
desired : uint256,
slippage : uint256,
payload : Bytes[224]
payload : Bytes[512]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

) -> Ctx:
Copy link
Contributor

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

base_decimals : uint256 = convert(ERC20Plus(base_token).decimals(), uint256)
quote_decimals: uint256 = convert(ERC20Plus(quote_token).decimals(), uint256)
base_decimals : uint256 = ERC20Plus(base_token).decimals()
quote_decimals: uint256 = ERC20Plus(quote_token).decimals()
Comment on lines +62 to +63

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

# this will revert on error
Comment on lines +63 to 64
Copy link
Contributor

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

price : uint256 = self.ORACLE.price(quote_decimals,
desired,
Expand All @@ -72,6 +73,7 @@ def CONTEXT(

########################################################################
@external
@nonreentrant("lock")

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

def mint(
base_token : address, #ERC20
quote_token : address, #ERC20
Expand All @@ -80,7 +82,7 @@ def mint(
quote_amt : uint256,
desired : uint256,
slippage : uint256,
payload : Bytes[224]
payload : Bytes[512]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

) -> uint256:
"""
@notice Provide liquidity to the pool
Copy link
Contributor

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

Expand All @@ -101,14 +103,15 @@ def mint(
return self.CORE.mint(1, base_token, quote_token, lp_token, base_amt, quote_amt, ctx)

@external
@nonreentrant("lock")

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

def burn(
base_token : address,
quote_token : address,
lp_token : address,
lp_amt : uint256,
desired : uint256,
slippage : uint256,
payload : Bytes[224]
payload : Bytes[512]

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

) -> Tokens:
"""
@notice Withdraw liquidity from the pool
Expand All @@ -128,6 +131,7 @@ def burn(
return self.CORE.burn(1, base_token, quote_token, lp_token, lp_amt, ctx)

@external
@nonreentrant("lock")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

def open(
base_token : address,
quote_token : address,
Expand All @@ -136,7 +140,7 @@ def open(
leverage : uint256,
desired : uint256,
slippage : uint256,
payload : Bytes[224]
payload : Bytes[512]

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

) -> PositionState:
"""
@notice Open a position
Expand All @@ -158,13 +162,14 @@ def open(
return self.CORE.open(1, base_token, quote_token, long, collateral0, leverage, ctx)

@external
@nonreentrant("lock")

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

def close(
base_token : address,
quote_token : address,
position_id : uint256,
desired : uint256,
slippage : uint256,
payload : Bytes[224]
payload : Bytes[512]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

) -> PositionValue:
"""
@notice Close a position
Expand All @@ -183,13 +188,14 @@ def close(
return self.CORE.close(1, base_token, quote_token, position_id, ctx)

@external
@nonreentrant("lock")

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

def liquidate(
base_token : address,
quote_token: address,
position_id: uint256,
desired : uint256,
slippage : uint256,
payload : Bytes[224]
payload : Bytes[512]

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/1.

Fixes issues: #94

) -> PositionValue:
"""
@notice Liquidate a position
Expand Down
2 changes: 1 addition & 1 deletion gl-sherlock/contracts/math.vy
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def balanced(state: Value, burn_value: uint256, ctx: Ctx) -> Tokens:
# Magic number which depends on the smallest fee one wants to support
# and the blocktime (since fees are per block). See types.vy/Parameters
# for an example.
DENOM: constant(uint256) = 1_000_000_000
DENOM: constant(uint256) = 1_000_000_000_000
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/3.

Fixes issues: #66

Copy link
Contributor

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/3.

Fixes issues: #66


@external
@pure
Expand Down
4 changes: 2 additions & 2 deletions gl-sherlock/contracts/params.vy
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ def utilization(reserves: uint256, interest: uint256) -> uint256:
"""
Reserve utilization in percent (rounded down).
"""
return 0 if (reserves == 0 or interest == 0) else (interest / (reserves / 100))
return 0 if (reserves == 0 or interest == 0) else (interest / (reserves / 100_000))

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/3.

Fixes issues: #66

Copy link
Contributor

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/3.

Fixes issues: #66


@internal
@pure
def scale(fee: uint256, utilization: uint256) -> uint256:
return (fee * utilization) / 100
return (fee * utilization) / 100_000

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/3.

Fixes issues: #66

Copy link
Contributor

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/3.

Fixes issues: #66


@internal
@view
Expand Down
19 changes: 14 additions & 5 deletions gl-sherlock/contracts/positions.vy
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ def value(id: uint256, ctx: Ctx) -> PositionValue:
#
# Positions which go negative due to price fluctuations cost the pool
# EV profits since the most it can make is available collateral.
pool : PoolState = self.POOLS.lookup(pos.pool)
bc : uint256 = pool.base_collateral
qc : uint256 = pool.quote_collateral

Comment on lines +195 to +198
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/2.

Fixes issues: #96

Comment on lines +195 to +198
Copy link
Contributor

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/2.

Fixes issues: #96

deltas: Deltas = Deltas({
base_interest : [self.MATH.MINUS(pos.interest)],
quote_interest : [],
Expand All @@ -206,19 +210,19 @@ def value(id: uint256, ctx: Ctx) -> PositionValue:
quote_transfer : [],
# in the worst case describe above, reserves
# dont change
quote_reserves : [self.MATH.PLUS(pos.collateral), #does not need min()
quote_reserves : [self.MATH.PLUS(min(pos.collateral, qc)),

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/2.

Fixes issues: #96

Copy link
Contributor

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/2.

Fixes issues: #96

self.MATH.MINUS(fees.funding_paid)],
quote_collateral: [self.MATH.PLUS(fees.funding_paid),
self.MATH.MINUS(pos.collateral)],
self.MATH.MINUS(min(pos.collateral, qc))],

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/2.

Fixes issues: #96

Copy link
Contributor

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/2.

Fixes issues: #96

}) if pos.long else Deltas({
base_interest : [],
quote_interest : [self.MATH.MINUS(pos.interest)],

base_transfer : [],
base_reserves : [self.MATH.PLUS(pos.collateral),
base_reserves : [self.MATH.PLUS(min(pos.collateral, bc)),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/2.

Fixes issues: #96

Copy link
Contributor

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/2.

Fixes issues: #96

self.MATH.MINUS(fees.funding_paid)],
base_collateral : [self.MATH.PLUS(fees.funding_paid), # <-
self.MATH.MINUS(pos.collateral)],
self.MATH.MINUS(min(pos.collateral, bc))],

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/2.

Fixes issues: #96

Copy link
Contributor

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/2.

Fixes issues: #96


quote_transfer : [self.MATH.PLUS(pnl.payout),
self.MATH.PLUS(fees.funding_received)],
Expand Down Expand Up @@ -250,8 +254,13 @@ def calc_fees(id: uint256) -> FeesPaid:
c1 : Val = self.deduct(c0, fees.funding_paid)
c2 : Val = self.deduct(c1.remaining, fees.borrowing_paid)
# Funding fees prioritized over borrowing fees.
funding_paid : uint256 = c1.deducted
avail_pay : uint256 = pool.quote_collateral if pos.long else (
pool.base_collateral )
funding_paid : uint256 = min(c1.deducted, avail_pay)
# borrowing_paid is for informational purposes only, could also say
# min(c2.deducted, avail_pay - funding_paid).
Comment on lines +257 to +261

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/2.

Fixes issues: #96

Comment on lines +257 to +261
Copy link
Contributor

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/2.

Fixes issues: #96

borrowing_paid : uint256 = c2.deducted
# other users' bad positions do not affect liquidatability
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/2.

Fixes issues: #96

Copy link
Contributor

Choose a reason for hiding this comment

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

Change related to https://github.com/Velar-co/gl-sherlock/pull/2.

Fixes issues: #96

remaining : uint256 = c2.remaining
# When there are negative positions (liquidation bot failure):
avail : uint256 = pool.base_collateral if pos.long else (
Expand Down