Skip to content

Commit

Permalink
feat: add scale command for handling bot count (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Jul 9, 2024
1 parent 3d77862 commit 6302c03
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
47 changes: 46 additions & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"fmt"
"log"
"os"
"strconv"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/dymensionxyz/eibc-client/store"
utils "github.com/dymensionxyz/eibc-client/utils/viper"
)

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -208,7 +211,11 @@ var balancesCmd = &cobra.Command{

if !oc.whale.accountSvc.balances.IsZero() {
accPref := fmt.Sprintf("Whale | '%s': ", oc.whale.accountSvc.accountName)
printAccountSlot(oc.whale.accountSvc.account.GetAddress().String(), accPref, dividerItem)
printAccountSlot(
oc.whale.accountSvc.account.GetAddress().String(),
accPref,
dividerItem,
)
printBalances(oc.whale.accountSvc.balances, longestAmountStr, maxDen)
fmt.Println()
}
Expand All @@ -225,6 +232,43 @@ var balancesCmd = &cobra.Command{
},
}

var scaleCmd = &cobra.Command{
Use: "scale [count]",
Short: "scale bot count",
Long: `scale the number of bot accounts that fulfill the eibc orders`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
newBotCount, err := strconv.Atoi(args[0])
if err != nil {
return
}

home, err := homedir.Dir()
if err != nil {
log.Fatalf("failed to get home directory: %v", err)
}

defaultHomeDir := home + "/.eibc-client"
cfgFile = defaultHomeDir + "/config.yaml"

viper.SetConfigFile(cfgFile)
err = viper.ReadInConfig()
if err != nil {
return
}

err = utils.UpdateViperConfig("bots.number_of_bots", newBotCount, viper.ConfigFileUsed())
if err != nil {
return
}

fmt.Printf(
"bot count successfully scaled to %d, please restart the eibc process if it's running\n",
newBotCount,
)
},
}

func printAccountSlot(address string, accPref, dividerItem string) {
dividerAcc := ""

Expand Down Expand Up @@ -276,6 +320,7 @@ func init() {
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(startCmd)
rootCmd.AddCommand(scaleCmd)

balancesCmd.Flags().BoolP("all", "a", false, "Filter by fulfillment status")
rootCmd.AddCommand(balancesCmd)
Expand Down
25 changes: 25 additions & 0 deletions utils/viper/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package utils

import (
"fmt"
"os"

"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)

func UpdateViperConfig(key string, value any, configFile string) error {
viper.Set(key, value)

updatedConfig, err := yaml.Marshal(viper.AllSettings())
if err != nil {
fmt.Printf("unable to marshal config to yaml: %v\n", err)
}

err = os.WriteFile(configFile, updatedConfig, 0o644)
if err != nil {
fmt.Printf("failed to update config file: %v\n", err)
}

return nil
}

0 comments on commit 6302c03

Please sign in to comment.