-
Notifications
You must be signed in to change notification settings - Fork 12
/
packet.go
67 lines (60 loc) · 2.46 KB
/
packet.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
package types
import (
"strings"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
var (
// DefaultRelativePacketTimeoutHeight is the default packet timeout height (in blocks) relative
// to the current block height of the counterparty chain provided by the client state. The
// timeout is disabled when set to 0.
DefaultRelativePacketTimeoutHeight = "0-20000"
// DefaultRelativePacketTimeoutTimestamp is the default packet timeout timestamp (in nanoseconds)
// relative to the current block timestamp of the counterparty chain provided by the client
// state. The timeout is disabled when set to 0. The default is currently set to a 12 hour
// timeout.
DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(12) * time.Hour).Nanoseconds())
)
// NewFungibleTokenPacketData contructs a new FungibleTokenPacketData instance
func NewFungibleTokenPacketData(denom, amount, sender, receiver, router string, fee string,
) FungibleTokenPacketData {
return FungibleTokenPacketData{
Denom: denom,
Amount: amount,
Sender: sender,
Receiver: receiver,
Router: router,
Fee: fee,
}
}
// ValidateBasic is used for validating the token transfer.
// NOTE: The addresses formats are not validated as the sender and recipient can have different
// formats defined by their corresponding chains that are not known to IBC.
func (ftpd FungibleTokenPacketData) ValidateBasic() error {
amount, ok := sdk.NewIntFromString(ftpd.Amount)
if !ok {
return sdkerrors.Wrapf(ErrInvalidAmount, "unable to parse transfer amount (%s) into sdk.Int", ftpd.Amount)
}
if !amount.IsPositive() {
return sdkerrors.Wrapf(ErrInvalidAmount, "amount must be strictly positive: got %d", amount)
}
if strings.TrimSpace(ftpd.Sender) == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "sender address cannot be blank")
}
if strings.TrimSpace(ftpd.Receiver) == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "receiver address cannot be blank")
}
fee, ok := sdk.NewIntFromString(ftpd.Fee)
if !ok {
return sdkerrors.Wrapf(ErrInvalidAmount, "unable to parse transfer fee (%s) into sdk.Int", ftpd.Fee)
}
if fee.IsNegative() {
return sdkerrors.Wrapf(ErrInvalidAmount, "fee must be strictly not negative: got %d", fee)
}
return ValidatePrefixedDenom(ftpd.Denom)
}
// GetBytes is a helper for serialising
func (ftpd FungibleTokenPacketData) GetBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&ftpd))
}