Skip to content

Commit

Permalink
Read URIs from avalanche-ops (#116)
Browse files Browse the repository at this point in the history
* read from avalanche-ops

* fix error

* linter

* change to ops
  • Loading branch information
patrick-ogrady authored Mar 24, 2023
1 parent f237e5a commit 488221a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
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

0 comments on commit 488221a

Please sign in to comment.