github.com/lmittmann/w3@v0.20.0/docs/pages/rpc-extension.mdx (about) 1 # Custom RPC Method Bindings 2 3 Custom RPC method bindings can be created by implementing the <DocLink title="w3types.RPCCaller" /> interface. By convention, the `w3types.RPCCaller` is setup using an unexported factory, which implements the <DocLink title="w3types.RPCCallerFactory" /> interface, to keep the package API small and readable. The factory stores the method parameters and the reference to the return value. 4 5 **Example:** RPC binding for the Otterscan `ots_getTransactionBySenderAndNonce` method ([Playground](https://pkg.go.dev/github.com/lmittmann/w3/w3types#example-RPCCaller-GetTransactionBySenderAndNonce)) 6 7 ```go 8 // TxBySenderAndNonceFactory requests the senders transaction hash by the nonce. 9 func TxBySenderAndNonceFactory(sender common.Address, nonce uint64) w3types.RPCCallerFactory[common.Hash] { 10 return &getTransactionBySenderAndNonceFactory{ 11 sender: sender, 12 nonce: nonce, 13 } 14 } 15 16 // getTransactionBySenderAndNonceFactory implements the w3types.RPCCaller and 17 // w3types.RPCCallerFactory interfaces. It stores the method parameters and 18 // the reference to the return value. 19 type getTransactionBySenderAndNonceFactory struct { 20 // params 21 sender common.Address 22 nonce uint64 23 24 // returns 25 returns *common.Hash 26 } 27 28 // Returns sets the reference to the return value. 29 func (f *getTransactionBySenderAndNonceFactory) Returns(txHash *common.Hash) w3types.RPCCaller { 30 f.returns = txHash 31 return f 32 } 33 34 // CreateRequest creates a batch request element for the Otterscan getTransactionBySenderAndNonce method. 35 func (f *getTransactionBySenderAndNonceFactory) CreateRequest() (rpc.BatchElem, error) { 36 return rpc.BatchElem{ 37 Method: "ots_getTransactionBySenderAndNonce", 38 Args: []any{f.sender, f.nonce}, 39 Result: f.returns, 40 }, nil 41 } 42 43 // HandleResponse handles the response of the Otterscan getTransactionBySenderAndNonce method. 44 func (f *getTransactionBySenderAndNonceFactory) HandleResponse(elem rpc.BatchElem) error { 45 if err := elem.Error; err != nil { 46 return err 47 } 48 return nil 49 } 50 ```