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...
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityI'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...
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.