Skip to content

Commit

Permalink
fix: smart-contract deployment command + eth_call cmd (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanchon authored Aug 19, 2024
1 parent 8435050 commit 0a85301
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 0 deletions.
46 changes: 46 additions & 0 deletions cmd/playground/query/evmos/ethCode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package evmos

import (
"fmt"
"os"

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

// ethCodeCmd represents the ethCode command
var ethCodeCmd = &cobra.Command{
Use: "eth-code [address]",
Args: cobra.ExactArgs(1),
Short: "Get the smartcontract registered eth code",
Run: func(cmd *cobra.Command, args []string) {
queries := sql.InitDBFromCmd(cmd)
nodeID, err := cmd.Flags().GetString("node")
if err != nil {
fmt.Println("node not set")
os.Exit(1)
}
height, err := cmd.Flags().GetString("height")
if err != nil {
fmt.Println("error getting the request height:", err.Error())
os.Exit(1)
}

e := evmos.NewEvmosFromDB(queries, nodeID)
client := e.NewRequester()

code, err := client.EthCode(args[0], height)
if err != nil {
fmt.Println("could not get the ethCode:", err.Error())
os.Exit(1)
}

fmt.Println(string(code))
},
}

func init() {
EvmosCmd.AddCommand(ethCodeCmd)
ethCodeCmd.Flags().String("height", "latest", "Query at the given height.")
}
7 changes: 7 additions & 0 deletions cmd/playground/tx/solidity/deployContract.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package solidity

import (
"encoding/hex"
"fmt"
"os"

Expand Down Expand Up @@ -42,6 +43,12 @@ var deployContractCmd = &cobra.Command{
os.Exit(1)
}

bytecode, err = hex.DecodeString(string(bytecode))
if err != nil {
fmt.Println("error converting bytecode to []byte:", err.Error())
os.Exit(1)
}

txHash, err := builder.DeployContract(0, bytecode, uint64(gasLimit))
if err != nil {
fmt.Printf("error sending the transaction: %s\n", err.Error())
Expand Down
12 changes: 12 additions & 0 deletions docs/pages/hanchond/playground/queries/evmos/ethCode.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Eth Code

The command `eth-code` returns the smart-contract bytecode.

```sh
hanchond playground query evmos eth-code 0xc0ac82342eacc3e5d38744660f7a57f465568746 | jq .
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x6080604052348015600f57600080fd5b506004361060285760003560e01c80631627905514602d575b600080fd5b605060048036036020811015604157600080fd5b50356001600160a01b03166064565b604080519115158252519081900360200190f35b6000813f7fc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470818114801590609757508115155b94935050505056fea264697066735822122030375d12bcd1c1173c9972f2bab6ba2db059fae1a35020cc0d1115194a8b445464736f6c634300060c0033"
}
```
10 changes: 10 additions & 0 deletions docs/pages/lib/requester/web3.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ txHash, err := c.BroadcastTX(tx)

## Eth_call

Interacts with a public function exposed by the smartcontract at the given height

```go
resp, err := client.EthCall(contract, callData, height)
```

## Eth_code

Gets the smartcontract bytecode at the given height

```go
resp, err := client.EthCode(contract, height)
```
13 changes: 13 additions & 0 deletions lib/requester/web3.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ func (c *Client) EthCall(address string, data string, height string) ([]byte, er
)
}

// Eth_code
func (c *Client) EthCode(address string, height string) ([]byte, error) {
heightString, err := heigthToQueryParam(height)
if err != nil {
return nil, err
}
return c.SendPostRequest(
c.Web3Endpoint,
[]byte(`{"method":"eth_getCode","params":["`+address+`","`+heightString+`"],"id":1,"jsonrpc":"2.0"}`),
c.Web3Auth,
)
}

func heigthToQueryParam(height string) (string, error) {
heightString := "latest"
if height != "latest" {
Expand Down
4 changes: 4 additions & 0 deletions vocs.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ export default defineConfig({
text: "IBC Rate Limit",
link: "/hanchond/playground/queries/evmos/rateLimit",
},
{
text: "Smart-Contract Bytecode",
link: "/hanchond/playground/queries/evmos/ethCode",
},
],
},
],
Expand Down

0 comments on commit 0a85301

Please sign in to comment.