Skip to content

Commit

Permalink
feat(rpc): add eth_chainId to tm rpc (#1364)
Browse files Browse the repository at this point in the history
  • Loading branch information
srene authored Feb 13, 2025
1 parent b43f03a commit 60b92da
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
16 changes: 14 additions & 2 deletions rpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"time"

sdkerrors "cosmossdk.io/errors"
"github.com/dymensionxyz/gerr-cosmos/gerrc"
evmostypes "github.com/evmos/evmos/v12/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/config"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
Expand All @@ -22,8 +24,6 @@ import (
tmtypes "github.com/tendermint/tendermint/types"
tm_version "github.com/tendermint/tendermint/version"

"github.com/dymensionxyz/gerr-cosmos/gerrc"

"github.com/dymensionxyz/dymint/mempool"
"github.com/dymensionxyz/dymint/node"
"github.com/dymensionxyz/dymint/types"
Expand Down Expand Up @@ -68,6 +68,10 @@ type ResultBlockValidated struct {
Result BlockValidationStatus
}

type ResultChainId struct {
ChainID string
}

// NewClient returns Client working with given node.
func NewClient(node *node.Node) *Client {
return &Client{
Expand Down Expand Up @@ -824,6 +828,14 @@ func (c *Client) CheckTx(ctx context.Context, tx tmtypes.Tx) (*ctypes.ResultChec
return &ctypes.ResultCheckTx{ResponseCheckTx: *res}, nil
}

func (c *Client) ChainID() (*ResultChainId, error) {
eip155ChainID, err := evmostypes.ParseChainID(c.node.BlockManager.State.ChainID)
if err != nil {
return &ResultChainId{}, nil
}
return &ResultChainId{ChainID: fmt.Sprintf("0x%x", eip155ChainID)}, nil
}

func (c *Client) BlockValidated(height *int64) (*ResultBlockValidated, error) {
_, _, chainID := c.node.P2P.Info()
// invalid height
Expand Down
8 changes: 8 additions & 0 deletions rpc/json/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/gorilla/rpc/v2"
"github.com/gorilla/rpc/v2/json2"

"github.com/dymensionxyz/dymint/rpc/client"
"github.com/dymensionxyz/dymint/types"
)

Expand Down Expand Up @@ -111,6 +112,13 @@ func (h *handler) serveJSONRPCforWS(w http.ResponseWriter, r *http.Request, wsCo
// from the declared content-type
w.Header().Set("x-content-type-options", "nosniff")

// it is necessary to return a plain string instead of json object for eth_chainId, since it is required by Metamask to validate chain id
if errResult == nil && method == "eth_chainId" {
result := (*client.ResultChainId)(rets[0].UnsafePointer())
codecReq.WriteResponse(w, result.ChainID)
return
}

// Encode the response.
if errResult == nil {
var raw json.RawMessage
Expand Down
5 changes: 5 additions & 0 deletions rpc/json/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func newService(c *client.Client, l types.Logger, opts ...option) *service {
"abci_info": newMethod(s.ABCIInfo),
"broadcast_evidence": newMethod(s.BroadcastEvidence),
"block_validated": newMethod(s.BlockValidated),
"eth_chainId": newMethod(s.ChainId),
}

for _, opt := range opts {
Expand Down Expand Up @@ -295,3 +296,7 @@ func (s *service) BlockValidated(req *http.Request, args *blockArgs) (*client.Re
fmt.Println(args)
return s.client.BlockValidated((*int64)(&args.Height))
}

func (s *service) ChainId(req *http.Request, args *blockArgs) (*client.ResultChainId, error) {
return s.client.ChainID()
}

0 comments on commit 60b92da

Please # to comment.