-
Notifications
You must be signed in to change notification settings - Fork 39
/
min_initial_deposit.go
83 lines (69 loc) · 2.58 KB
/
min_initial_deposit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package ante
import (
"fmt"
core "github.com/classic-terra/core/v2/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)
// MinInitialDeposit Decorator will check Initial Deposits for MsgSubmitProposal
type MinInitialDepositDecorator struct {
govKeeper govkeeper.Keeper
treasuryKeeper TreasuryKeeper
}
// NewMinInitialDeposit returns new min initial deposit decorator instance
func NewMinInitialDepositDecorator(govKeeper govkeeper.Keeper, treasuryKeeper TreasuryKeeper) MinInitialDepositDecorator {
return MinInitialDepositDecorator{
govKeeper: govKeeper,
treasuryKeeper: treasuryKeeper,
}
}
// IsMsgSubmitProposal checks whether the input msg is a MsgSubmitProposal
func IsMsgSubmitProposal(msg sdk.Msg) bool {
switch msg.(type) {
case *govv1beta1.MsgSubmitProposal, *govv1.MsgSubmitProposal:
return true
default:
return false
}
}
// HandleCheckMinInitialDeposit
func HandleCheckMinInitialDeposit(ctx sdk.Context, msg sdk.Msg, govKeeper govkeeper.Keeper, treasuryKeeper TreasuryKeeper) (err error) {
var initialDepositCoins sdk.Coins
switch submitPropMsg := msg.(type) {
case *govv1beta1.MsgSubmitProposal:
initialDepositCoins = submitPropMsg.GetInitialDeposit()
case *govv1.MsgSubmitProposal:
initialDepositCoins = submitPropMsg.GetInitialDeposit()
default:
return fmt.Errorf("could not dereference msg as MsgSubmitProposal")
}
minDeposit := govKeeper.GetDepositParams(ctx).MinDeposit
requiredAmount := sdk.NewDecFromInt(minDeposit[0].Amount).Mul(treasuryKeeper.GetMinInitialDepositRatio(ctx)).TruncateInt()
requiredDepositCoins := sdk.NewCoins(
sdk.NewCoin(core.MicroLunaDenom, requiredAmount),
)
if !initialDepositCoins.IsAllGTE(requiredDepositCoins) {
return fmt.Errorf("not enough initial deposit provided. Expected %q; got %q", requiredDepositCoins, initialDepositCoins)
}
return nil
}
// AnteHandle handles checking MsgSubmitProposal
func (midd MinInitialDepositDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if simulate {
return next(ctx, tx, simulate)
}
msgs := tx.GetMsgs()
for _, msg := range msgs {
if !IsMsgSubmitProposal(msg) {
continue
}
err := HandleCheckMinInitialDeposit(ctx, msg, midd.govKeeper, midd.treasuryKeeper)
if err != nil {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, err.Error())
}
}
return next(ctx, tx, simulate)
}