-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
324 lines (276 loc) · 9.38 KB
/
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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package cli
import (
"fmt"
ibcnfttransfertypes "github.com/bianjieai/nft-transfer/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
channelutils "github.com/cosmos/ibc-go/v7/modules/core/04-channel/client/utils"
"strings"
"github.com/spf13/cobra"
"github.com/UptickNetwork/wasm-nft-convert/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"time"
)
// NewTxCmd returns a root CLI command handler for cw721 transaction commands
func NewTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "cw721 subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
txCmd.AddCommand(
NewConvertNFTCmd(),
NewConvertCW721Cmd(),
NewTransferCW721Cmd(),
)
return txCmd
}
// NewConvertNFTCmd returns a CLI command handler for converting a Cosmos coin
func NewConvertNFTCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "convert-nft [class_id] [nft_ids] [contract_address] [token_ids] [receiver_hex]",
Short: "Convert a Cosmos nft to cw721. When the receiver [optional] is omitted, the cw721 tokens are transferred to the sender.",
Args: cobra.RangeArgs(4, 5),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
classID := args[0]
if len(classID) == 0 {
return fmt.Errorf("classId can not be empty")
}
if args[1] == "" {
return fmt.Errorf("nft IDs can not be empty")
}
nftIDs := strings.Split(args[1], ",")
if len(nftIDs) == 0 {
return fmt.Errorf("nftID can not be empty")
}
contractAddress := args[2]
//if contractAddress == "" {
// return fmt.Errorf("contract address can not be empty")
//}
//if args[3] == "" {
// return fmt.Errorf("token IDs can not be empty")
//}
tokenIDs := strings.Split(args[3], ",")
//if len(tokenIDs) == 0 {
// return fmt.Errorf("token IDs can not be empty")
//}
var receiver string
sender := cliCtx.GetFromAddress()
if len(args) == 5 {
receiver = args[4]
} else {
receiver = sender.String()
}
msg := &types.MsgConvertNFT{
ContractAddress: contractAddress,
NftIds: nftIDs,
ClassId: classID,
TokenIds: tokenIDs,
Receiver: receiver,
Sender: sender.String(),
}
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewConvertCW721Cmd returns a CLI command handler for converting an cw721
func NewConvertCW721Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "convert-cw721 [contract_address] [token_ids] [class_id] [nft_ids] [receiver]",
Short: "Convert an cw721 token to Cosmos coin. " +
"When the receiver [optional] is omitted, the Cosmos coins are transferred to the sender.",
Args: cobra.RangeArgs(4, 5),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
contractAddress := args[0]
if len(contractAddress) == 0 {
return fmt.Errorf("contract address can not be empty")
}
if args[1] == "" {
return fmt.Errorf("token IDs can not be empty")
}
tokenIDs := strings.Split(args[1], ",")
if len(tokenIDs) == 0 {
return fmt.Errorf("tokenID can not be empty")
}
// from := common.BytesToAddress(cliCtx.GetFromAddress().Bytes())
from := cliCtx.GetFromAddress()
classID := args[2]
//if len(classID) == 0 {
// return fmt.Errorf("classId can not be empty")
//}
//if args[3] == "" {
// return fmt.Errorf("nft IDs can not be empty")
//}
nftIDs := strings.Split(args[3], ",")
//if len(nftIDs) == 0 {
// return fmt.Errorf("nft IDs can not be empty")
//}
receiver := cliCtx.GetFromAddress()
if len(args) == 5 {
receiver, err = sdk.AccAddressFromBech32(args[4])
if err != nil {
return err
}
}
msg := &types.MsgConvertCW721{
ContractAddress: contractAddress,
TokenIds: tokenIDs,
Receiver: receiver.String(),
Sender: from.String(),
ClassId: classID,
NftIds: nftIDs,
}
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
const (
flagPacketTimeoutHeight = "packet-timeout-height"
flagPacketTimeoutTimestamp = "packet-timeout-timestamp"
flagPacketMemo = "packet-memo"
flagAbsoluteTimeouts = "absolute-timeouts"
)
// NewTransferCW721Cmd returns a CLI command handler for converting an cw721
func NewTransferCW721Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "ibc-transfer-cw721 [cw_contract_address] [cw_token_ids] [src_port] [src_channel] [cosmos_receiver] [class_id] [cosmos_token_ids]",
Short: "Convert an cw721 token to Cosmos coin and transfer a non-fungible token through IBC " +
"When the receiver [optional] is omitted, the Cosmos coins are transferred to the sender.",
Args: cobra.RangeArgs(7, 8),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
cwSender := cliCtx.GetFromAddress()
cwContractAddress := args[0]
if cwContractAddress == "" {
return fmt.Errorf("contract address can not be empty")
}
if args[1] == "" {
return fmt.Errorf("wasm token IDs can not be empty")
}
cwTokenIds := strings.Split(args[1], ",")
if len(cwTokenIds) == 0 {
return fmt.Errorf("tokenID can not be empty")
}
sourcePort := args[2]
if sourcePort == "" {
return fmt.Errorf("source port address can not be empty")
}
sourceChannel := args[3]
if sourceChannel == "" {
return fmt.Errorf("source channel address can not be empty")
}
cosmosReceiver := args[4]
if cosmosReceiver == "" {
return fmt.Errorf("cosmos channel address can not be empty")
}
classId := args[5]
//if classId == "" {
// return fmt.Errorf("classId can not be empty")
//}
//if args[6] == "" {
// return fmt.Errorf("cosmos token IDs can not be empty")
//}
cosmosTokenIds := strings.Split(args[6], ",")
//if len(cwTokenIds) == 0 {
// return fmt.Errorf("tokenIDs cannot be empty")
//}
timeoutHeightStr, err := cmd.Flags().GetString(flagPacketTimeoutHeight)
if err != nil {
return err
}
timeoutHeight, err := clienttypes.ParseHeight(timeoutHeightStr)
if err != nil {
return err
}
timeoutTimestamp, err := cmd.Flags().GetUint64(flagPacketTimeoutTimestamp)
if err != nil {
return err
}
absoluteTimeouts, err := cmd.Flags().GetBool(flagAbsoluteTimeouts)
if err != nil {
return err
}
memo, err := cmd.Flags().GetString(flagPacketMemo)
if err != nil {
return err
}
// if the timeouts are not absolute, retrieve latest block height and block timestamp
// for the consensus state connected to the destination port/channel
if !absoluteTimeouts {
consensusState, height, _, err := channelutils.QueryLatestConsensusState(cliCtx, sourcePort, sourceChannel)
if err != nil {
return 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 {
// return errors.New("local clock time is not greater than Jan 1st, 1970 12:00 AM")
return fmt.Errorf("tokenIDs cannot be empty")
}
}
}
msg := &types.MsgTransferCW721{
CwContractAddress: cwContractAddress,
CwTokenIds: cwTokenIds,
SourcePort: sourcePort,
SourceChannel: sourceChannel,
ClassId: classId,
CosmosTokenIds: cosmosTokenIds,
CwSender: cwSender.String(),
CosmosReceiver: cosmosReceiver,
TimeoutHeight: timeoutHeight,
TimeoutTimestamp: timeoutTimestamp,
Memo: memo,
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(flagPacketTimeoutHeight, ibcnfttransfertypes.DefaultRelativePacketTimeoutHeight, "Packet timeout block height. The timeout is disabled when set to 0-0.")
cmd.Flags().Uint64(flagPacketTimeoutTimestamp, ibcnfttransfertypes.DefaultRelativePacketTimeoutTimestamp, "Packet timeout timestamp in nanoseconds from now. Default is 10 minutes. The timeout is disabled when set to 0.")
cmd.Flags().String(flagPacketMemo, "", "Packet memo. Default is empty")
cmd.Flags().Bool(flagAbsoluteTimeouts, false, "Timeout flags are used as absolute timeouts.")
flags.AddTxFlagsToCmd(cmd)
return cmd
}