Nodejs and Javascript library for decoding data params and events from ethereum transactions
npx @tessl/cli install tessl/npm-abi-decoder@2.4.0ABI Decoder is a JavaScript library for decoding transaction data parameters and event logs from Ethereum blockchain transactions. It provides comprehensive ABI (Application Binary Interface) decoding capabilities for both Node.js and browser environments, enabling developers to interpret smart contract method calls and event emissions.
npm install abi-decoderconst abiDecoder = require('abi-decoder');For browser environments:
<script src="bower_components/abi-decoder/dist/abi-decoder.js"></script>const abiDecoder = require('abi-decoder');
// Add ABI definitions for contracts you want to decode
const contractABI = [
{
"inputs": [{"type": "address[]", "name": "_owners"}, {"type": "uint256", "name": "_required"}],
"name": "create",
"type": "function"
},
{
"inputs": [{"indexed": false, "type": "address", "name": "sender"}],
"type": "event",
"name": "ContractInstantiation"
}
];
abiDecoder.addABI(contractABI);
// Decode transaction input data
const txData = "0x53d9d910..."; // transaction input data
const decodedMethod = abiDecoder.decodeMethod(txData);
// Decode event logs from transaction receipt
const logs = [/* log objects from transaction receipt */];
const decodedLogs = abiDecoder.decodeLogs(logs);ABI Decoder is built around several key components:
Registers ABI definitions for smart contracts to enable decoding of their method calls and events.
/**
* Add ABI definitions to the decoder
* @param {Array} abiArray - Array of ABI objects defining contract interfaces
* @throws {Error} If abiArray is not an array
*/
function addABI(abiArray);Usage Example:
const contractABI = [
{
"inputs": [{"type": "address", "name": "to"}, {"type": "uint256", "name": "value"}],
"name": "transfer",
"type": "function"
}
];
abiDecoder.addABI(contractABI);Removes previously registered ABI definitions from the decoder.
/**
* Remove ABI definitions from the decoder
* @param {Array} abiArray - Array of ABI objects to remove
* @throws {Error} If abiArray is not an array
*/
function removeABI(abiArray);Important Notes:
getABIs()addABIRetrieves all currently registered ABI definitions.
/**
* Get all currently stored ABI definitions
* @returns {Array} Array of all registered ABI objects
*/
function getABIs();Returns the internal mapping of method signatures to ABI objects.
/**
* Get mapping of method signatures to ABI objects
* @returns {Object} Object with method IDs as keys and ABI objects as values
*/
function getMethodIDs();Decodes transaction input data into human-readable method call information.
/**
* Decode transaction input data into method call information
* @param {string} data - Hex-encoded transaction input data (starts with 0x)
* @returns {Object|undefined} Decoded method object or undefined if method not found
*/
function decodeMethod(data);Return Format:
interface DecodedMethod {
name: string; // Method name
params: Array<{
name: string; // Parameter name
value: any; // Decoded parameter value
type: string; // Parameter type (uint256, address, etc.)
}>;
}Usage Example:
const txData = "0x53d9d9100000000000000000...";
const decoded = abiDecoder.decodeMethod(txData);
if (decoded) {
console.log(`Method: ${decoded.name}`);
decoded.params.forEach(param => {
console.log(`${param.name} (${param.type}): ${param.value}`);
});
}Type Handling:
Decodes event logs from transaction receipts into human-readable event information.
/**
* Decode event logs from transaction receipts
* @param {Array} logs - Array of log objects from transaction receipt
* @returns {Array} Array of decoded event objects
*/
function decodeLogs(logs);Return Format:
interface DecodedLog {
name: string; // Event name
address: string; // Contract address that emitted the event
events: Array<{
name: string; // Parameter name
type: string; // Parameter type
value: any; // Decoded parameter value
}>;
}Usage Example:
// From web3.js transaction receipt
web3.eth.getTransactionReceipt(txHash, (error, receipt) => {
if (!error) {
const decodedLogs = abiDecoder.decodeLogs(receipt.logs);
decodedLogs.forEach(log => {
console.log(`Event: ${log.name} from ${log.address}`);
log.events.forEach(event => {
console.log(`${event.name} (${event.type}): ${event.value}`);
});
});
}
});Event Processing:
Expected structure for log objects passed to decodeLogs:
interface LogObject {
data: string; // Hex-encoded log data
topics: string[]; // Array of log topics (event signature + indexed params)
address: string; // Contract address that emitted the log
}Standard Ethereum ABI format for functions and events:
interface ABIFunction {
type: "function";
name: string;
inputs: Array<{
name: string;
type: string;
components?: Array<{ // For tuple types (ABIEncoderV2)
name: string;
type: string;
}>;
}>;
}
interface ABIEvent {
type: "event";
name: string;
inputs: Array<{
name: string;
type: string;
indexed: boolean;
}>;
}The library throws errors in the following cases:
addABI() or removeABI() receive non-array inputError Handling Example:
try {
abiDecoder.addABI(invalidABI);
} catch (error) {
console.error('Failed to add ABI:', error.message);
}
// decodeMethod returns undefined for unrecognized methods
const decoded = abiDecoder.decodeMethod(unknownData);
if (!decoded) {
console.log('Method not found in registered ABIs');
}The library depends on:
These dependencies are automatically installed with the package.