3

I'm building an Ethereum private network and I want to make a custom JSON-RPC endpoint.

I've been customizing and adding some things in the go-ethereum source code but I don't know yet where to look to make this and which files would be involved...

1 Answer 1

1

Trying different things I found that the answer is very simple:

Just by adding a method in go-ethereum/internal/ethapi/api.go let's say "MyMethod", it will map to the JSON-RPC method "eth_myMethod" like so:

// go-ethereum/internal/ethapi/api.go
func (s *PublicTransactionPoolAPI) MyMethod(ctx context.Context, text string) error {
    log.Info(text)
    return nil
}


RPC call:

{
    "jsonrpc": "2.0",
    "method": "eth_myMethod",
    "params": [
        "Hello, World!"
    ],
    "id": 1
}


That will return:

{
    "jsonrpc": "2.0",
    "id": 1,
    "result": null
}


And will print Hello, World! in the console.

2
  • I have tried but did not work for me, keep getting "eth_myMethod does not exist/is not available" error message.
    – mamur
    Nov 21, 2022 at 5:35
  • {"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"the method eth_myMethod does not exist/is not available"}}
    – aakash4dev
    May 22, 2023 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.