Ethereum Contract ABI codec. Encodes function input and output according to provided contract ABI.
The purpose of this is library is to simplify working directly with JSON RPC methods such as eth_call
and eth_sendTransaction
.
Based on web3-eth-abi.
npm install abi-codec
yarn add abi-codec
- jsonInterface -
Object
: Contract JSON interface - method -
String
: Method name - params -
Array
: Parameters to encode
String
: Encoded input
- jsonInterface -
Object
: Contract JSON interface - method -
String
: Method name - output -
String
: Bytes to decode
Array
: Decoded output
// Example using an abbreviated JSON interface for the ENS registrar contract
const { encodeInput, decodeOutput } = require('abi-codec')
const jsonInterface = [
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "resolver",
"outputs": [
{
"name": "",
"type": "address"
}
]
}
...
]
// Encode input for JSON RPC method `eth_call`
const namehash = '0x78c5b99cf4668cf6da387866de4331c78b75b7db0087988c552f73e1714447b9' // 32 byte name hash for 'ethereum.eth'
encodeInput(jsonInterface, 'resolver', [namehash])
// -> '0x0178b8bf78c5b99cf4668cf6da387866de4331c78b75b7db0087988c552f73e1714447b9'
// Decode output from JSON RPC method `eth_call`
const output = '0x0178b8bf78c5b99cf4668cf6da387866de4331c78b75b7db0087988c552f73e1714447b9'
decodeOutput(jsonInterface, 'resolver', output)
// -> [ '0x1da022710dF5002339274AaDEe8D58218e9D6AB5' ]