/
withdraw_authorizaton.go
64 lines (55 loc) · 1.82 KB
/
withdraw_authorizaton.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
package types
import (
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrtypes "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/authz"
)
var _ authz.Authorization = &WithdrawAuthorization{}
// NewWithdrawAuthorization creates a new WithdrawAuthorization object.
func NewWithdrawAuthorization(withdrawLimit sdkmath.Int) *WithdrawAuthorization {
return &WithdrawAuthorization{
WithdrawLimit: withdrawLimit,
}
}
// MsgTypeURL implements Authorization.MsgTypeURL.
func (WithdrawAuthorization) MsgTypeURL() string {
return sdk.MsgTypeURL(&MsgWithdraw{})
}
// Accept implements Authorization.Accept.
func (a WithdrawAuthorization) Accept(_ sdk.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
mWithdraw, ok := msg.(*MsgWithdraw)
if !ok {
return authz.AcceptResponse{}, sdkerrtypes.ErrInvalidType.Wrap("type mismatch")
}
limitLeft := a.WithdrawLimit.Sub(mWithdraw.Amount)
if limitLeft.IsNegative() {
return authz.AcceptResponse{}, sdkerrtypes.ErrInsufficientFunds.Wrapf(
"requested amount is more than withdraw limit",
)
}
if limitLeft.IsZero() {
return authz.AcceptResponse{Accept: true, Delete: true}, nil
}
return authz.AcceptResponse{
Accept: true,
Delete: false,
Updated: &WithdrawAuthorization{WithdrawLimit: limitLeft},
}, nil
}
// ValidateBasic implements Authorization.ValidateBasic.
func (a WithdrawAuthorization) ValidateBasic() error {
if a.WithdrawLimit.IsNil() {
return sdkerrtypes.ErrInvalidCoins.Wrap("withdraw limit cannot be nil")
}
if a.WithdrawLimit.LTE(sdk.ZeroInt()) {
return sdkerrtypes.ErrInvalidCoins.Wrap("withdraw limit cannot be less than or equal to zero")
}
if a.WithdrawLimit.GT(maxWithdrawGrant) {
return sdkerrtypes.ErrInvalidCoins.Wrapf(
"withdraw limit cannot be grated than %s",
maxWithdrawGrant,
)
}
return nil
}