Skip to content

Commit

Permalink
feat: hermes support for non-local chains (#41)
Browse files Browse the repository at this point in the history
* feat: hermes support for non-local chains

* feat: hermes support for non local chains

* fix: sidebar links
  • Loading branch information
hanchon authored Aug 26, 2024
1 parent 701424f commit 2ddd5da
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 14 deletions.
10 changes: 6 additions & 4 deletions cmd/playground/hermesAddChannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ var hermesAddChannelCmd = &cobra.Command{
fmt.Println("Adding gaia chain")
if err := h.AddCosmosChain(
v.ChainID_2,
v.P26657,
v.P9090,
hermes.LocalEndpoint(v.P26657),
hermes.LocalEndpoint(v.P9090),
v.ValidatorKeyName,
v.ValidatorKey,
v.Prefix,
Expand All @@ -76,10 +76,12 @@ var hermesAddChannelCmd = &cobra.Command{
fmt.Println("Adding evmos chain")
if err := h.AddEvmosChain(
v.ChainID_2,
v.P26657,
v.P9090,
hermes.LocalEndpoint(v.P26657),
hermes.LocalEndpoint(v.P9090),
v.ValidatorKeyName,
v.ValidatorKey,
v.Prefix,
v.Denom,
); err != nil {
fmt.Println("error adding first chain to the relayer:", err.Error())
os.Exit(1)
Expand Down
2 changes: 2 additions & 0 deletions cmd/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/hanchon/hanchond/cmd/playground/query"
"github.com/hanchon/hanchond/cmd/playground/relayer"
"github.com/hanchon/hanchond/cmd/playground/tx"
"github.com/hanchon/hanchond/playground/filesmanager"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -34,4 +35,5 @@ var PlaygroundCmd = &cobra.Command{
func init() {
PlaygroundCmd.AddCommand(tx.TxCmd)
PlaygroundCmd.AddCommand(query.QueryCmd)
PlaygroundCmd.AddCommand(relayer.RelayerCmd)
}
114 changes: 114 additions & 0 deletions cmd/playground/relayer/addChainConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package relayer

import (
"fmt"
"os"

"github.com/hanchon/hanchond/playground/hermes"
"github.com/hanchon/hanchond/playground/sql"
"github.com/spf13/cobra"
)

// represents the addChainConfigCmd command
var addChainConfigCmd = &cobra.Command{
Use: "add-chain-config",
Args: cobra.ExactArgs(0),
Short: "Add chain config to hermes, it is ignored if the chain id already exists",
Run: func(cmd *cobra.Command, _ []string) {
_ = sql.InitDBFromCmd(cmd)

h := hermes.NewHermes()
fmt.Println("Relayer initialized")

chainID, err := cmd.Flags().GetString("chainid")
if err != nil || chainID == "" {
fmt.Println("missing chainid value")
os.Exit(1)
}

p26657, err := cmd.Flags().GetString("p26657")
if err != nil || chainID == "" {
fmt.Println("missing p26657 value")
os.Exit(1)
}

p9090, err := cmd.Flags().GetString("p9090")
if err != nil || chainID == "" {
fmt.Println("missing p9090 value")
os.Exit(1)
}

keyname, err := cmd.Flags().GetString("keyname")
if err != nil || chainID == "" {
fmt.Println("missing keyname value")
os.Exit(1)
}

keymnemonic, err := cmd.Flags().GetString("keymnemonic")
if err != nil || chainID == "" {
fmt.Println("missing keymnemonic value")
os.Exit(1)
}

prefix, err := cmd.Flags().GetString("prefix")
if err != nil || chainID == "" {
fmt.Println("missing prefix value")
os.Exit(1)
}

denom, err := cmd.Flags().GetString("denom")
if err != nil || chainID == "" {
fmt.Println("missing denom value")
os.Exit(1)
}

isEvm, err := cmd.Flags().GetBool("is-evm")
if err != nil || chainID == "" {
fmt.Println("missing is-evm value")
os.Exit(1)
}

switch isEvm {
case false:
fmt.Println("Adding a NOT EVM chain")
if err := h.AddCosmosChain(
chainID,
p26657,
p9090,
keyname,
keymnemonic,
prefix,
denom,
); err != nil {
fmt.Println("error adding first chain to the relayer:", err.Error())
os.Exit(1)
}
case true:
fmt.Println("Adding a EVM chain")
if err := h.AddEvmosChain(
chainID,
p26657,
p9090,
keyname,
keymnemonic,
prefix,
denom,
); err != nil {
fmt.Println("error adding first chain to the relayer:", err.Error())
os.Exit(1)
}
}
},
}

func init() {
RelayerCmd.AddCommand(addChainConfigCmd)
addChainConfigCmd.Flags().String("chainid", "", "Chain-id, i.e., evmos_9001-2")
addChainConfigCmd.Flags().String("p26657", "", "Endpoint where the port 26657 is exposed, i.e., http://localhost:26657")
addChainConfigCmd.Flags().String("p9090", "", "Endpoint where the port 9090 is exposed, i.e., http://localhost:9090")
addChainConfigCmd.Flags().String("keyname", "", "Key name, it's used to identify the files inside hermes, i.e., relayerkey")
addChainConfigCmd.Flags().String("keymnemonic", "", "Key mnemonic, mnemonic for the wallet")
addChainConfigCmd.Flags().String("prefix", "", "Prefix for the bech32 address, i.e, osmo")
addChainConfigCmd.Flags().String("denom", "", "Denom of the base token, i.e, aevmos")
addChainConfigCmd.Flags().Bool("is-evm", false, "If the chain is evm compatible, this is used to determinate the type of wallet.")
}
38 changes: 38 additions & 0 deletions cmd/playground/relayer/createChannel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package relayer

import (
"fmt"
"os"

"github.com/hanchon/hanchond/playground/hermes"
"github.com/hanchon/hanchond/playground/sql"
"github.com/spf13/cobra"
)

// represents the createChannelCmd command
var createChannelCmd = &cobra.Command{
Use: "create-channel [chain_id] [chain_id]",
Args: cobra.ExactArgs(2),
Short: "Create an IBC channel between two chains. The chains must be previously registered",
Run: func(cmd *cobra.Command, args []string) {
_ = sql.InitDBFromCmd(cmd)

h := hermes.NewHermes()
fmt.Println("Relayer initialized")

chain1 := args[0]
chain2 := args[1]

fmt.Println("Calling create channel")
err := h.CreateChannel(chain1, chain2)
if err != nil {
fmt.Println("error creating channel", err.Error())
os.Exit(1)
}
fmt.Println("Channel created")
},
}

func init() {
RelayerCmd.AddCommand(createChannelCmd)
}
20 changes: 20 additions & 0 deletions cmd/playground/relayer/relayer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package relayer

import (
"os"

"github.com/hanchon/hanchond/playground/filesmanager"
"github.com/spf13/cobra"
)

// RelayerCmd represents the relayer command
var RelayerCmd = &cobra.Command{
Use: "relayer",
Aliases: []string{"r"},
Short: "Relayer related functions",
Run: func(cmd *cobra.Command, _ []string) {
filesmanager.SetHomeFolderFromCobraFlags(cmd)
_ = cmd.Help()
os.Exit(0)
},
}
33 changes: 33 additions & 0 deletions docs/pages/hanchond/playground/relayer/addChainConfig.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Add Chain Configuration

To run a relayer between two chains, first you need to set up the configuration for both chains.

If you are running with just local chains, the `hermesAddChannel` command takes care of everything. But if you need to communicate with chains that are outside the networks created by `hanchond`, you need to manually add both chains and call the `create-channel` command.

## Parameters

To add a configuration you must send the following parameters as flags:

- `chainid`: Chain-id, i.e., `evmos_9001-2`
- `p26657`: Endpoint where the port 26657 is exposed, i.e., `http://localhost:26657`
- `p9090`: Endpoint where the port 9090 is exposed, i.e., `http://localhost:9090`
- `keyname`: Key name, it's used to identify the files inside Hermes, i.e., `relayerkey`
- `keymnemonic`: Key mnemonic, mnemonic for the wallet
- `prefix`: Prefix for the bech32 address, i.e, `osmo`
- `denom`: Denomination of the base token, i.e, `aevmos`
- `is-evm`: If the chain is EVM compatible, this is used to determinate the type of wallet.

```bash
hanchond playground relayer add-chain-config \
--chainid=evmos_9001-2 \
--p26657=http://localhost:26657 \
--p9090=http://localhost:9090 \
--keyname=evmosrelayer \
--keymnemonic="attend recipe own funny garden fiber glad inherit loud skull kangaroo swing license visual learn explain economy best news sibling float video disorder prepare" \
--prefix="evmos" \
--denom="aevmos" \
--is-evm=true

Relayer initialized
Adding a EVM chain
```
9 changes: 9 additions & 0 deletions docs/pages/hanchond/playground/relayer/createChannel.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Create a Channel

To send IBC transactions between two chains, an IBC channel must be created.

After adding the two chain configs using the `addChainConfig` command, the `createChannel` command can be used.

```bash
hanchond playground relayer create-channel evmos_9001-1 cosmoshub-1
```
22 changes: 13 additions & 9 deletions playground/hermes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ import (
"github.com/hanchon/hanchond/playground/filesmanager"
)

func LocalEndpoint(port int64) string {
return fmt.Sprintf("http://127.0.0.1:%d", port)
}

func (h *Hermes) GetConfigFile() string {
// If the dir already existed it will return error, but that is fine
_ = filesmanager.CreateHermesFolder()
return filesmanager.GetHermesPath() + "/config.toml"
}

func (h *Hermes) AddCosmosChain(chainID string, p26657 int64, p9090 int64, keyname string, mnemonic string, accountPrefix string, denom string) error {
func (h *Hermes) AddCosmosChain(chainID string, p26657 string, p9090 string, keyname string, mnemonic string, accountPrefix string, denom string) error {
values := fmt.Sprintf(`
[[chains]]
id = '%s'
rpc_addr = 'http://127.0.0.1:%d'
grpc_addr = 'http://127.0.0.1:%d'
rpc_addr = '%s'
grpc_addr = '%s'
event_source = { mode = 'pull', interval = '1s' }
rpc_timeout = '10s'
account_prefix = '%s'
Expand Down Expand Up @@ -60,15 +64,14 @@ trust_threshold = { numerator = '1', denominator = '3' }
return nil
}

func (h *Hermes) AddEvmosChain(chainID string, p26657 int64, p9090 int64, keyname string, mnemonic string) error {
func (h *Hermes) AddEvmosChain(chainID string, p26657 string, p9090 string, keyname string, mnemonic string, prefix string, denom string) error {
values := fmt.Sprintf(`
[[chains]]
id = '%s'
rpc_addr = 'http://127.0.0.1:%d'
grpc_addr = 'http://127.0.0.1:%d'
rpc_addr = '%s'
grpc_addr = '%s'
event_source = { mode = 'pull', interval = '1s' }
rpc_timeout = '10s'
account_prefix = 'evmos'
key_name = '%s'
key_store_folder = '%s'
store_prefix = 'ibc'
Expand All @@ -78,9 +81,10 @@ clock_drift = '15s'
max_block_time = '10s'
trusting_period = '14days'
trust_threshold = { numerator = '1', denominator = '3' }
gas_price = { price = 800000000, denom = 'aevmos' }
account_prefix = '%s'
gas_price = { price = 800000000, denom = '%s' }
address_type = { derivation = 'ethermint', proto_type = { pk_type = '/ethermint.crypto.v1.ethsecp256k1.PubKey' } }
`, chainID, p26657, p9090, keyname, filesmanager.GetHermesPath()+"/keyring"+chainID)
`, chainID, p26657, p9090, keyname, filesmanager.GetHermesPath()+"/keyring"+chainID, prefix, denom)

configFile, err := filesmanager.ReadFile(h.GetConfigFile())
if err != nil {
Expand Down
16 changes: 15 additions & 1 deletion vocs.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default defineConfig({
collapsed: true,
items: [
{
text: "Add a New Channel",
text: "Add a New Channel (only local networks)",
link: "/hanchond/playground/hermesAddChannel",
},
{
Expand All @@ -124,6 +124,20 @@ export default defineConfig({
},
],
},
{
text: "Relayer",
collapsed: true,
items: [
{
text: "Add a Chain Configuration",
link: "/hanchond/playground/relayer/addChainConfig",
},
{
text: "Create a Channel",
link: "/hanchond/playground/relayer/createChannel",
},
],
},
{
text: "Transactions",
collapsed: true,
Expand Down

0 comments on commit 2ddd5da

Please sign in to comment.