Skip to content

Commit

Permalink
Merge branch 'development' into 3-create-contract-utility
Browse files Browse the repository at this point in the history
  • Loading branch information
nagdahimanshu committed Jan 18, 2024
2 parents 3950db4 + 87774a9 commit 7bc4b66
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 208 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ require (
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ github.com/crate-crypto/go-ipa v0.0.0-20231025140028-3c0104f4b233 h1:d28BXYi+wUp
github.com/crate-crypto/go-ipa v0.0.0-20231025140028-3c0104f4b233/go.mod h1:geZJZH3SzKCqnz5VT0q/DyIG/tvu/dZk+VIfXicupJs=
github.com/crate-crypto/go-kzg-4844 v0.7.0 h1:C0vgZRk4q4EZ/JgPfzuSoxdCq3C3mOZMBShovmncxvA=
github.com/crate-crypto/go-kzg-4844 v0.7.0/go.mod h1:1kMhvPgI0Ky3yIa+9lFySEBUBXkYxeOi8ZF1sYioxhc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set/v2 v2.1.0 h1:g47V4Or+DUdzbs8FxCCmgb6VYd+ptPAngjM6dtGktsI=
Expand Down Expand Up @@ -143,12 +142,6 @@ github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKl
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/status-im/keycard-go v0.2.0 h1:QDLFswOQu1r5jsycloeQh3bVU8n/NatHHaZobtDnDzA=
github.com/status-im/keycard-go v0.2.0/go.mod h1:wlp8ZLbsmrF6g6WjugPAx+IzoLrkdf9+mHxBEeo3Hbg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/supranational/blst v0.3.11 h1:LyU6FolezeWAhvQk0k6O/d49jqgO52MSDDfYgbeoEm4=
Expand Down Expand Up @@ -202,7 +195,6 @@ gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXL
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU=
Expand Down
87 changes: 28 additions & 59 deletions pkg/chain/chain.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package chain implements everything related to interaction with smart contracts, rpcprovider, etc
// Package chain implements everything related to interaction with smart contracts, rpcprovider, etc.
package chain

import (
Expand All @@ -10,92 +10,61 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
)

const (
RPCEndpointGetProof = "eth_getProof"
rpcEndpointGetProof = "eth_getProof"
)

type APIMethods interface {
ChainID(ctx context.Context) (*big.Int, error)
BlockNumber(ctx context.Context) (uint64, error)
BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
Client() *rpc.Client
// ChainAPIClient connects and encapsulates all the methods to interact with a chain.
type ChainAPIClient struct {
eth *ethclient.Client
log log.Logger
}

type RPCClient interface {
Call(result interface{}, method string, args ...interface{}) error
type ProofResponse struct {
Address common.Address `json:"address"`
AccountProof []hexutil.Bytes `json:"accountProof"`
Balance *hexutil.Big `json:"balance"`
CodeHash common.Hash `json:"codeHash"`
Nonce hexutil.Uint64 `json:"nonce"`
StorageHash common.Hash `json:"storageHash"`
StorageProof []common.Hash `json:"storageProof"`
}

// GetAPIClient return [ChainAPIClient] with client attached.
func GetAPIClient(url string, log log.Logger) (*ChainAPIClient, error) {
client, err := ethclient.Dial(url)
// GetAPIClient returns [ChainAPIClient] with client attached.
func GetAPIClient(ctx context.Context, url string, log log.Logger) (*ChainAPIClient, error) {
client, err := ethclient.DialContext(ctx, url)
if err != nil {
return nil, err
}

return NewChainAPIClient(client, log)
}

type ChainAPIClient struct {
apiClient APIMethods
log log.Logger
}

// NewChainAPIClient returns a [ChainAPIClient], wrapping all RPC endpoints to access chain related data.
func NewChainAPIClient(apiClient APIMethods, log log.Logger) (*ChainAPIClient, error) {
return &ChainAPIClient{
apiClient: apiClient,
log: log,
eth: client,
log: log,
}, nil
}

// Returns chainID of the connected node.
// GetChainID returns chainID of a connected node.
func (c *ChainAPIClient) GetChainID(ctx context.Context) (*big.Int, error) {
chainID, err := c.apiClient.ChainID(ctx)
if err != nil {
return nil, err
}

return chainID, nil
return c.eth.ChainID(ctx)
}

// Returns latest block number from the connected node.
// GetLatestBlockNumber returns latest block number from a connected node.
func (c *ChainAPIClient) GetLatestBlockNumber(ctx context.Context) (uint64, error) {
blockNumber, err := c.apiClient.BlockNumber(ctx)
if err != nil {
return 0, err
}

return blockNumber, nil
return c.eth.BlockNumber(ctx)
}

// Returns block for a given block number from the connected node.
// GetBlockByNumber returns block for a given block number from a connected node.
func (c *ChainAPIClient) GetBlockByNumber(ctx context.Context, blockNumber *big.Int) (*types.Block, error) {
block, err := c.apiClient.BlockByNumber(ctx, blockNumber)
if err != nil {
return nil, err
}

return block, nil
}

type ProofResponse struct {
Address common.Address `json:"address"`
AccountProof []hexutil.Bytes `json:"accountProof"`
Balance *hexutil.Big `json:"balance"`
CodeHash common.Hash `json:"codeHash"`
Nonce hexutil.Uint64 `json:"nonce"`
StorageHash common.Hash `json:"storageHash"`
StorageProof []common.Hash `json:"storageProof"`
return c.eth.BlockByNumber(ctx, blockNumber)
}

// Returns the account and storage values, including the Merkle proof, of the specified account/address.
func (c *ChainAPIClient) GetProof(client RPCClient, blockNumber *big.Int, address string) (*ProofResponse, error) {
// GetProof returns the account and storage values, including the Merkle proof, of the specified account/address.
func (c *ChainAPIClient) GetProof(ctx context.Context, blockNumber *big.Int, address common.Address) (*ProofResponse, error) {
var result ProofResponse

if err := client.Call(&result, RPCEndpointGetProof, address, []string{}, blockNumber); err != nil {
if err := c.eth.Client().CallContext(ctx, &result, rpcEndpointGetProof, address, []string{}, hexutil.Big(*blockNumber)); err != nil {
return nil, err
}

Expand Down
49 changes: 0 additions & 49 deletions pkg/chain/chain_mocks_test.go

This file was deleted.

91 changes: 0 additions & 91 deletions pkg/chain/chain_test.go

This file was deleted.

0 comments on commit 7bc4b66

Please sign in to comment.