-
Notifications
You must be signed in to change notification settings - Fork 580
/
fee.go
92 lines (76 loc) · 2.55 KB
/
fee.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
84
85
86
87
88
89
90
91
92
package types
import (
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
)
// NewPacketFee creates and returns a new PacketFee struct including the incentivization fees, refund address and relayers
func NewPacketFee(fee Fee, refundAddr string, relayers []string) PacketFee {
return PacketFee{
Fee: fee,
RefundAddress: refundAddr,
Relayers: relayers,
}
}
// Validate performs basic stateless validation of the associated PacketFee
func (p PacketFee) Validate() error {
_, err := sdk.AccAddressFromBech32(p.RefundAddress)
if err != nil {
return sdkerrors.Wrap(err, "failed to convert RefundAddress into sdk.AccAddress")
}
// enforce relayers are not set
if len(p.Relayers) != 0 {
return ErrRelayersNotEmpty
}
if err := p.Fee.Validate(); err != nil {
return err
}
return nil
}
// NewPacketFees creates and returns a new PacketFees struct including a list of type PacketFee
func NewPacketFees(packetFees []PacketFee) PacketFees {
return PacketFees{
PacketFees: packetFees,
}
}
// NewIdentifiedPacketFees creates and returns a new IdentifiedPacketFees struct containing a packet ID and packet fees
func NewIdentifiedPacketFees(packetID channeltypes.PacketId, packetFees []PacketFee) IdentifiedPacketFees {
return IdentifiedPacketFees{
PacketId: packetID,
PacketFees: packetFees,
}
}
// NewFee creates and returns a new Fee struct encapsulating the receive, acknowledgement and timeout fees as sdk.Coins
func NewFee(recvFee, ackFee, timeoutFee sdk.Coins) Fee {
return Fee{
RecvFee: recvFee,
AckFee: ackFee,
TimeoutFee: timeoutFee,
}
}
// Total returns the total amount for a given Fee
func (f Fee) Total() sdk.Coins {
return f.RecvFee.Add(f.AckFee...).Add(f.TimeoutFee...)
}
// Validate asserts that each Fee is valid and all three Fees are not empty or zero
func (f Fee) Validate() error {
var errFees []string
if !f.AckFee.IsValid() {
errFees = append(errFees, "ack fee invalid")
}
if !f.RecvFee.IsValid() {
errFees = append(errFees, "recv fee invalid")
}
if !f.TimeoutFee.IsValid() {
errFees = append(errFees, "timeout fee invalid")
}
if len(errFees) > 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "contains invalid fees: %s", strings.Join(errFees, " , "))
}
// if all three fee's are zero or empty return an error
if f.AckFee.IsZero() && f.RecvFee.IsZero() && f.TimeoutFee.IsZero() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "all fees are zero")
}
return nil
}