Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read URIs from avalanche-ops #116

Merged
merged 4 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions examples/tokenvm/cmd/token-cli/cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package cmd
import (
"context"
"fmt"
"os"
"reflect"
"strconv"
"strings"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/ava-labs/hypersdk/utils"
"github.com/ava-labs/hypersdk/vm"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"

"github.com/ava-labs/hypersdk/examples/tokenvm/actions"
"github.com/ava-labs/hypersdk/examples/tokenvm/auth"
Expand Down Expand Up @@ -126,6 +128,68 @@ var importANRChainCmd = &cobra.Command{
},
}

type AvalancheOpsConfig struct {
Resources struct {
CreatedNodes []struct {
HTTPEndpoint string `yaml:"httpEndpoint"`
} `yaml:"created_nodes"`
} `yaml:"resources"`
}

var importAvalancheOpsChainCmd = &cobra.Command{
Use: "import-ops [chainID] [path]",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return ErrInvalidArgs
}
_, err := ids.FromString(args[0])
return err
},
RunE: func(_ *cobra.Command, args []string) error {
// Delete previous items
if deleteOtherChains {
oldChains, err := DeleteChains()
if err != nil {
return err
}
if len(oldChains) > 0 {
utils.Outf("{{yellow}}deleted old chains:{{/}} %+v\n", oldChains)
}
}

// Load chainID
chainID, err := ids.FromString(args[0])
if err != nil {
return err
}

// Load yaml file
var opsConfig AvalancheOpsConfig
yamlFile, err := os.ReadFile(args[1])
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, &opsConfig)
if err != nil {
return err
}

// Add chains
for _, node := range opsConfig.Resources.CreatedNodes {
uri := fmt.Sprintf("%s/ext/bc/%s", node.HTTPEndpoint, chainID)
if err := StoreChain(chainID, uri); err != nil {
return err
}
utils.Outf(
"{{yellow}}stored chainID:{{/}} %s {{yellow}}uri:{{/}} %s\n",
chainID,
uri,
)
}
return StoreDefault(defaultChainKey, chainID[:])
},
}

var setChainCmd = &cobra.Command{
Use: "set",
RunE: func(*cobra.Command, []string) error {
Expand Down
18 changes: 13 additions & 5 deletions examples/tokenvm/cmd/token-cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ var (
dbPath string
db database.Database

genesisFile string
minUnitPrice int64
hideTxs bool
randomRecipient bool
maxTxBacklog int
genesisFile string
minUnitPrice int64
hideTxs bool
randomRecipient bool
maxTxBacklog int
deleteOtherChains bool

rootCmd = &cobra.Command{
Use: "token-cli",
Expand Down Expand Up @@ -95,9 +96,16 @@ func init() {
false,
"hide txs",
)
importAvalancheOpsChainCmd.PersistentFlags().BoolVar(
&deleteOtherChains,
"delete-other-chains",
true,
"delete other chains",
)
chainCmd.AddCommand(
importChainCmd,
importANRChainCmd,
importAvalancheOpsChainCmd,
setChainCmd,
chainInfoCmd,
watchChainCmd,
Expand Down