Skip to content

Commit

Permalink
address julien's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
likhita-809 committed Nov 6, 2023
1 parent ae39a2d commit bcd5221
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
15 changes: 15 additions & 0 deletions x/protocolpool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
7 changes: 2 additions & 5 deletions x/protocolpool/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 16 additions & 7 deletions x/protocolpool/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
5 changes: 2 additions & 3 deletions x/protocolpool/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 0 additions & 12 deletions x/protocolpool/types/msg.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package types

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -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,
}
}

0 comments on commit bcd5221

Please sign in to comment.