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

feat: leverage + historacle #1654

Merged
merged 44 commits into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
5dc0efe
add pump&dump tokens to mock oracle
toteki Dec 12, 2022
84065b5
value functions
toteki Dec 12, 2022
109a2ba
comments, renames
toteki Dec 12, 2022
d09eea6
function signature
toteki Dec 12, 2022
45eb372
no one outpizzas the hut
toteki Dec 12, 2022
db15b24
var
toteki Dec 12, 2022
5ebcae6
implement AssertBorrowerHealth
toteki Dec 12, 2022
c4a61db
implement restrictions - broken tests
toteki Dec 12, 2022
f481911
refactor MsgCollateralize
toteki Dec 12, 2022
37298a5
move some tests from Keeper to MsgServer due to liquidity checks bein…
toteki Dec 12, 2022
49fbedf
MaxSuply check moved
toteki Dec 12, 2022
7958af4
Merge branch 'main' into adam/hist
toteki Dec 15, 2022
588b011
use AvailableMedians return value
toteki Dec 15, 2022
0641e25
lint++
toteki Dec 15, 2022
899130c
Merge branch 'main' into adam/hist
adamewozniak Dec 20, 2022
107ae1f
previous tests fully refactored
toteki Dec 21, 2022
58f2273
fix case - new error precedence
toteki Dec 21, 2022
32cc4d8
reorder
toteki Dec 21, 2022
b44be59
test msg MaxWithdraw
toteki Dec 21, 2022
3ba51e6
add additional test case
toteki Dec 21, 2022
8001bcb
modify maxWithdraw and query to use minimum of historic and current max
toteki Dec 21, 2022
2529cb9
Merge branch 'main' into adam/hist
toteki Dec 21, 2022
9625469
Update x/leverage/keeper/borrows.go
toteki Dec 21, 2022
4e881b5
Merge branch 'main' into adam/hist
toteki Dec 22, 2022
046c835
Update x/leverage/keeper/oracle.go
toteki Dec 22, 2022
1dc6d21
lint++
toteki Dec 22, 2022
8c6522f
Merge branch 'main' into adam/hist
adamewozniak Dec 23, 2022
58ff6e6
Merge branch 'main' into adam/hist
adamewozniak Dec 27, 2022
034757a
historic withdraw test cases
toteki Dec 27, 2022
f9ce975
max withdraw scenarios - historic
toteki Dec 27, 2022
726d74a
historic decollateralize cases
toteki Dec 28, 2022
4b97faa
historic borrow cases
toteki Dec 28, 2022
8770428
helper funtion
toteki Dec 28, 2022
285885a
fail-safe on no historic prices - breaks tests
toteki Dec 28, 2022
3a4c499
Merge branch 'main' into adam/hist
toteki Dec 29, 2022
f151eb6
Update x/leverage/keeper/msg_server_test.go
toteki Dec 29, 2022
7385533
Update x/leverage/keeper/msg_server_test.go
toteki Dec 29, 2022
93cfa64
Update x/leverage/keeper/msg_server_test.go
toteki Dec 29, 2022
6421445
Update x/leverage/keeper/msg_server_test.go
toteki Dec 29, 2022
74dacb8
Update x/leverage/keeper/msg_server_test.go
toteki Dec 29, 2022
7eea870
fixed leverage simulations mock oracle
toteki Dec 30, 2022
f07af39
fix leverage CLI test mock oracle
toteki Dec 30, 2022
7d867cf
cl++
toteki Dec 30, 2022
d710901
Merge branch 'main' into adam/hist
toteki Jan 1, 2023
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
Prev Previous commit
Next Next commit
modify maxWithdraw and query to use minimum of historic and current max
  • Loading branch information
toteki committed Dec 21, 2022
commit 8001bcb2884fc8f83f85f27269eed324f9af95dc
11 changes: 10 additions & 1 deletion x/leverage/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,19 @@ func (q Querier) MaxWithdraw(
return nil, err
}

uToken, err := q.Keeper.maxWithdraw(ctx, addr, req.Denom)
maxCurrentWithdraw, err := q.Keeper.maxWithdraw(ctx, addr, req.Denom, false)
if err != nil {
return nil, err
}
maxHistoricWithdraw, err := q.Keeper.maxWithdraw(ctx, addr, req.Denom, true)
if err != nil {
return nil, err
}

uToken := sdk.NewCoin(
maxCurrentWithdraw.Denom,
sdk.MinInt(maxCurrentWithdraw.Amount, maxHistoricWithdraw.Amount),
)

token, err := q.Keeper.ExchangeUToken(ctx, uToken)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions x/leverage/keeper/limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

// maxWithdraw calculates the maximum amount of uTokens an account can currently withdraw.
// input denom should be a base token.
func (k *Keeper) maxWithdraw(ctx sdk.Context, addr sdk.AccAddress, denom string) (sdk.Coin, error) {
// input denom should be a base token. Uses either real or historic prices.
func (k *Keeper) maxWithdraw(ctx sdk.Context, addr sdk.AccAddress, denom string, historic bool) (sdk.Coin, error) {
if types.HasUTokenPrefix(denom) {
return sdk.Coin{}, types.ErrUToken
}
Expand All @@ -25,7 +25,7 @@ func (k *Keeper) maxWithdraw(ctx sdk.Context, addr sdk.AccAddress, denom string)
specificCollateral := sdk.NewCoin(uDenom, totalCollateral.AmountOf(uDenom))

// calculate borrowed value for the account
borrowedValue, err := k.TotalTokenValue(ctx, totalBorrowed, false)
borrowedValue, err := k.TotalTokenValue(ctx, totalBorrowed, historic)
if err != nil {
return sdk.Coin{}, err
}
Expand All @@ -38,7 +38,7 @@ func (k *Keeper) maxWithdraw(ctx sdk.Context, addr sdk.AccAddress, denom string)
}

// for nonzero borrows, calculations are based on unused borrow limit
borrowLimit, err := k.CalculateBorrowLimit(ctx, totalCollateral, false)
borrowLimit, err := k.CalculateBorrowLimit(ctx, totalCollateral, historic)
if err != nil {
return sdk.Coin{}, err
}
Expand All @@ -52,7 +52,7 @@ func (k *Keeper) maxWithdraw(ctx sdk.Context, addr sdk.AccAddress, denom string)
unusedBorrowLimit := borrowLimit.Sub(borrowedValue)

// calculate the contribution to borrow limit made by only the type of collateral being withdrawn
specificBorrowLimit, err := k.CalculateBorrowLimit(ctx, sdk.NewCoins(specificCollateral), false)
specificBorrowLimit, err := k.CalculateBorrowLimit(ctx, sdk.NewCoins(specificCollateral), historic)
if err != nil {
return sdk.Coin{}, err
}
Expand Down
11 changes: 10 additions & 1 deletion x/leverage/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,19 @@ func (s msgServer) MaxWithdraw(
return nil, err
}

uToken, err := s.keeper.maxWithdraw(ctx, supplierAddr, msg.Denom)
maxCurrentWithdraw, err := s.keeper.maxWithdraw(ctx, supplierAddr, msg.Denom, false)
if err != nil {
return nil, err
}
maxHistoricWithdraw, err := s.keeper.maxWithdraw(ctx, supplierAddr, msg.Denom, true)
if err != nil {
return nil, err
}

uToken := sdk.NewCoin(
maxCurrentWithdraw.Denom,
sdk.MinInt(maxCurrentWithdraw.Amount, maxHistoricWithdraw.Amount),
)

if uToken.IsZero() {
return nil, types.ErrMaxWithdrawZero
Expand Down