✏️
go-conflux-sdk
  • README
  • Quickstart
  • Getting Started
    • Run a Node locally
    • Interacting with a Node
    • Deploy and Interact with Smart Contracts
    • Subscription
  • Smart Contracts
    • Smart Contract Overview
    • Getting Started with Solidity
    • Compiling Solidity source code
    • Interacting with Smart Contracts
    • Application Binary Interface
    • Contracts Supported
  • Transactions
    • Conflux Transactions
    • Obtaining CFX
    • Gas & Storage Collateral
    • Account Manager
    • Transfer CFX
    • Transaction Nonce
    • Batch Call snd Send Transactions
  • Other
    • Call/BatchCall RPC Hook
    • Conflux Addreess
  • Conflux-ABIGEN
  • ChangeLog
  • References
Powered by GitBook
On this page

Was this helpful?

  1. Other

Call/BatchCall RPC Hook

Client composite MiddlewarableMiddle for support setting middleware by HookCallContext for hooking provider.CallContext method which is the core of all single RPC-related methods. And HookBatchCallContext to set middleware for hooking BatchCallContext.

For example, we can custom a logger middleware to log for rpc requests.

client.HookCallContext(callContextConsoleMiddleware)

and the callContextConsoleMiddleware implementation is like

func callContextConsoleMiddleware(f providers.CallFunc) providers.CallFunc {
	return func(ctx context.Context, resultPtr interface{}, method string, args ...interface{}) error {
		fmt.Printf("request %v %v\n", method, args)
		err := f(ctx, resultPtr, method, args...)
		j, _ := json.Marshal(resultPtr)
		fmt.Printf("response %s\n", j)
		return err
	}
}

Also, you could

  • customize middleware

  • use multiple middlewares

Notice that the middleware chain execution order is like onion, for example, use middleware A first and then middleware B

client.HookCallContext(A)
client.HookCallContext(B)

the middleware execution order is

A --> B --> client.callRpc --> B --> A
PreviousOtherNextConflux-ABIGEN

Last updated 3 years ago

Was this helpful?