-
Notifications
You must be signed in to change notification settings - Fork 41
/
feedistrib.go
51 lines (38 loc) · 1.24 KB
/
feedistrib.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
package ante
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
BurnFeePortion int = iota
RewardsFeePortion
FeePortionCount
)
type DistributionFeeAllocation = [FeePortionCount]sdk.Coins
func GetDistributionFee(ctx sdk.Context, fee sdk.Coins, burnFeePortion sdk.Coins) (DistributionFeeAllocation, error) {
rewardsFeePortion := sdk.NewCoins(fee...).Sub(burnFeePortion...)
distrFeeAlloc := DistributionFeeAllocation{
BurnFeePortion: burnFeePortion,
RewardsFeePortion: rewardsFeePortion,
}
if ValidateDistributionFee(fee, distrFeeAlloc) != nil {
return distrFeeAlloc, sdkerrors.Wrap(sdkerrors.ErrLogic, "fee distribution is invalid")
}
return distrFeeAlloc, nil
}
func SumDistributionFee(distrFeeAlloc DistributionFeeAllocation) sdk.Coins {
sum := sdk.NewCoins()
for _, fee := range distrFeeAlloc {
sum = sum.Add(fee...)
}
return sum
}
func ValidateDistributionFee(fee sdk.Coins, distrFeeAlloc DistributionFeeAllocation) error {
if fee.IsZero() {
return sdkerrors.Wrap(sdkerrors.ErrInsufficientFee, "fee cannot be zero")
}
if !fee.IsEqual(SumDistributionFee(distrFeeAlloc)) {
return sdkerrors.Wrap(sdkerrors.ErrLogic, "fee distribution is invalid")
}
return nil
}