-
Notifications
You must be signed in to change notification settings - Fork 672
/
cmd.go
85 lines (74 loc) · 1.93 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package create
import (
"log"
"time"
"github.com/spf13/cobra"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/example/xsvm/genesis"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
)
func Command() *cobra.Command {
c := &cobra.Command{
Use: "create",
Short: "Creates a new chain",
RunE: createFunc,
}
flags := c.Flags()
AddFlags(flags)
return c
}
func createFunc(c *cobra.Command, args []string) error {
flags := c.Flags()
config, err := ParseFlags(flags, args)
if err != nil {
return err
}
ctx := c.Context()
kc := secp256k1fx.NewKeychain(config.PrivateKey)
// NewWalletFromURI fetches the available UTXOs owned by [kc] on the network
// that [uri] is hosting.
walletSyncStartTime := time.Now()
wallet, err := primary.MakeWallet(ctx, &primary.WalletConfig{
URI: config.URI,
AVAXKeychain: kc,
EthKeychain: kc,
PChainTxsToFetch: set.Of(config.SubnetID),
})
if err != nil {
return err
}
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
// Get the P-chain wallet
pWallet := wallet.P()
genesisBytes, err := genesis.Codec.Marshal(genesis.CodecVersion, &genesis.Genesis{
Timestamp: 0,
Allocations: []genesis.Allocation{
{
Address: config.Address,
Balance: config.Balance,
},
},
})
if err != nil {
return err
}
createChainStartTime := time.Now()
createChainTxID, err := pWallet.IssueCreateChainTx(
config.SubnetID,
genesisBytes,
constants.XSVMID,
nil,
config.Name,
common.WithContext(ctx),
)
if err != nil {
return err
}
log.Printf("created chain %s in %s\n", createChainTxID, time.Since(createChainStartTime))
return nil
}