/
fee.go
60 lines (49 loc) · 1.48 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
package types
import (
"strings"
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
func NewPacketID(portID, channelID string, sequence uint64) PacketID {
return PacketID{
ChannelId: channelID,
PortId: portID,
Sequence: sequence,
}
}
// 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 (m Fee) Total() sdk.Coins {
return m.RecvFee.Add(m.AckFee...).Add(m.TimeoutFee...)
}
// Validate asserts that each Fee is valid:
// * RecvFee must be zero;
// * AckFee and TimeoutFee must be non-zero
func (m Fee) Validate() error {
var errFees []string
if !m.AckFee.IsValid() {
errFees = append(errFees, "ack fee is invalid")
}
if !m.TimeoutFee.IsValid() {
errFees = append(errFees, "timeout fee invalid")
}
if len(errFees) > 0 {
return errors.Wrapf(sdkerrors.ErrInvalidCoins, "contains invalid fees: %s", strings.Join(errFees, " , "))
}
if !m.RecvFee.IsZero() {
return errors.Wrapf(sdkerrors.ErrInvalidCoins, "recv fee must be zero")
}
// if ack or timeout fees are zero or empty return an error
if m.AckFee.IsZero() || m.TimeoutFee.IsZero() {
return errors.Wrap(sdkerrors.ErrInvalidCoins, "ack fee or timeout fee is zero")
}
return nil
}