3.6.0 • Published 5 years ago

web3-api-helper v3.6.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Web3 API Helper

Build Status

This module contains a couple of helper methods which extend the standard web3.js API (1.0.0 and above).

Install

npm install web3-api-helper

Encode Method

In order to encode a method, you'd need to provide its ABI and a list of parameters:

const web3Helper = require('web3-api-helper').Web3Helper;

let method = {
    name: 'transfer',
    type: 'function',
    inputs: [{
        type: 'address',
        name: 'to'
    },
    {
        type: 'uint256',
        name: 'value'
    }]
};

let params = ['0xff98336a9027A09355e7b4326CA79eFfE3660415', 500000000000000000];

let encoded = web3Helper.encodeMethod(method, params);
console.log(`Encoded function call is: ${encoded}\n`);

The output should be:

Encoding:
  Function: transfer
  Parameters:
    to (address): 0xff98336a9027A09355e7b4326CA79eFfE3660415
    value (uint256): 500000000000000000

Encoded function call is: 0xa9059cbb000000000000000000000000ff98336a9027a09355e7b4326ca79effe366041500000000000000000000000000000000000000000000000006f05b59d3b20000

Decode Method

In order to encode a method, you'd need to provide the encoded data. The helper will looks for an existing signature which matches it and then parse it accordingly:

const web3Helper = require('web3-api-helper').Web3Helper;

let decoded = web3Helper.decodeMethod("0xa9059cbb000000000000000000000000ff98336a9027a09355e7b4326ca79effe366041500000000000000000000000000000000000000000000000006f05b59d3b20000");

console.log(
    `Decoded: \n` +
    `  Function: ${decoded.abi.name} \n` +
    `  Parameters: \n${web3Helper.paramsToString(decoded.abi, decoded.params) || '    N/A'}`
);

The output should be:

Decoding:
0xa9059cbb000000000000000000000000ff98336a9027a09355e7b4326ca79effe366041500000000000000000000000000000000000000000000000006f05b59d3b20000

Decoded:
  Function: transfer(address,uint256)
  Parameters:
    to (address): 0xff98336a9027A09355e7b4326CA79eFfE3660415
    value (uint256): 500000000000000000
3.6.0

5 years ago

3.5.0

5 years ago

3.4.0

5 years ago

3.3.0

5 years ago

3.2.1

5 years ago

3.2.0

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.4.1

5 years ago

2.3.0

5 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago