From 300f20ed06709844fe92c60a5cdd6aee30f2b054 Mon Sep 17 00:00:00 2001
From: Guillermo Paoletti <guillermo.paoletti@gmail.com>
Date: Thu, 15 Aug 2024 16:00:15 +0200
Subject: [PATCH] docs: v0.2.0 debug `eth_call` example (#25)

* docs: v0.2.0 debug eth call example

* chore: update requirements
---
 cmd/playground/query/erc20/balance.go         |  15 +-
 cmd/playground/query/erc20/supply.go          |  16 +-
 .../playground/examples/debugEthCall.mdx      | 150 ++++++++++++++++++
 .../playground/queries/erc20/balance.mdx      |   2 +
 .../playground/queries/erc20/supply.mdx       |   4 +
 playground/cosmosdaemon/config_files.go       |   4 +-
 playground/evmos/command_gov.go               |   6 +-
 vocs.config.ts                                |   4 +
 8 files changed, 195 insertions(+), 6 deletions(-)
 create mode 100644 docs/pages/hanchond/playground/examples/debugEthCall.mdx

diff --git a/cmd/playground/query/erc20/balance.go b/cmd/playground/query/erc20/balance.go
index 971467b..0b4b908 100644
--- a/cmd/playground/query/erc20/balance.go
+++ b/cmd/playground/query/erc20/balance.go
@@ -3,6 +3,7 @@ package erc20
 import (
 	"fmt"
 	"os"
+	"strconv"
 	"strings"
 
 	"github.com/hanchon/hanchond/lib/converter"
@@ -25,6 +26,8 @@ var balanceCmd = &cobra.Command{
 			fmt.Println("node not set")
 			os.Exit(1)
 		}
+
+		height, _ := cmd.Flags().GetString("height")
 		contract := strings.TrimSpace(args[0])
 		wallet := strings.TrimSpace(args[1])
 		wallet, err = converter.NormalizeAddressToHex(wallet)
@@ -35,7 +38,16 @@ var balanceCmd = &cobra.Command{
 
 		e := evmos.NewEvmosFromDB(queries, nodeID)
 		client := requester.NewClient().WithUnsecureWeb3Endpoint(fmt.Sprintf("http://localhost:%d", e.Ports.P8545))
-		balance, err := client.GetBalanceERC20(contract, wallet, erc20.Latest)
+		heightInt := erc20.Latest
+		if height != "latest" {
+			temp, err := strconv.ParseInt(height, 10, 64)
+			if err != nil {
+				fmt.Printf("invalid height: %s\n", err.Error())
+				os.Exit(1)
+			}
+			heightInt = int(temp)
+		}
+		balance, err := client.GetBalanceERC20(contract, wallet, heightInt)
 		if err != nil {
 			fmt.Println("could not get the balance:", err.Error())
 			os.Exit(1)
@@ -46,4 +58,5 @@ var balanceCmd = &cobra.Command{
 
 func init() {
 	ERC20Cmd.AddCommand(balanceCmd)
+	balanceCmd.Flags().String("height", "latest", "Query at the given height.")
 }
diff --git a/cmd/playground/query/erc20/supply.go b/cmd/playground/query/erc20/supply.go
index 7be0f2a..6885b7f 100644
--- a/cmd/playground/query/erc20/supply.go
+++ b/cmd/playground/query/erc20/supply.go
@@ -3,6 +3,7 @@ package erc20
 import (
 	"fmt"
 	"os"
+	"strconv"
 	"strings"
 
 	"github.com/hanchon/hanchond/lib/requester"
@@ -27,7 +28,19 @@ var supplyCmd = &cobra.Command{
 		contract := strings.TrimSpace(args[0])
 		e := evmos.NewEvmosFromDB(queries, nodeID)
 		client := requester.NewClient().WithUnsecureWeb3Endpoint(fmt.Sprintf("http://localhost:%d", e.Ports.P8545))
-		supply, err := client.GetTotalSupply(contract, erc20.Latest)
+
+		height, _ := cmd.Flags().GetString("height")
+		heightInt := erc20.Latest
+		if height != "latest" {
+			temp, err := strconv.ParseInt(height, 10, 64)
+			if err != nil {
+				fmt.Printf("invalid height: %s\n", err.Error())
+				os.Exit(1)
+			}
+			heightInt = int(temp)
+		}
+
+		supply, err := client.GetTotalSupply(contract, heightInt)
 		if err != nil {
 			fmt.Println("could not get the supply:", err.Error())
 			os.Exit(1)
@@ -38,4 +51,5 @@ var supplyCmd = &cobra.Command{
 
 func init() {
 	ERC20Cmd.AddCommand(supplyCmd)
+	supplyCmd.Flags().String("height", "latest", "Query at the given height.")
 }
diff --git a/docs/pages/hanchond/playground/examples/debugEthCall.mdx b/docs/pages/hanchond/playground/examples/debugEthCall.mdx
new file mode 100644
index 0000000..9b7ab04
--- /dev/null
+++ b/docs/pages/hanchond/playground/examples/debugEthCall.mdx
@@ -0,0 +1,150 @@
+# Debug `eth_call` for archive queries
+
+:::info
+This example is using `hanchond` v0.2.0. Some interfaces may change in the future
+:::
+
+## Requirements
+
+```sh
+$ hanchond p build-evmos v18.1.0
+$ hanchond p build-evmos v19.1.0
+$ hanchond p build-hermes
+```
+
+## Set up the chains
+
+Create two chains with just one validator each using the `v18.1.0` Evmos version and start them
+
+```bash
+hanchond p remove-data && hanchond p init-chain 1 --version v18.1.0 && hanchond p init-chain 1 --version v18.1.0 && hanchond p start-chain 1 && hanchond p start-chain 2
+```
+
+## IBC config
+
+- Create an IBC Channel and start the relayer
+
+```bash
+hanchond p hermes-add-channel 1 2 && hanchond p start-hermes
+```
+
+- Send an IBC transfer
+
+```bash
+hanchond p tx ibc-transfer evmos1x49lse4ykqrvntuancrdjdjzz75xzmsj5nn9p0 1000 --node 2
+```
+
+- Verify the balance:
+
+```bash
+hanchond p q balance evmos1x49lse4ykqrvntuancrdjdjzz75xzmsj5nn9p0
+```
+
+## Register the ERC20
+
+```bash
+hanchond playground tx str-v1-proposal ibc/8EAC8061F4499F03D2D1419A3E73D346289AE9DB89CAB1486B72539572B1915E
+hanchond playground tx vote
+hanchond p q evmos token-pairs | jq .
+```
+
+We have an ERC20 with the address: `0x80b5a32E4F032B2a058b4F29EC95EEfEEB87aDcd`
+
+- Send another IBC transfer so all the balance is converted
+
+```bash
+hanchond p tx ibc-transfer evmos1x49lse4ykqrvntuancrdjdjzz75xzmsj5nn9p0 1000 --node 2
+```
+
+## Debug `eth_call`
+
+-Get the current value
+
+```bash
+hanchond p q height
+118
+
+hanchond p q erc20 balance 0x80b5a32E4F032B2a058b4F29EC95EEfEEB87aDcd 0x354bF866A4B006C9AF9d9e06d9364217A8616E12 --height 118
+2000
+```
+
+- Upgrade the network
+
+```bash
+hanchond playground tx upgrade-proposal v19.1.0 --height-diff 50
+hanchond playground tx vote
+```
+
+- Wait for the upgrade height
+
+```bash
+hanchond p q height
+235
+```
+
+### Apply the upgrade
+
+- Stop the node
+
+```bash
+hanchond p stop-node 1
+```
+
+- Change the version
+
+```bash
+hanchond p change-version 1 evmosdv19.1.0
+```
+
+- Restart the node
+
+```bash
+hanchond p start-node 1
+```
+
+### Query archive data
+
+```bash
+hanchond p q erc20 balance 0x80b5a32E4F032B2a058b4F29EC95EEfEEB87aDcd 0x354bF866A4B006C9AF9d9e06d9364217A8616E12 --height 118
+
+could not get the balance: rpc error: code = Internal desc = invalid opcode: PUSH0
+```
+
+## Create a patch
+
+- Clone the repo locally and apply the needed changes.
+
+- Build the binary locally
+
+```bash
+hanchond p build-evmos --path /Users/hanchon/devel/evmos/evmos
+```
+
+- Update the node to use the local fork
+
+```bash
+hanchond p change-version 1 evmosdlocal
+```
+
+- Stop the node
+
+```bash
+hanchond p stop-node 1
+```
+
+- Start the node
+
+```bash
+hanchond p start-node 1
+```
+
+- Test your patch
+
+```bash
+hanchond p q erc20 balance 0x80b5a32E4F032B2a058b4F29EC95EEfEEB87aDcd 0x354bF866A4B006C9AF9d9e06d9364217A8616E12 --height 118
+2000
+```
+
+:::info
+Logs can be found at `~/hanchond/data/1-0/run.log`
+:::
diff --git a/docs/pages/hanchond/playground/queries/erc20/balance.mdx b/docs/pages/hanchond/playground/queries/erc20/balance.mdx
index d24b8c4..f59c625 100644
--- a/docs/pages/hanchond/playground/queries/erc20/balance.mdx
+++ b/docs/pages/hanchond/playground/queries/erc20/balance.mdx
@@ -9,4 +9,6 @@ hanchond playground query erc20 balance 0x80b5a32E4F032B2a058b4F29EC95EEfEEB87aD
 
 :::info
 The command supports wallets in `Bech32` and `Hex` encoding
+
+The `height` flag is available to query archive data, it defaults to `latest`
 :::
diff --git a/docs/pages/hanchond/playground/queries/erc20/supply.mdx b/docs/pages/hanchond/playground/queries/erc20/supply.mdx
index f569c93..a63d073 100644
--- a/docs/pages/hanchond/playground/queries/erc20/supply.mdx
+++ b/docs/pages/hanchond/playground/queries/erc20/supply.mdx
@@ -6,3 +6,7 @@ The command `erc20 supply [contract]` can be used to get the total supply of an
 hanchond playground query erc20 supply 0x80b5a32E4F032B2a058b4F29EC95EEfEEB87aDcd
 5
 ```
+
+:::info
+The `height` flag is available to query archive data, it defaults to `latest`
+:::
diff --git a/playground/cosmosdaemon/config_files.go b/playground/cosmosdaemon/config_files.go
index 13dce9e..da068a2 100644
--- a/playground/cosmosdaemon/config_files.go
+++ b/playground/cosmosdaemon/config_files.go
@@ -28,7 +28,9 @@ func (d *Daemon) UpdateAppFile() error {
 	if err != nil {
 		return err
 	}
-	appFile = d.SetPruningInAppFile(true, appFile)
+
+	// No pruning to use archive queries
+	appFile = d.SetPruningInAppFile(false, appFile)
 	appFile = d.SetMinGasPricesInAppFile(appFile)
 	return d.SaveAppFile(appFile)
 }
diff --git a/playground/evmos/command_gov.go b/playground/evmos/command_gov.go
index 501aa92..34ecc25 100644
--- a/playground/evmos/command_gov.go
+++ b/playground/evmos/command_gov.go
@@ -76,7 +76,7 @@ func (e *Evmos) CreateSTRv1Proposal(params STRv1) (string, error) {
 		"--from",
 		e.ValKeyName,
 		"--gas-prices",
-		fmt.Sprintf("100%s", e.BaseDenom),
+		fmt.Sprintf("100000000000000%s", e.BaseDenom),
 		"--gas-adjustment",
 		"4",
 		"--gas",
@@ -105,7 +105,7 @@ func (e *Evmos) VoteOnProposal(proposalID string, option string) (string, error)
 		"--from",
 		e.ValKeyName,
 		"--gas-prices",
-		fmt.Sprintf("100%s", e.BaseDenom),
+		fmt.Sprintf("100000000000000%s", e.BaseDenom),
 		"--gas-adjustment",
 		"4",
 		"-y",
@@ -189,7 +189,7 @@ func (e *Evmos) CreateUpgradeProposal(versionName string, upgradeHeight string)
 		"--from",
 		e.ValKeyName,
 		"--gas-prices",
-		fmt.Sprintf("100%s", e.BaseDenom),
+		fmt.Sprintf("100000000000000%s", e.BaseDenom),
 		"--gas-adjustment",
 		"4",
 		"--gas",
diff --git a/vocs.config.ts b/vocs.config.ts
index 0b7e081..7c67fb7 100644
--- a/vocs.config.ts
+++ b/vocs.config.ts
@@ -208,6 +208,10 @@ export default defineConfig({
                   text: "v19.1.0 Upgrade (v0.1.0)",
                   link: "/hanchond/playground/examples/upgradev19",
                 },
+                {
+                  text: "Debug eth_call (v0.2.0)",
+                  link: "/hanchond/playground/examples/debugEthCall",
+                },
               ],
             },
           ],