Skip to content

Commit

Permalink
Consider module max withdraw in max_withdraw query (#2462)
Browse files Browse the repository at this point in the history
* Consider module max withdraw in max_withdraw query

* Restructure code, previously sdk.MinInt might have been called with nil int

* fix lint issues

---------

Co-authored-by: deficruncher <deficruncher>
Co-authored-by: Robert Zaremba <[email protected]>
  • Loading branch information
deficruncher and robert-zaremba authored Mar 18, 2024
1 parent 0aeea63 commit 32e7c21
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions x/leverage/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,19 +414,32 @@ func (q Querier) MaxWithdraw(
// will be nil and the resulting value will be what
// can safely be withdrawn even with missing prices.
// On non-nil error here, max withdraw is zero.
uToken, _, err := q.userMaxWithdraw(ctx, addr, denom)
if err == nil && uToken.IsPositive() {

userMaxWithdrawUToken, _, err := q.Keeper.userMaxWithdraw(ctx, addr, denom)
if err != nil {
if nonOracleError(err) {
return nil, err
}
continue
}

moduleMaxWithdrawUToken, err := q.Keeper.ModuleMaxWithdraw(ctx, userMaxWithdrawUToken)
if err != nil {
if nonOracleError(err) {
return nil, err
}
continue
}

uToken := sdk.NewCoin(userMaxWithdrawUToken.Denom, sdk.MinInt(userMaxWithdrawUToken.Amount, moduleMaxWithdrawUToken))
if uToken.IsPositive() {
token, err := q.ToToken(ctx, uToken)
if err != nil {
return nil, err
}
maxUTokens = maxUTokens.Add(uToken)
maxTokens = maxTokens.Add(token)
}
// Non-price errors will cause the query itself to fail
if nonOracleError(err) {
return nil, err
}
}

return &types.QueryMaxWithdrawResponse{
Expand Down

0 comments on commit 32e7c21

Please sign in to comment.