From bcd522101b28064810f8bd8aa1782cf23fe9eea9 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Mon, 6 Nov 2023 16:09:28 +0530 Subject: [PATCH] address julien's comments --- x/protocolpool/README.md | 15 +++++++++++++++ x/protocolpool/keeper/grpc_query.go | 7 ++----- x/protocolpool/keeper/keeper.go | 23 ++++++++++++++++------- x/protocolpool/keeper/msg_server.go | 5 ++--- x/protocolpool/types/msg.go | 12 ------------ 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/x/protocolpool/README.md b/x/protocolpool/README.md index 5a610d1c277e..6e9f6021c5ea 100644 --- a/x/protocolpool/README.md +++ b/x/protocolpool/README.md @@ -112,6 +112,21 @@ The message will fail under the following conditions: * The number of tranches is not a positive integer. * The period length is not a positive integer. +*Disclaimer: The behavior described here is based on the current implementation and may be subject to changes in future versions.* +```go +func (k MsgServer) SubmitBudgetProposal(ctx context.Context, msg *types.MsgSubmitBudgetProposal) (*types.MsgSubmitBudgetProposalResponse, error) { + // ... (existing code) + + // Set the budget proposal in the state. + err = k.BudgetProposal.Set(ctx, recipient, *budgetProposal) + if err != nil { + return nil, err + } + + // ... (rest of the code) +} +``` + ```go reference https://github.com/cosmos/cosmos-sdk/blob/97724493d792517ba2be8969078b5f92ad04d79c/x/protocolpool/keeper/msg_server.go#L39-l61 ``` diff --git a/x/protocolpool/keeper/grpc_query.go b/x/protocolpool/keeper/grpc_query.go index 1d80714c9a3f..2786fd72eb3f 100644 --- a/x/protocolpool/keeper/grpc_query.go +++ b/x/protocolpool/keeper/grpc_query.go @@ -53,13 +53,10 @@ func (k Querier) UnclaimedBudget(ctx context.Context, req *types.QueryUnclaimedB var unclaimedBudget sdk.Coin if budget.ClaimedAmount == nil { unclaimedBudget = *budget.TotalBudget - } else { - unclaimedBudget = budget.TotalBudget.Sub(*budget.ClaimedAmount) - } - - if budget.ClaimedAmount == nil { zeroCoin := sdk.NewCoin(budget.TotalBudget.Denom, math.ZeroInt()) budget.ClaimedAmount = &zeroCoin + } else { + unclaimedBudget = budget.TotalBudget.Sub(*budget.ClaimedAmount) } if budget.NextClaimFrom == nil { diff --git a/x/protocolpool/keeper/keeper.go b/x/protocolpool/keeper/keeper.go index 57e940fc0867..ed8af7e152fa 100644 --- a/x/protocolpool/keeper/keeper.go +++ b/x/protocolpool/keeper/keeper.go @@ -188,13 +188,13 @@ func (k Keeper) calculateClaimableFunds(ctx context.Context, recipient sdk.AccAd return amount, nil } -func (k Keeper) validateAndUpdateBudgetProposal(ctx context.Context, bp types.MsgSubmitBudgetProposal) error { +func (k Keeper) validateAndUpdateBudgetProposal(ctx context.Context, bp *types.MsgSubmitBudgetProposal) (*types.Budget, error) { if bp.TotalBudget.IsZero() { - return fmt.Errorf("invalid budget proposal: total budget cannot be zero") + return nil, fmt.Errorf("invalid budget proposal: total budget cannot be zero") } if err := validateAmount(sdk.NewCoins(*bp.TotalBudget)); err != nil { - return fmt.Errorf("invalid budget proposal: %w", err) + return nil, fmt.Errorf("invalid budget proposal: %w", err) } currentTime := sdk.UnwrapSDKContext(ctx).BlockTime() @@ -204,16 +204,25 @@ func (k Keeper) validateAndUpdateBudgetProposal(ctx context.Context, bp types.Ms // if bp.StartTime < uint64(currentTime) { if currentTime.After(*bp.StartTime) { - return fmt.Errorf("invalid budget proposal: start time cannot be less than the current block time") + return nil, fmt.Errorf("invalid budget proposal: start time cannot be less than the current block time") } if bp.Tranches == 0 { - return fmt.Errorf("invalid budget proposal: tranches must be greater than zero") + return nil, fmt.Errorf("invalid budget proposal: tranches must be greater than zero") } if bp.Period == nil || *bp.Period == 0 { - return fmt.Errorf("invalid budget proposal: period length should be greater than zero") + return nil, fmt.Errorf("invalid budget proposal: period length should be greater than zero") } - return nil + // Create and return an updated budget proposal + updatedBudget := types.Budget{ + RecipientAddress: bp.RecipientAddress, + TotalBudget: bp.TotalBudget, + StartTime: bp.StartTime, + Tranches: bp.Tranches, + Period: bp.Period, + } + + return &updatedBudget, nil } diff --git a/x/protocolpool/keeper/msg_server.go b/x/protocolpool/keeper/msg_server.go index 43098528aad1..7206e5354848 100644 --- a/x/protocolpool/keeper/msg_server.go +++ b/x/protocolpool/keeper/msg_server.go @@ -46,12 +46,11 @@ func (k MsgServer) SubmitBudgetProposal(ctx context.Context, msg *types.MsgSubmi return nil, err } - if err := k.validateAndUpdateBudgetProposal(ctx, *msg); err != nil { + budgetProposal, err := k.validateAndUpdateBudgetProposal(ctx, msg) + if err != nil { return nil, err } - budgetProposal := types.NewBudgetProposal(msg.RecipientAddress, *msg.TotalBudget, msg.StartTime, msg.Tranches, msg.Period) - // set budget proposal in state err = k.BudgetProposal.Set(ctx, recipient, *budgetProposal) if err != nil { diff --git a/x/protocolpool/types/msg.go b/x/protocolpool/types/msg.go index b6c4925c8175..c25616d8b584 100644 --- a/x/protocolpool/types/msg.go +++ b/x/protocolpool/types/msg.go @@ -1,8 +1,6 @@ package types import ( - "time" - sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -29,13 +27,3 @@ func NewCommunityPoolSpend(amount sdk.Coins, authority, recipient string) *MsgCo Amount: amount, } } - -func NewBudgetProposal(recipient string, totalBudget sdk.Coin, startTime *time.Time, tranches uint64, period *time.Duration) *Budget { - return &Budget{ - RecipientAddress: recipient, - TotalBudget: &totalBudget, - StartTime: startTime, - Tranches: tranches, - Period: period, - } -}