Skip to content

Commit

Permalink
feat(eibc): prompts user for fee percentage for the rollapp upon init (
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Oct 3, 2024
1 parent a475d5b commit b6ec48a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 16 deletions.
18 changes: 2 additions & 16 deletions cmd/eibc/fulfill/rollapps/set/set.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package set

import (
"fmt"
"math/big"
"os"
"path/filepath"

"github.com/pterm/pterm"
"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"
)

Expand Down Expand Up @@ -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
}
},
Expand Down
58 changes: 58 additions & 0 deletions cmd/eibc/init/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package init

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"

Expand All @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions utils/eibc/eibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package eibc
import (
"context"
"fmt"
"math/big"
"os"
"os/exec"
"path/filepath"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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
}

0 comments on commit b6ec48a

Please sign in to comment.