Skip to content

Latest commit

 

History

History
171 lines (109 loc) · 5.55 KB

chain3-core-method.rst

File metadata and controls

171 lines (109 loc) · 5.55 KB

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 - Utils The Utils object from the web3-utils module.
  • formatters - Object The formatters object from the web3-core-helpers module.

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 - 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.

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:



AbstractObservedTransactionMethod

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 The TransactionObserver 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>