/
packet-tx.go
60 lines (52 loc) · 1.41 KB
/
packet-tx.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 core
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func SendTransferMsg(src, dst *ProvableChain, amount sdk.Coin, dstAddr string, toHeightOffset uint64, toTimeOffset time.Duration) error {
logger := GetChannelPairLogger(src, dst)
defer logger.TimeTrack(time.Now(), "SendTransferMsg")
var (
timeoutHeight uint64
timeoutTimestamp uint64
)
h, err := dst.LatestHeight()
if err != nil {
return err
}
switch {
case toHeightOffset > 0 && toTimeOffset > 0:
return fmt.Errorf("cant set both timeout height and time offset")
case toHeightOffset > 0:
timeoutHeight = uint64(h.GetRevisionHeight()) + toHeightOffset
timeoutTimestamp = 0
case toTimeOffset > 0:
timeoutHeight = 0
timeoutTimestamp = uint64(time.Now().Add(toTimeOffset).UnixNano())
case toHeightOffset == 0 && toTimeOffset == 0:
timeoutHeight = uint64(h.GetRevisionHeight() + 1000)
timeoutTimestamp = 0
}
srcAddr, err := src.GetAddress()
if err != nil {
logger.Error(
"failed to get address for send transfer",
err,
)
return err
}
// MsgTransfer will call SendPacket on src chain
txs := RelayMsgs{
Src: []sdk.Msg{src.Path().MsgTransfer(
dst.Path(), amount, dstAddr, srcAddr, timeoutHeight, timeoutTimestamp, "",
)},
Dst: []sdk.Msg{},
}
if txs.Send(src, dst); !txs.Success() {
err := fmt.Errorf("failed to send transfer message")
logger.Error(err.Error(), err)
return err
}
return nil
}