Skip to content

Commit

Permalink
feat: local-ic native cli interaction (#1007)
Browse files Browse the repository at this point in the history
* local-ic interact

* use flag for API addr

* faster blocks

* interact examples
  • Loading branch information
Reecepbcups authored Mar 7, 2024
1 parent be3ee74 commit 3b7f1ce
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 27 deletions.
4 changes: 2 additions & 2 deletions local-interchain/chains/base_ibc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"docker_image": {
"version": "v9.1.0"
},
"block_time": "6s",
"block_time": "2s",
"gas_prices": "0%DENOM%",
"gas_adjustment": 2.0,
"ibc_paths": ["atom-juno", "atom-terra"],
Expand Down Expand Up @@ -49,7 +49,7 @@
"docker_image": {
"version": "v2.3.4"
},
"block_time": "6s",
"block_time": "2s",
"encoding-options": ["wasm"],
"gas_prices": "0%DENOM%",
"gas_adjustment": 2.0,
Expand Down
18 changes: 9 additions & 9 deletions local-interchain/chains/cosmoshub.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
{
"chains": [
{
"name": "gaia",
"name": "gaia",
"chain_id": "localcosmos-1",
"denom": "uatom",
"binary": "gaiad",
"bech32_prefix": "cosmos",
"docker_image": {
"docker_image": {
"version": "v10.0.1"
},
},
"gas_prices": "0%DENOM%",
"chain_type": "cosmos",
"coin_type": 118,
"trusting_period": "112h",
"gas_adjustment": 2.0,
"number_vals": 1,
"number_node": 0,
"number_node": 0,
"debugging": true,
"block_time": "500ms",
"block_time": "500ms",
"genesis": {
"modify": [
{
Expand All @@ -32,15 +32,15 @@
"key": "app_state.gov.deposit_params.min_deposit.0.denom",
"value": "uatom"
}
],
],
"accounts": [
{
"name": "acc0",
"address": "cosmos1hj5fveer5cjtn4wd6wstzugjfdxzl0xpxvjjvr",
"address": "cosmos1hj5fveer5cjtn4wd6wstzugjfdxzl0xpxvjjvr",
"amount": "10000000000%DENOM%",
"mnemonic": "decorate bright ozone fork gallery riot bus exhaust worth way bone indoor calm squirrel merry zero scheme cotton until shop any excess stage laundry"
}
]
]
}
}
]
Expand Down
16 changes: 8 additions & 8 deletions local-interchain/chains/juno_ibc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
{
"chains": [
{
"name": "juno",
"name": "juno",
"chain_id": "localjuno-1",
"denom": "ujuno",
"binary": "junod",
Expand All @@ -19,7 +19,7 @@
"number_node": 0,
"block_time": "500ms",
"ibc_paths": ["juno-ibc-1"],
"debugging": true,
"debugging": true,
"encoding-options": ["juno"],
"genesis": {
"modify": [
Expand All @@ -35,7 +35,7 @@
"key": "app_state.gov.params.min_deposit.0.denom",
"value": "ujuno"
}
],
],
"accounts": [
{
"name": "acc0",
Expand All @@ -49,7 +49,7 @@
"amount": "10000000%DENOM%",
"mnemonic": "wealth flavor believe regret funny network recall kiss grape useless pepper cram hint member few certain unveil rather brick bargain curious require crowd raise"
}
]
]
}
},
{
Expand All @@ -62,7 +62,7 @@
"repository": "ghcr.io/cosmoscontracts/juno",
"version": "v17.0.0"
},
"genesis": {
"genesis": {
"accounts": [
{
"name": "second0",
Expand All @@ -76,13 +76,13 @@
"amount": "10000000%DENOM%",
"mnemonic": "wealth flavor believe regret funny network recall kiss grape useless pepper cram hint member few certain unveil rather brick bargain curious require crowd raise"
}
]
]
},
"block_time": "500ms",
"encoding-options": ["juno"],
"gas_prices": "0%DENOM%",
"gas_adjustment": 2.0,
"ibc_paths": ["juno-ibc-1"]
"ibc_paths": ["juno-ibc-1"]
}
]
}
16 changes: 8 additions & 8 deletions local-interchain/chains/stargaze.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
{
"chains": [
{
"name": "stargaze",
"name": "stargaze",
"chain_id": "localstars-1",
"denom": "ustars",
"binary": "starsd",
"bech32_prefix": "stars",
"docker_image": {
"docker_image": {
"version": "v10.0.1"
},
},
"gas_prices": "0%DENOM%",
"chain_type": "cosmos",
"coin_type": 118,
"trusting_period": "112h",
"gas_adjustment": 2.0,
"number_vals": 1,
"number_node": 0,
"number_node": 0,
"debugging": true,
"block_time": "500ms",
"encoding-options": ["wasm"],
Expand All @@ -33,15 +33,15 @@
"key": "app_state.gov.deposit_params.min_deposit.0.denom",
"value": "ustars"
}
],
],
"accounts": [
{
"name": "acc0",
"address": "stars1hj5fveer5cjtn4wd6wstzugjfdxzl0xpjs908j",
"address": "stars1hj5fveer5cjtn4wd6wstzugjfdxzl0xpjs908j",
"amount": "10000000000%DENOM%",
"mnemonic": "decorate bright ozone fork gallery riot bus exhaust worth way bone indoor calm squirrel merry zero scheme cotton until shop any excess stage laundry"
}
]
]
}
}
]
Expand Down
83 changes: 83 additions & 0 deletions local-interchain/cmd/local-ic/interaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"

"github.com/spf13/cobra"
"github.com/strangelove-ventures/localinterchain/interchain/handlers"
)

const (
FlagAPIEndpoint = "api-endpoint"
)

func init() {
interactCmd.Flags().String(FlagAPIAddressOverride, "http://127.0.0.1:8080", "override the default API address")
}

var interactCmd = &cobra.Command{
Use: "interact [chain_id] [interaction] [arguments...]",
Short: "Interact with a node",
Example: `local-ic interact localcosmos-1 bin 'status --node=%RPC%' --api-endpoint=http://127.0.0.1:8080
local-ic interact localcosmos-1 query bank balances cosmos1hj5fveer5cjtn4wd6wstzugjfdxzl0xpxvjjvr
local-ic interact localcosmos-1 get_channels
local-ic interact localcosmos-1 relayer-exec rly q channels localcosmos-1
`,
Args: cobra.MinimumNArgs(2),
Aliases: []string{"i"},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetFiles(), cobra.ShellCompDirectiveNoFileComp
},
Run: func(cmd *cobra.Command, args []string) {

ah := handlers.ActionHandler{
ChainId: args[0],
Action: args[1],
}

if len(args) > 2 {
ah.Cmd = strings.Join(args[2:], " ")
}

apiAddr, _ := cmd.Flags().GetString(FlagAPIAddressOverride)

res := makeHttpReq(apiAddr, ah)
fmt.Println(res)
},
}

func makeHttpReq(apiEndpoint string, ah handlers.ActionHandler) string {
client := &http.Client{}

// curl -X POST -H "Content-Type: application/json" -d '{
// "chain_id": "localjuno-1",
// "action": "query",
// "cmd": "bank balances juno1hj5fveer5cjtn4wd6wstzugjfdxzl0xps73ftl"
// }' http://127.0.0.1:8080/
jsonData, err := json.Marshal(ah)
if err != nil {
panic(err)
}

req, err := http.NewRequest("POST", apiEndpoint, bytes.NewReader(jsonData))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}

return string(bodyText)
}
1 change: 1 addition & 0 deletions local-interchain/cmd/local-ic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func main() {
rootCmd.AddCommand(chainsCmd)
rootCmd.AddCommand(newChainCmd)
rootCmd.AddCommand(startCmd)
rootCmd.AddCommand(interactCmd)

if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "error while executing your CLI. Err: %v\n", err)
Expand Down

0 comments on commit 3b7f1ce

Please sign in to comment.