From 32e7c21c76065c76c586c9cce9d1fd42171b6b0c Mon Sep 17 00:00:00 2001 From: deficruncher <163729542+deficruncher@users.noreply.github.com> Date: Mon, 18 Mar 2024 22:01:31 +0100 Subject: [PATCH] Consider module max withdraw in max_withdraw query (#2462) * 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 Co-authored-by: Robert Zaremba --- x/leverage/keeper/grpc_query.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/x/leverage/keeper/grpc_query.go b/x/leverage/keeper/grpc_query.go index 522efe9d0a..d05381fa3e 100644 --- a/x/leverage/keeper/grpc_query.go +++ b/x/leverage/keeper/grpc_query.go @@ -414,8 +414,25 @@ 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 @@ -423,10 +440,6 @@ func (q Querier) MaxWithdraw( 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{