-
Notifications
You must be signed in to change notification settings - Fork 2
/
ibctransfer.go
177 lines (150 loc) · 5.34 KB
/
ibctransfer.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package tx
import (
"context"
"errors"
"fmt"
"time"
"github.com/b-harvest/modules-test-tool/client"
"github.com/spf13/cobra"
sdkclient "github.com/cosmos/cosmos-sdk/client"
sdkclienttx "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
ibctypes "github.com/cosmos/ibc-go/v2/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v2/modules/core/02-client/types"
channelutils "github.com/cosmos/ibc-go/v2/modules/core/04-channel/client/utils"
)
// Transaction is an object that has common fields when signing transaction.
type IBCtransaction struct {
Client *client.Client `json:"client"`
ChainID string `json:"chain_id"`
GasLimit uint64 `json:"gas_limit"`
Fees sdktypes.Coins `json:"fees"`
Memo string `json:"memo"`
}
const (
flagPacketTimeoutHeight = "packet-timeout-height"
flagPacketTimeoutTimestamp = "packet-timeout-timestamp"
flagAbsoluteTimeouts = "absolute-timeouts"
)
// NewTransaction returns new Transaction object.
func IbcNewtransaction(client *client.Client, chainID string, gasLimit uint64, fees sdktypes.Coins, memo string) *Transaction {
return &Transaction{
Client: client,
ChainID: chainID,
GasLimit: gasLimit,
Fees: fees,
Memo: memo,
}
}
// MsgCreatePool creates create pool message and returns MsgCreatePool transaction message.
func MsgTransfer(cmd *cobra.Command, ctx sdkclient.Context, srcPort string, srcChannel string, coin sdktypes.Coin, sender string, receiver string) (sdktypes.Msg, error) {
//prefix := strings.Split(sender, "1")
//ibcsender, err := sdktypes.GetFromBech32(sender, prefix[0])
//if err != nil {
// return &ibctypes.MsgTransfer{}, err
//}
timeoutHeightStr, err := cmd.Flags().GetString(flagPacketTimeoutHeight)
if err != nil {
return nil, err
}
timeoutHeight, err := clienttypes.ParseHeight(timeoutHeightStr)
if err != nil {
return nil, err
}
timeoutTimestamp, err := cmd.Flags().GetUint64(flagPacketTimeoutTimestamp)
if err != nil {
return nil, err
}
absoluteTimeouts, err := cmd.Flags().GetBool(flagAbsoluteTimeouts)
if err != nil {
return nil, err
}
if !absoluteTimeouts {
consensusState, height, _, err := channelutils.QueryLatestConsensusState(ctx, srcPort, srcChannel)
if err != nil {
return nil, err
}
if !timeoutHeight.IsZero() {
absoluteHeight := height
absoluteHeight.RevisionNumber += timeoutHeight.RevisionNumber
absoluteHeight.RevisionHeight += timeoutHeight.RevisionHeight
timeoutHeight = absoluteHeight
}
if timeoutTimestamp != 0 {
// use local clock time as reference time if it is later than the
// consensus state timestamp of the counter party chain, otherwise
// still use consensus state timestamp as reference
now := time.Now().UnixNano()
consensusStateTimestamp := consensusState.GetTimestamp()
if now > 0 {
now := uint64(now)
if now > consensusStateTimestamp {
timeoutTimestamp = now + timeoutTimestamp
} else {
timeoutTimestamp = consensusStateTimestamp + timeoutTimestamp
}
} else {
println(errors.New("local clock time is not greater than Jan 1st, 1970 12:00 AM"))
}
}
}
msg := ibctypes.NewMsgTransfer(srcPort, srcChannel, coin, sender, receiver, timeoutHeight, timeoutTimestamp)
if err := msg.ValidateBasic(); err != nil {
return &ibctypes.MsgTransfer{}, err
}
msg.Sender = sender
return msg, nil
}
func (t *Transaction) CreateTransferBot(cmd *cobra.Command, ctx sdkclient.Context, srcPort string, srcChannel string, coin sdktypes.Coin, sender string, receiver string, msgNum int) ([]sdktypes.Msg, error) {
var msgs []sdktypes.Msg
for i := 0; i < msgNum; i++ {
msg, err := MsgTransfer(cmd, ctx, srcPort, srcChannel, coin, sender, receiver)
if err != nil {
return []sdktypes.Msg{}, err
}
msgs = append(msgs, msg)
}
return msgs, nil
}
// Sign signs message(s) with the account's private key and braodacasts the message(s).
func (t *Transaction) IbcSign(ctx context.Context, accSeq uint64, accNum uint64, privKey *secp256k1.PrivKey, msgs ...sdktypes.Msg) ([]byte, error) {
txBuilder := t.Client.CliCtx.TxConfig.NewTxBuilder()
txBuilder.SetMsgs(msgs...)
txBuilder.SetGasLimit(t.GasLimit)
txBuilder.SetFeeAmount(t.Fees)
txBuilder.SetMemo(t.Memo)
signMode := t.Client.CliCtx.TxConfig.SignModeHandler().DefaultMode()
sigV2 := signing.SignatureV2{
PubKey: privKey.PubKey(),
Data: &signing.SingleSignatureData{
SignMode: signMode,
Signature: nil,
},
Sequence: accSeq,
}
err := txBuilder.SetSignatures(sigV2)
if err != nil {
return nil, fmt.Errorf("failed to set signatures: %s", err)
}
signerData := authsigning.SignerData{
ChainID: t.ChainID,
AccountNumber: accNum,
Sequence: accSeq,
}
sigV2, err = sdkclienttx.SignWithPrivKey(signMode, signerData, txBuilder, privKey, t.Client.CliCtx.TxConfig, accSeq)
if err != nil {
return nil, fmt.Errorf("failed to sign with private key: %s", err)
}
err = txBuilder.SetSignatures(sigV2)
if err != nil {
return nil, fmt.Errorf("failed to set signatures: %s", err)
}
txByte, err := t.Client.CliCtx.TxConfig.TxEncoder()(txBuilder.GetTx())
if err != nil {
return nil, fmt.Errorf("failed to encode tx and get raw tx data: %s", err)
}
return txByte, nil
}