-
Notifications
You must be signed in to change notification settings - Fork 661
/
cmd.go
70 lines (58 loc) · 1.42 KB
/
cmd.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package export
import (
"encoding/json"
"log"
"time"
"github.com/spf13/cobra"
"github.com/ava-labs/avalanchego/vms/example/xsvm/api"
"github.com/ava-labs/avalanchego/vms/example/xsvm/tx"
)
func Command() *cobra.Command {
c := &cobra.Command{
Use: "export",
Short: "Issues an export transaction",
RunE: exportFunc,
}
flags := c.Flags()
AddFlags(flags)
return c
}
func exportFunc(c *cobra.Command, args []string) error {
flags := c.Flags()
config, err := ParseFlags(flags, args)
if err != nil {
return err
}
ctx := c.Context()
client := api.NewClient(config.URI, config.SourceChainID.String())
nonce, err := client.Nonce(ctx, config.PrivateKey.Address())
if err != nil {
return err
}
utx := &tx.Export{
ChainID: config.SourceChainID,
Nonce: nonce,
MaxFee: config.MaxFee,
PeerChainID: config.DestinationChainID,
IsReturn: config.IsReturn,
Amount: config.Amount,
To: config.To,
}
stx, err := tx.Sign(utx, config.PrivateKey)
if err != nil {
return err
}
txJSON, err := json.MarshalIndent(stx, "", " ")
if err != nil {
return err
}
issueTxStartTime := time.Now()
txID, err := client.IssueTx(ctx, stx)
if err != nil {
return err
}
log.Printf("issued tx %s in %s\n%s\n", txID, time.Since(issueTxStartTime), string(txJSON))
return nil
}