Skip to content

Commit 7906066

Browse files
authored
add ethrpc#DoRequest sugar for easily calling custom/arbitrary json-rpc method (#199)
1 parent 85820c3 commit 7906066

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

ethrpc/ethrpc_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,25 @@ func TestDebugTraceTransaction(t *testing.T) {
299299
require.NotEmpty(t, payload)
300300
}*/
301301

302+
func TestDoRequest_SeqChainHealth(t *testing.T) {
303+
p, err := ethrpc.NewProvider("https://dev-nodes.sequence.app/polygon")
304+
require.NoError(t, err)
305+
306+
result, err := ethrpc.DoRequest(context.Background(), p, "seq_chainHealth")
307+
require.NoError(t, err)
308+
require.NotNil(t, result)
309+
310+
// Expect isHealthy to be true
311+
isHealthy, ok := result["isHealthy"].(bool)
312+
require.True(t, ok, "expected isHealthy to be a bool")
313+
assert.True(t, isHealthy, "expected isHealthy to be true")
314+
315+
// Expect expiresAt to be present as a string (RFC3339 timestamp)
316+
expiresAt, ok := result["expiresAt"].(string)
317+
require.True(t, ok, "expected expiresAt to be a string")
318+
assert.NotEmpty(t, expiresAt)
319+
}
320+
302321
func TestFetchBlockWithInvalidVRS(t *testing.T) {
303322
url := "https://rpc.telos.net"
304323
// url := "https://node.mainnet.etherlink.com"

ethrpc/interface.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,19 @@ type DebugInterface interface {
211211
DebugTraceBlockByHash(ctx context.Context, blockHash common.Hash) ([]*TransactionDebugTrace, error)
212212
DebugTraceTransaction(ctx context.Context, txHash common.Hash) (*CallDebugTrace, error)
213213
}
214+
215+
// DoRequest is a helper for sending a custom JSON-RPC method to a provider
216+
// and decoding the response into a map[string]any.
217+
//
218+
// Usage:
219+
//
220+
// result, err := ethrpc.DoRequest(ctx, provider, "seq_chainHealth")
221+
// result, err := ethrpc.DoRequest(ctx, provider, "custom_method", arg1, arg2)
222+
func DoRequest(ctx context.Context, provider Interface, method string, args ...any) (map[string]any, error) {
223+
var result map[string]any
224+
_, err := provider.Do(ctx, NewCallBuilder[map[string]any](method, nil, args...).Into(&result))
225+
if err != nil {
226+
return nil, err
227+
}
228+
return result, nil
229+
}

0 commit comments

Comments
 (0)