The Core Method Module
does provide all method classes and the abstract method factory which will be used in the AbstractChain3Module
.
Source: AbstractMethodFactory
The AbstractMethodFactory
does have the following constructor parameters:
utils
-Utils
TheUtils
object from theweb3-utils
module.formatters
-Object
The formatters object from theweb3-core-helpers
module.
import {
AbstractMethodFactory,
GetBlockByNumberMethod,
ListeningMethod,
PeerCountMethod,
VersionMethod
} from 'web3-core-method';
class MethodFactory extends AbstractMethodFactory {
/**
* @param {Utils} utils
* @param {Object} formatters
*
* @constructor
*/
constructor(utils, formatters) {
super(utils, formatters);
this.methods = {
getId: VersionMethod,
getBlockByNumber: GetBlockByNumberMethod,
isListening: ListeningMethod,
getPeerCount: PeerCountMethod
};
}
}
Source: AbstractMethod
Because we are always adding new JSON-RPC methods do we just link the methods folder as resource.
Source: Methods
The provided method classes do have the following interface:
The AbstractMethod
class does have the following constructor parameters:
rpcMethod
-String
The JSON-RPC method name.parametersAmount
-Number
The amount of parameters this JSON-RPC method has.utils
-Utils
formatters
-Object
The formatters object.moduleInstance
-AbstractWeb3Module
The AbstractMethod
class is the base JSON-RPC method class and does provide the basic methods and properties for creating a Web3.js compatible JSON-RPC method.
You're able to overwrite these methods:
execute(): PromiEvent <web3-abstract-method-execute>
afterExecution(response: any): void <web3-abstract-method-after-execution>
beforeExecution(moduleInstance: AbstractWeb3Module): void <web3-abstract-method-before-execution>
setArguments(arguments: IArguments): void <web3-abstract-method-set-arguments>
getArguments(arguments: IArguments): {parameters: any[], callback: Function} <web3-abstract-method-get-arguments>
This example will show the usage of the setArguments(arguments: IArguments)
method.
It's also possible to set the parameters and callback method directly over the parameters
and callback
property of the method class.
class Example extends AbstractWeb3Module {
constructor(...) {
// ...
}
sign() {
const method = new AbstractMethod('eth_sign', 2, utils, formatters, this);
method.setArguments(arguments)
return method.execute();
}
}
const example = new Example(...);
const response = await example.sign('0x0', 'message').
// > "response"
example.sign('0x0', 'message', (error, response) => {
console.log(response);
};
// > "response"
The AbstractMethod
class interface:
Source: AbstractObservedTransactionMethod
The AbstractObservedTransactionMethod
extends from the AbstractMethod <web3-module-abstract-method
and does have the following constructor parameters:
rpcMethod
-String
The JSON-RPC method name.parametersAmount
-Number
The amount of parameters this JSON-RPC method has.utils
-Object
The Utils object.formatters
-Object
The formatters object.transactionObserver
-TransactionObserver
TheTransactionObserver
class which defines the confirmation process of the transaction.
The AbstractObservedTransactionMethod
is the base method class for all "send transaction" methods.
Abstract methods:
afterExecution <web3-abstract-method-after-execution>
beforeExecution <web3-abstract-method-before-execution>