This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
create_blockchain.go
123 lines (111 loc) · 3.12 KB
/
create_blockchain.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package cmd
import (
"context"
"fmt"
"os"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/subnet-cli/pkg/color"
"github.com/manifoldco/promptui"
"github.com/onsi/ginkgo/v2/formatter"
"github.com/spf13/cobra"
)
func newCreateBlockchainCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "blockchain [options]",
Short: "Creates a blockchain",
Long: `
Creates a blockchain.
$ subnet-cli create blockchain \
--private-key-path=.insecure.ewoq.key \
--subnet-id="24tZhrm8j8GCJRE9PomW8FaeqbgGS4UAQjJnqqn8pq5NwYSYV1" \
--chain-name=my-custom-chain \
--vm-id=tGas3T58KzdjLHhBDMnH2TvrddhqTji5iZAMZ3RXs2NLpSnhH \
--vm-genesis-path=.my-custom-vm.genesis
`,
RunE: createBlockchainFunc,
}
cmd.PersistentFlags().StringVar(&subnetIDs, "subnet-id", "", "subnet ID (must be formatted in ids.ID)")
cmd.PersistentFlags().StringVar(&chainName, "chain-name", "", "chain name")
cmd.PersistentFlags().StringVar(&vmIDs, "vm-id", "", "VM ID (must be formatted in ids.ID)")
cmd.PersistentFlags().StringVar(&vmGenesisPath, "vm-genesis-path", "", "VM genesis file path")
return cmd
}
func createBlockchainFunc(cmd *cobra.Command, args []string) error {
cli, info, err := InitClient(publicURI, true)
if err != nil {
return err
}
info.subnetIDType = "SUBNET ID"
info.subnetID, err = ids.FromString(subnetIDs)
if err != nil {
return err
}
info.vmID, err = ids.FromString(vmIDs)
if err != nil {
return err
}
vmGenesisBytes, err := os.ReadFile(vmGenesisPath)
if err != nil {
return err
}
info.txFee = uint64(info.feeData.CreateBlockchainTxFee)
info.requiredBalance = info.txFee
if err := info.CheckBalance(); err != nil {
return err
}
info.chainName = chainName
info.vmGenesisPath = vmGenesisPath
msg := MakeCreateTable(info)
if enablePrompt {
msg = formatter.F("\n{{blue}}{{bold}}Ready to create blockchain resources, should we continue?{{/}}\n") + msg
}
fmt.Fprint(formatter.ColorableStdOut, msg)
if enablePrompt {
prompt := promptui.Select{
Label: "\n",
Stdout: os.Stdout,
Items: []string{
formatter.F("{{green}}Yes, let's create! {{bold}}{{underline}}I agree to pay the fee{{/}}{{green}}!{{/}}"),
formatter.F("{{red}}No, stop it!{{/}}"),
},
}
idx, _, err := prompt.Run()
if err != nil {
return nil //nolint:nilerr
}
if idx == 1 {
return nil
}
}
println()
println()
println()
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
blockchainID, took, err := cli.P().CreateBlockchain(
ctx,
info.key,
info.subnetID,
info.chainName,
info.vmID,
vmGenesisBytes,
)
cancel()
if err != nil {
return err
}
info.blockchainID = blockchainID
color.Outf("{{magenta}}created blockchain{{/}} %q {{light-gray}}(took %v){{/}}\n\n", info.blockchainID, took)
info.requiredBalance = 0
info.stakeAmount = 0
info.txFee = 0
ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
info.balance, err = cli.P().Balance(ctx, info.key)
cancel()
if err != nil {
return err
}
fmt.Fprint(formatter.ColorableStdOut, MakeCreateTable(info))
return nil
}