Skip to content

Commit

Permalink
refactoring : separate main package
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsuryansh committed Jul 3, 2023
1 parent 395ba79 commit 97db770
Show file tree
Hide file tree
Showing 81 changed files with 219 additions and 16 deletions.
1 change: 1 addition & 0 deletions EVMStateVoteClient/abi.json

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions EVMStateVoteClient/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ module EVMStateVoteClient

go 1.20

replace evmstorechain => ../../EVMStoreChain
replace evmstorechain => ../EVMStoreChain

replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

require (
evmstorechain v0.0.0-00010101000000-000000000000
github.com/ethereum/go-ethereum v1.12.0
github.com/ignite/cli v0.27.1
)

require (
cosmossdk.io/api v0.3.1 // indirect
cosmossdk.io/core v0.5.1 // indirect
cosmossdk.io/depinject v1.0.0-alpha.3 // indirect
cosmossdk.io/errors v1.0.0-beta.7 // indirect
cosmossdk.io/math v1.0.1 // indirect
evmstorechain v0.0.0-00010101000000-000000000000 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d // indirect
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
Expand All @@ -39,6 +45,7 @@ require (
github.com/cosmos/ledger-cosmos-go v0.12.1 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
Expand All @@ -49,11 +56,13 @@ require (
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/gorilla/mux v1.8.0 // indirect
Expand All @@ -67,8 +76,8 @@ require (
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hdevalence/ed25519consensus v0.1.0 // indirect
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c // indirect
github.com/huandu/skiplist v1.2.0 // indirect
github.com/ignite/cli v0.27.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.16.5 // indirect
Expand All @@ -90,6 +99,7 @@ require (
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rs/cors v1.9.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/cobra v1.7.0 // indirect
Expand All @@ -102,6 +112,8 @@ require (
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tidwall/btree v1.6.0 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
github.com/zondax/hid v0.9.1 // indirect
github.com/zondax/ledger-go v0.14.1 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
Expand All @@ -117,6 +129,7 @@ require (
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
pgregory.net/rapid v0.5.5 // indirect
Expand Down
110 changes: 108 additions & 2 deletions EVMStateVoteClient/go.sum

Large diffs are not rendered by default.

103 changes: 93 additions & 10 deletions EVMStateVoteClient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,92 @@ package main

import (
"context"
"strconv"
"fmt"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum"
"math/big"
"io/ioutil"
"log"
"github.com/ignite/cli/ignite/pkg/cosmosclient"
"evmstorechain/x/evmstorechain/types"
"os"
"fmt"
"strconv"
"strings"
"github.com/ignite/cli/ignite/pkg/cosmosclient"
"evmstorechain/x/evmstorechain/types"
)

type EthState struct {
Blocknumber *big.Int
BorrowIndex *big.Int
}

const contractAddress = "0xf650C3d88D12dB855b8bf7D11Be6C55A4e07dCC9"

func main() {
ethState := getStateFromEthereum()
PostEthereumStateToCosmos(ethState.Blocknumber, ethState.BorrowIndex)
QueryEthereumStateFromCosmos(ethState.Blocknumber)
}

func getStateFromEthereum() *EthState {
abiBytes, err := ioutil.ReadFile("abi.json")
if err != nil {
log.Fatal(err)
}

// Convert []byte to string
contractAbi := string(abiBytes)

fmt.Println("Querying borrowIndex from Ethereum")

// Get Infura URL from environment variable
infuraURL := os.Getenv("INFURA_URL")

client, err := ethclient.Dial(infuraURL)
if err != nil {
log.Fatalf("Failed to connect to the Ethereum client: %v", err)
}

parsedABI, err := abi.JSON(strings.NewReader(contractAbi))
if err != nil {
log.Fatalf("Failed to parse contract ABI: %v", err)
}

contractAddress := common.HexToAddress(contractAddress)

borrowIndex := parsedABI.Methods["borrowIndex"]

callMsg := ethereum.CallMsg{
To: &contractAddress,
Data: borrowIndex.ID,
}

header, err := client.HeaderByNumber(context.Background(), nil)
if err != nil {
log.Fatalf("Failed to get header: %v", err)
}

res, err := client.CallContract(context.Background(), callMsg, header.Number)
if err != nil {
log.Fatalf("Failed to execute function call: %v", err)
}

borrowIndexRes := new(big.Int)
borrowIndexRes.SetBytes(res)

fmt.Printf("BorrowIndex: %s\n", borrowIndexRes.String()) // Print the result

// Print the borrowIndex
log.Printf("Block number: %s, Borrow Index: %s", header.Number.String(), borrowIndexRes.String())

return &EthState{
Blocknumber: header.Number,
BorrowIndex: borrowIndexRes,
}
}

func PostEthereumStateToCosmos(blockNum, borrowIndex *big.Int) {
ctx := context.Background()
addressPrefix := "cosmosvaloper"

Expand All @@ -32,12 +110,10 @@ func main() {
}

// msg to submit a state
var blocknum uint64 = 1
var state uint64 = 12
msg := &types.MsgSubmitEthereumState{
Creator: addr,
Blocknumber: blocknum,
State: state,
Blocknumber: blockNum.Uint64(),
State: borrowIndex.Uint64(),
}

//broadcast transaction from accountName "alice"
Expand All @@ -48,12 +124,19 @@ func main() {
}
fmt.Println("Ethereum State Submited")
fmt.Println(txResp)
}

// Query
func QueryEthereumStateFromCosmos(blockNum *big.Int) {
ctx := context.Background()
client, err := cosmosclient.New(ctx, cosmosclient.WithAddressPrefix("cosmos"))
if err != nil {
log.Fatalf("Failed to connect to the Cosmos client: %v", err)
}
queryClient := types.NewQueryClient(client.Context())
queryResp, err := queryClient.Blockstoragestate(ctx, &types.QueryGetBlockstoragestateRequest{ Blocknumber: strconv.FormatUint(blocknum,10)})
queryResp, err := queryClient.Blockstoragestate(ctx, &types.QueryGetBlockstoragestateRequest{ Blocknumber: strconv.FormatUint(blockNum.Uint64(),10)})
if err!=nil {
log.Fatal(err)
}
fmt.Println("Query Result")
fmt.Println(queryResp)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 97db770

Please sign in to comment.