Skip to content

Commit

Permalink
feat: eth_call command to query view functions (#29)
Browse files Browse the repository at this point in the history
* feat: contracts eth_call to view functions

* fixes

* docs

* fix: build issues

* chore: remove unused code

* update erc20 abi
  • Loading branch information
hanchon authored Aug 18, 2024
1 parent d3e147e commit 8435050
Show file tree
Hide file tree
Showing 17 changed files with 548 additions and 63 deletions.
222 changes: 222 additions & 0 deletions abis/erc20.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
[
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_from",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
},
{
"name": "_spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"payable": true,
"stateMutability": "payable",
"type": "fallback"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": true,
"name": "spender",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
}
]
8 changes: 7 additions & 1 deletion cmd/playground/query/erc20/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hanchon/hanchond/lib/converter"
"github.com/hanchon/hanchond/lib/requester"
"github.com/hanchon/hanchond/lib/smartcontract/erc20"
"github.com/hanchon/hanchond/playground/cosmosdaemon"
"github.com/hanchon/hanchond/playground/sql"
"github.com/spf13/cobra"
)
Expand All @@ -30,7 +31,12 @@ var balanceCmd = &cobra.Command{
os.Exit(1)
}

endpoint := getEndpoint(queries, cmd)
endpoint, err := cosmosdaemon.GetWeb3Endpoint(queries, cmd)
if err != nil {
fmt.Printf("error generting web3 endpoint: %s\n", err.Error())
os.Exit(1)
}

client := requester.NewClient().WithUnsecureWeb3Endpoint(endpoint)
heightInt := erc20.Latest
if height != "latest" {
Expand Down
25 changes: 0 additions & 25 deletions cmd/playground/query/erc20/erc20.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package erc20

import (
"fmt"
"os"

"github.com/hanchon/hanchond/playground/database"
"github.com/hanchon/hanchond/playground/evmos"
"github.com/hanchon/hanchond/playground/filesmanager"
"github.com/spf13/cobra"
)
Expand All @@ -25,25 +22,3 @@ func init() {
ERC20Cmd.PersistentFlags().String("url", "", "Set the url path if using external provider")
ERC20Cmd.PersistentFlags().Bool("mainnet", false, "Set as true if the query for Evmos mainnet. This flag takes overwrite all the other provider related flags.")
}

func getEndpoint(queries *database.Queries, cmd *cobra.Command) string {
endpoint := ""
mainnet, _ := cmd.Flags().GetBool("mainnet")
if mainnet {
return "https://proxy.evmos.org/web3"
}

url, _ := cmd.Flags().GetString("url")
if url != "" {
endpoint = url
} else {
nodeID, err := cmd.Flags().GetString("node")
if err != nil {
fmt.Println("node not set")
os.Exit(1)
}
e := evmos.NewEvmosFromDB(queries, nodeID)
endpoint = fmt.Sprintf("http://localhost:%d", e.Ports.P8545)
}
return endpoint
}
7 changes: 6 additions & 1 deletion cmd/playground/query/erc20/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/hanchon/hanchond/lib/requester"
"github.com/hanchon/hanchond/lib/smartcontract/erc20"
"github.com/hanchon/hanchond/playground/cosmosdaemon"
"github.com/hanchon/hanchond/playground/sql"
"github.com/spf13/cobra"
)
Expand All @@ -21,7 +22,11 @@ var supplyCmd = &cobra.Command{
queries := sql.InitDBFromCmd(cmd)
contract := strings.TrimSpace(args[0])

endpoint := getEndpoint(queries, cmd)
endpoint, err := cosmosdaemon.GetWeb3Endpoint(queries, cmd)
if err != nil {
fmt.Printf("error generting web3 endpoint: %s\n", err.Error())
os.Exit(1)
}
client := requester.NewClient().WithUnsecureWeb3Endpoint(endpoint)

height, _ := cmd.Flags().GetString("height")
Expand Down
76 changes: 76 additions & 0 deletions cmd/playground/tx/solidity/callContract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package solidity

import (
"fmt"
"os"
"strings"

"github.com/hanchon/hanchond/lib/requester"
"github.com/hanchon/hanchond/lib/smartcontract"
"github.com/hanchon/hanchond/playground/cosmosdaemon"
"github.com/hanchon/hanchond/playground/filesmanager"
"github.com/hanchon/hanchond/playground/sql"
"github.com/spf13/cobra"
)

var params []string

// callContractViewCmd represents the callContractView command
var callContractViewCmd = &cobra.Command{
Use: "call-contract-view [contract] [abi_path] [method]",
Args: cobra.ExactArgs(3),
Short: "Call a contract view with eth_call",
Run: func(cmd *cobra.Command, args []string) {
queries := sql.InitDBFromCmd(cmd)

height, err := cmd.Flags().GetString("height")
if err != nil {
fmt.Println("could not read height value:", err.Error())
os.Exit(1)
}

contract := strings.TrimSpace(args[0])
abiPath := strings.TrimSpace(args[1])
method := strings.TrimSpace(args[2])

abiBytes, err := filesmanager.ReadFile(abiPath)
if err != nil {
fmt.Printf("error reading the abi file:%s\n", err.Error())
os.Exit(1)
}

endpoint, err := cosmosdaemon.GetWeb3Endpoint(queries, cmd)
if err != nil {
fmt.Printf("error generting web3 endpoint: %s\n", err.Error())
os.Exit(1)
}

callArgs, err := smartcontract.StringsToABIArguments(params)
if err != nil {
fmt.Printf("error converting arguments: %s\n", err.Error())
os.Exit(1)
}

client := requester.NewClient().WithUnsecureWeb3Endpoint(endpoint)

callData, err := smartcontract.ABIPack(abiBytes, method, callArgs...)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}

resp, err := client.EthCall(contract, callData, height)
if err != nil {
fmt.Println("error on eth call", err.Error())
os.Exit(1)
}
fmt.Println(string(resp))
os.Exit(0)
},
}

func init() {
SolidityCmd.AddCommand(callContractViewCmd)
callContractViewCmd.Flags().String("height", "latest", "Query at the given height.")
callContractViewCmd.Flags().StringSliceVarP(&params, "params", "p", []string{}, "A list of params. If the param is an address, prefix with `a:0x123...`")
}
Loading

0 comments on commit 8435050

Please sign in to comment.