From b6ec48a14539d0b5ea7c863f85f9f5b685494bc2 Mon Sep 17 00:00:00 2001 From: artpav <19916123+artemijspavlovs@users.noreply.github.com> Date: Thu, 3 Oct 2024 13:17:28 +0300 Subject: [PATCH] feat(eibc): prompts user for fee percentage for the rollapp upon init (#1059) --- cmd/eibc/fulfill/rollapps/set/set.go | 18 +-------- cmd/eibc/init/init.go | 58 ++++++++++++++++++++++++++++ utils/eibc/eibc.go | 21 ++++++++++ 3 files changed, 81 insertions(+), 16 deletions(-) diff --git a/cmd/eibc/fulfill/rollapps/set/set.go b/cmd/eibc/fulfill/rollapps/set/set.go index 59cfdb94..984dcfbb 100644 --- a/cmd/eibc/fulfill/rollapps/set/set.go +++ b/cmd/eibc/fulfill/rollapps/set/set.go @@ -1,8 +1,6 @@ package set import ( - "fmt" - "math/big" "os" "path/filepath" @@ -10,7 +8,7 @@ import ( "github.com/spf13/cobra" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/utils/config/yamlconfig" + "github.com/dymensionxyz/roller/utils/eibc" "github.com/dymensionxyz/roller/utils/filesystem" ) @@ -46,23 +44,11 @@ instance. return } - eibcConfigPath := filepath.Join(eibcHome, "config.yaml") rollAppID := args[0] value := args[1] - vf, _, err := big.ParseFloat(value, 10, 64, big.ToNearestEven) - valueFloat, _ := vf.Float32() + err = eibc.AddRollappToEibc(value, rollAppID, eibcHome) if err != nil { - pterm.Error.Println("failed to convert value to float", err) - return - } - - updates := map[string]interface{}{ - fmt.Sprintf("fulfill_criteria.min_fee_percentage.chain.%s", rollAppID): valueFloat, - } - err = yamlconfig.UpdateNestedYAML(eibcConfigPath, updates) - if err != nil { - pterm.Error.Println("failed to update config", err) return } }, diff --git a/cmd/eibc/init/init.go b/cmd/eibc/init/init.go index 0e7a9e15..11ec6010 100644 --- a/cmd/eibc/init/init.go +++ b/cmd/eibc/init/init.go @@ -1,6 +1,9 @@ package init import ( + "errors" + "fmt" + "io/fs" "os" "path/filepath" @@ -11,6 +14,7 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" "github.com/dymensionxyz/roller/utils/bash" + configutils "github.com/dymensionxyz/roller/utils/config" "github.com/dymensionxyz/roller/utils/config/tomlconfig" "github.com/dymensionxyz/roller/utils/config/yamlconfig" eibcutils "github.com/dymensionxyz/roller/utils/eibc" @@ -107,6 +111,53 @@ func Cmd() *cobra.Command { return } + var runForExisting bool + var raID string + rollerConfigFilePath := filepath.Join(home, consts.RollerConfigFileName) + var rollerData configutils.RollappConfig + + _, err = os.Stat(rollerConfigFilePath) + if err != nil { + if errors.Is(err, fs.ErrNotExist) { + pterm.Info.Println("existing roller configuration not found") + runForExisting = false + } else { + pterm.Error.Println("failed to check existing roller config") + return + } + } else { + pterm.Info.Println("existing roller configuration found, retrieving RollApp ID from it") + rollerData, err = tomlconfig.LoadRollerConfig(home) + if err != nil { + pterm.Error.Printf("failed to load rollapp config: %v\n", err) + return + } + rollerRaID := rollerData.RollappID + rollerHubData := rollerData.HubData + msg := fmt.Sprintf( + "the retrieved RollApp ID is: %s, would you like to initialize the eibc client for this RollApp?", + rollerRaID, + ) + rlyFromRoller, _ := pterm.DefaultInteractiveConfirm.WithDefaultText(msg).Show() + if rlyFromRoller { + raID = rollerRaID + hd = rollerHubData + runForExisting = true + } + + if !rlyFromRoller { + runForExisting = false + } + } + + if !runForExisting { + raID, _ = pterm.DefaultInteractiveTextInput.WithDefaultText("Please enter the RollApp ID"). + Show() + } + + raFeePercentage, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("Please provide the fee percentage for the RollApp"). + Show() + eibcConfigPath := filepath.Join(eibcHome, "config.yaml") updates := map[string]interface{}{ "node_address": hd.RPC_URL, @@ -115,6 +166,13 @@ func Cmd() *cobra.Command { "order_polling.indexer_url": "http://44.206.211.230:3000/", "order_polling.enabled": true, } + + err = eibcutils.AddRollappToEibc(raFeePercentage, raID, eibcHome) + if err != nil { + pterm.Error.Println("failed to add the rollapp to eibc config: ", err) + return + } + err = yamlconfig.UpdateNestedYAML(eibcConfigPath, updates) if err != nil { pterm.Error.Println("failed to update config", err) diff --git a/utils/eibc/eibc.go b/utils/eibc/eibc.go index fd41ab53..ba69d9a2 100644 --- a/utils/eibc/eibc.go +++ b/utils/eibc/eibc.go @@ -3,6 +3,7 @@ package eibc import ( "context" "fmt" + "math/big" "os" "os/exec" "path/filepath" @@ -12,6 +13,7 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" + "github.com/dymensionxyz/roller/utils/config/yamlconfig" dockerutils "github.com/dymensionxyz/roller/utils/docker" "github.com/dymensionxyz/roller/utils/keys" ) @@ -124,3 +126,22 @@ func CreateMongoDbContainer() error { } return err } + +func AddRollappToEibc(value, rollAppID, eibcHome string) error { + eibcConfigPath := filepath.Join(eibcHome, "config.yaml") + + vf, _, err := big.ParseFloat(value, 10, 64, big.ToNearestEven) + valueFloat, _ := vf.Float32() + if err != nil { + return fmt.Errorf("failed to convert value to float: %v", err) + } + + updates := map[string]interface{}{ + fmt.Sprintf("fulfill_criteria.min_fee_percentage.chain.%s", rollAppID): valueFloat, + } + err = yamlconfig.UpdateNestedYAML(eibcConfigPath, updates) + if err != nil { + return fmt.Errorf("failed to update config: %v", err) + } + return nil +}