Skip to content

Commit

Permalink
feat: add eibc client support (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Jul 11, 2024
1 parent 1d23104 commit 89dc1c6
Show file tree
Hide file tree
Showing 14 changed files with 1,646 additions and 45 deletions.
22 changes: 20 additions & 2 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func generateSequencersKeys(initConfig config.RollappConfig) ([]utils.AddressDat
for _, key := range keys {
var address string
var err error
address, err = createAddressBinary(key, initConfig.Home)
address, err = CreateAddressBinary(key, initConfig.Home)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -76,7 +76,7 @@ func getSequencerKeysConfig(rollappConfig config.RollappConfig) []utils.KeyConfi
// }
// }

func createAddressBinary(keyConfig utils.KeyConfig, home string) (string, error) {
func CreateAddressBinary(keyConfig utils.KeyConfig, home string) (string, error) {
args := []string{
"keys", "add", keyConfig.ID, "--keyring-backend", "test",
"--keyring-dir", filepath.Join(home, keyConfig.Dir),
Expand All @@ -90,6 +90,24 @@ func createAddressBinary(keyConfig utils.KeyConfig, home string) (string, error)
return utils.ParseAddressFromOutput(out)
}

func CreateAddressBinaryWithSensitiveOutput(
keyConfig utils.KeyConfig,
home string,
) (*utils.SensitiveKeyInfo, error) {
args := []string{
"keys", "add", keyConfig.ID, "--keyring-backend", "test",
"--keyring-dir", filepath.Join(home, keyConfig.Dir),
"--output", "json",
}
createKeyCommand := exec.Command(keyConfig.ChainBinary, args...)
fmt.Println(createKeyCommand.String())
out, err := utils.ExecBashCommandWithStdout(createKeyCommand)
if err != nil {
return nil, err
}
return utils.ParseAddressFromOutputWithSensisiveOutput(out)
}

// func generateRelayerKeys(rollappConfig config.RollappConfig) ([]utils.AddressData, error) {
// relayerAddresses := make([]utils.AddressData, 0)
// keys := getRelayerKeysConfig(rollappConfig)
Expand Down
4 changes: 4 additions & 0 deletions cmd/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var Executables = struct {
CelKey string
Roller string
Simd string
Eibc string
}{
Roller: fmt.Sprintf("%s/roller", binsDir),
RollappEVM: fmt.Sprintf("%s/rollapp_evm", binsDir),
Expand All @@ -29,6 +30,7 @@ var Executables = struct {
CelKey: fmt.Sprintf("%s/cel-key", internalBinsDir),
Relayer: fmt.Sprintf("%s/rly", internalBinsDir),
Simd: fmt.Sprintf("%s/simd", internalBinsDir),
Eibc: fmt.Sprintf("%s/eibc", binsDir),
}

var KeysIds = struct {
Expand All @@ -55,12 +57,14 @@ var ConfigDirName = struct {
DALightNode string
HubKeys string
LocalHub string
Eibc string
}{
Rollapp: "rollapp",
Relayer: "relayer",
DALightNode: "da-light-node",
HubKeys: "hub-keys",
LocalHub: "local-hub",
Eibc: ".order-client",
}

var Denoms = struct {
Expand Down
18 changes: 18 additions & 0 deletions cmd/eibc/eibc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package eibc

import (
"github.com/spf13/cobra"
)

func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "eibc",
Short: "Commands for running and managing eibc client",
}

cmd.AddCommand(initCmd())
cmd.AddCommand(startCmd())
cmd.AddCommand(scaleCmd())

return cmd
}
57 changes: 57 additions & 0 deletions cmd/eibc/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package eibc

import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"

"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
global_utils "github.com/dymensionxyz/roller/utils"
)

func initCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize eibc client",
Run: func(cmd *cobra.Command, args []string) {
home, _ := os.UserHomeDir()
eibcHome := filepath.Join(home, ".order-client")
ok, err := global_utils.DirNotEmpty(eibcHome)
if err != nil {
return
}

if ok {
fmt.Println("eibc client already initialized")
return
}

c := GetInitCommand()

err = utils.ExecBashCmd(c)
if err != nil {
return
}

err = ensureWhaleAccount()
if err != nil {
log.Printf("failed to create whale account: %v\n", err)
return
}
},
}
return cmd
}

func GetInitCommand() *exec.Cmd {
cmd := exec.Command(
consts.Executables.Eibc,
"init",
)
return cmd
}
38 changes: 38 additions & 0 deletions cmd/eibc/scale.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package eibc

import (
"os/exec"

"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
)

func scaleCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "scale [count]",
Short: "Start the eibc client",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
count := args[0]

c := GetScaleCmd(count)

err := utils.ExecBashCmdFollow(c)
if err != nil {
return
}
},
}
return cmd
}

func GetScaleCmd(count string) *exec.Cmd {
cmd := exec.Command(
consts.Executables.Eibc,
"scale",
count,
)
return cmd
}
66 changes: 66 additions & 0 deletions cmd/eibc/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package eibc

import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"

"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
global_utils "github.com/dymensionxyz/roller/utils"
)

func startCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Start the eibc client",
Run: func(cmd *cobra.Command, args []string) {
home, _ := os.UserHomeDir()
eibcHome := filepath.Join(home, ".order-client")
ok, err := global_utils.DirNotEmpty(eibcHome)
if err != nil {
return
}

if !ok {
fmt.Println("eibc home directory not present, running init")
c := GetInitCommand()

_, err := utils.ExecBashCommandWithStdout(c)
if err != nil {
return
}

err = ensureWhaleAccount()
if err != nil {
log.Printf("failed to create whale account: %v\n", err)
return
}
}

err = createMongoDbContainer()
if err != nil {
return
}

c := GetStartCmd()
err = utils.ExecBashCmdFollow(c)
if err != nil {
return
}
},
}
return cmd
}

func GetStartCmd() *exec.Cmd {
cmd := exec.Command(
consts.Executables.Eibc,
"start",
)
return cmd
}
68 changes: 68 additions & 0 deletions cmd/eibc/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package eibc

import (
"context"
"fmt"
"os"

"github.com/docker/docker/client"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
)

// ensureWhaleAccount function makes sure that eibc whale account is present in
// the keyring. In eibc client, whale account is the wallet that acts as the bank
// and distributes funds across a set of wallets that fulfill the eibc orders
func ensureWhaleAccount() error {
home, _ := os.UserHomeDir()
kc := utils.KeyConfig{
Dir: consts.ConfigDirName.Eibc,
ID: "client",
ChainBinary: consts.Executables.Dymension,
Type: "",
}

_, err := utils.GetAddressBinary(kc, consts.Executables.Dymension)
if err != nil {
fmt.Println("whale account not found in the keyring, creating it now")
addressInfo, err := initconfig.CreateAddressBinaryWithSensitiveOutput(kc, home)
if err != nil {
return err
}

whaleAddress := utils.SecretAddressData{
AddressData: utils.AddressData{
Name: addressInfo.Name,
Addr: addressInfo.Address,
},
Mnemonic: addressInfo.Mnemonic,
}

utils.PrintSecretAddressesWithTitle([]utils.SecretAddressData{whaleAddress})
}

return nil
}

// createMongoDbContainer function creates a mongodb container using docker
// sdk. Any 'DOCKER_HOST' can be used for this mongodb container.
// Mongodb is used to store information about processed eibc orders
func createMongoDbContainer() error {
cc, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
fmt.Printf("failed to create docker client: %v\n", err)
return err
}

err = utils.CheckAndCreateMongoDBContainer(
context.Background(),
cc,
)
if err != nil {
fmt.Printf("failed to run mongodb container: %v\n", err)
return err
}
return err
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/dymensionxyz/roller/cmd/config"
da_light_client "github.com/dymensionxyz/roller/cmd/da-light-client"
"github.com/dymensionxyz/roller/cmd/eibc"
"github.com/dymensionxyz/roller/cmd/keys"
"github.com/dymensionxyz/roller/cmd/migrate"
"github.com/dymensionxyz/roller/cmd/relayer"
Expand Down Expand Up @@ -47,6 +48,7 @@ func init() {
rootCmd.AddCommand(tx.Cmd())
rootCmd.AddCommand(test())
rootCmd.AddCommand(rollapp.Cmd())
rootCmd.AddCommand(eibc.Cmd())
utils.AddGlobalFlags(rootCmd)
}

Expand Down
Loading

0 comments on commit 89dc1c6

Please sign in to comment.