注解
This documentation is under construction and the chain3.js 1.0 stable version isn’t released. If you’re using a version v0.1.x of chain3.js then please check chain3js 0.1.x or https://github.com/MOACChain/moac-core/wiki/Chain3.
Core Method Module¶
The Core Method Module does provide all method classes and the abstract method factory which will be used in the AbstractChain3Module.
AbstractMethodFactory¶
Source: AbstractMethodFactory
The AbstractMethodFactory does have the following constructor parameters:
utils-UtilsTheUtilsobject from theweb3-utilsmodule.formatters-ObjectThe formatters object from theweb3-core-helpersmodule.
Example¶
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
};
}
}
AbstractMethod¶
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-StringThe JSON-RPC method name.parametersAmount-NumberThe amount of parameters this JSON-RPC method has.utils-Utilsformatters-ObjectThe 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
- afterExecution(response: any): void
- beforeExecution(moduleInstance: AbstractWeb3Module): void
- setArguments(arguments: IArguments): void
- getArguments(arguments: IArguments): {parameters: any[], callback: Function}
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.
Example¶
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:
Type¶
The static readonly property Type will be used in the AbstractMethodFactory class to determine how the class should get initiated.
Reserved types:
observed-transaction-method-AbstractObservedTransactionMethodeth-send-transaction-method-EthSendTransactionMethod
Returns¶
string - Example: observed-transaction-method
beforeExecution¶
method.beforeExecution(moduleInstance)
This method will be executed before the JSON-RPC request. It provides the possibility to customize the given parameters or other properties of the current method.
afterExecution¶
method.afterExecution(response)
This method will get executed when the provider returns with the response.
The afterExecution method does provide us the possibility to map the response to the desired value.
Parameters¶
response-anyThe response from the provider.
execute¶
method.execute()
This method will execute the current method.
Returns¶
Promise<Object|string>|PromiEvent|string
rpcMethod¶
method.rpcMethod
This property will return the rpcMethod string.
It will be used for the creation of the JSON-RPC payload object.
Returns¶
string
parametersAmount¶
method.parametersAmount
This property will return the parametersAmount.
It will be used for validating the given parameters length and for the detection of the callback method.
Returns¶
number
parameters¶
method.parameters
This property does contain the given parameters.
Use the setArguments() method for setting the parameters and the callback method with the given IArguments object.
Returns¶
any[]
callback¶
method.callback
This property does contain the given callback.
Use the setArguments() method for setting the parameters and the callback method with the given IArguments object.
setArguments¶
method.setArguments(arguments)
This method will be used to set the given method arguments.
The setArguments method will set the parameters and callback property.
Parameters¶
arguments-Array: Theargumentsof the function call.
getArguments¶
method.getArguments()
This method will be used to get the method arguments.
The getArguments method will return a object with the properties parameters and callback.
Returns¶
Object
isHash¶
method.isHash(value)
This method will check if the given value is a string and starts with 0x.
It will be used in several methods for deciding which JSON-RPC method should get executed.
Parameters¶
value-string
AbstractObservedTransactionMethod¶
Source: AbstractObservedTransactionMethod
The AbstractObservedTransactionMethod extends from the AbstractMethod <web3-module-abstract-method and
does have the following constructor parameters:
rpcMethod-StringThe JSON-RPC method name.parametersAmount-NumberThe amount of parameters this JSON-RPC method has.utils-ObjectThe Utils object.formatters-ObjectThe formatters object.transactionObserver-TransactionObserverTheTransactionObserverclass which defines the confirmation process of the transaction.
The AbstractObservedTransactionMethod is the base method class for all “send transaction” methods.
Abstract methods:
Type¶
The static readonly property Type will be used in the AbstractMethodFactory class to determine how the class should get initiated.
Reserved types:
observed-transaction-method-AbstractObservedTransactionMethodeth-send-transaction-method-EthSendTransactionMethod
Returns¶
string - Example: observed-transaction-method
beforeExecution¶
method.beforeExecution(moduleInstance)
This method will be executed before the JSON-RPC request. It provides the possibility to customize the given parameters or other properties of the current method.
afterExecution¶
method.afterExecution(response)
This method will get executed when the provider returns with the response.
The afterExecution method does provide us the possibility to map the response to the desired value.
Parameters¶
response-anyThe response from the provider.
execute¶
method.execute()
This method will execute the current method.
Returns¶
Promise<Object|string>|PromiEvent|string
rpcMethod¶
method.rpcMethod
This property will return the rpcMethod string.
It will be used for the creation of the JSON-RPC payload object.
Returns¶
string
parametersAmount¶
method.parametersAmount
This property will return the parametersAmount.
It will be used for validating the given parameters length and for the detection of the callback method.
Returns¶
number
parameters¶
method.parameters
This property does contain the given parameters.
Use the setArguments() method for setting the parameters and the callback method with the given IArguments object.
Returns¶
any[]
callback¶
method.callback
This property does contain the given callback.
Use the setArguments() method for setting the parameters and the callback method with the given IArguments object.
setArguments¶
method.setArguments(arguments)
This method will be used to set the given method arguments.
The setArguments method will set the parameters and callback property.
Parameters¶
arguments-Array: Theargumentsof the function call.
getArguments¶
method.getArguments()
This method will be used to get the method arguments.
The getArguments method will return a object with the properties parameters and callback.
Returns¶
Object