Options
All
  • Public
  • Public/Protected
  • All
Menu

External module @truffle/decoder

This module provides an interface for decoding contract state, transaction calldata, and events. It's an interface to the same low-level decoding functionality that Truffle Debugger uses. However, it has additional functionality that the debugger does not need, and the debugger has additional functionality that this interface either does not need or cannot currently replicated. In the future, this interface will also decode return values and revert strings.

The interface is split into three classes: The wire decoder, the contract decoder, and the contract instance decoder. The wire decoder is associated to the project as a whole and decodes transaction calldata and events. The contract decoder is associated to a specific contract class. It has all the capabilities of the wire decoder, but in addition it acts as a factory for contract instance decoders. The contract instance decoder is associated to a specific contract instance; it too has all the capabilities of the wire decoder, but it can also decode the state variables for the specific instance. (In addition, in the case that the contract does not include a deployedBytecode field in its artifact, which can hinder decoding certain things, the contract instance decoder can sometimes work around this where the other decoders cannot.)

This documentation describes the current state of the decoder, but you should expect to see improvements soon. Note that most of the documentation is not found in this README, but rather in this package's API documentation. However, this README describes the overall approach.

Usage

Initialization

Create a decoder with one of the various constructor functions.

For a wire decoder, use the forProject function.

For a contract decoder, use the forArtifact or forContract function.

For a contract instance decoder, use one of the following: forDeployedArtifact forDeployedContract forArtifactAt forContractAt [[forContractAbstraction|forContractAbstraction]]

See the API documentation of these functions for details, or below for usage examples.

All of these functions presently require a final argument containing all artifacts that could potentially be relevant. It's intended that this argument will be optional in the future.

Decoder methods

See the documentation for the individual decoder classes for a method listing.

Output format information

The decoder outputs lossless, machine-readable Format.Values.Result objects containing individual decoded values. See the Format documentation for an overview and complete module listing.

Decoding modes and abification

The decoder runs in either of two modes: full mode or ABI mdoe. Full mode requires some additional constraints but returns substantially more detailed information. Please see the notes on Decoding modes for more about this distinction.

Basic usage examples

Decoding a log with the wire decoder

This usage example is for a project with two contracts, Contract1 and Contract2.

import { forProject } from "@truffle/decoder";
const contract1 = artifacts.require("Contract1");
const contract2 = artifacts.require("Contract2");
const provider = web3.currentProvider;
const decoder = await Decoder.forProject(provider, [contract1, contract2]);
const decodedLog = await decoder.decodeLog(log);

The usage of decodeTransaction is similar.

For getting already-decoded logs meeting appropriate conditions, see WireDecoder.events.

Decoding state variables with the contract instance decoder

This usage example is for decoding the state variables of a contract Contract in a project that also contains a contract OtherContract.

import { forContract } from "@truffle/decoder";
const contract = artifacts.require("Contract");
const otherContract = artifacts.require("OtherContract");
const decoder = await Decoder.forContract(contract, [otherContract]);
const instanceDecoder = await decoder.forInstance();
const variables = await instanceDecoder.variables();

In this example, we use the deployed version of Contract. If we wanted an instance at a different address, we could pass the address to forInstance.

In addition, rather than using forContract and then forInstance, we could also use forContractInstance to perform both of these in one step. If we wanted to do this with a specified address, we could use forContractAt.

Yet another way would be:

import { forContractAbstraction } from "@truffle/decoder";
const contract = artifacts.require("Contract");
const otherContract = artifacts.require("OtherContract");
const deployedContract = await contract.deployed();
const instanceDecoder = await Decoder.forContractAbstraction(deployedContract, [otherContract]);
const variables = await instanceDecoder.variables();

These examples are not exhaustive.

See the API documentation for more advanced decoding examples with variable or watchMappingKey.

Index

Inputs Type aliases

BlockSpecifier

BlockSpecifier: number | "genesis" | "latest" | "pending"

Specifies a block. Can be given by number, or can be given via the special strings "genesis", "latest", or "pending".

Intended to work like Web3's BlockType.

Warning: Using "pending", while allowed, is not advised, as it may lead to internally inconsistent results. Use of "latest" is safe and will not lead to inconsistent results from a single decoder call due to the decoder's caching system, but pending blocks cannot be cached under this system, which may cause inconsistencies.

Provider-based Constructor Functions

forArtifact

  • forArtifact(artifact: ContractObject, provider: Provider, artifacts: ContractObject[]): Promise<ContractDecoder>
  • This function is asynchronous.

    Constructs a contract decoder for a given contract artifact.

    Parameters

    • artifact: ContractObject

      The artifact for the contract.

      A contract constructor object may be substituted for the artifact, so if you're not sure which you're dealing with, it's OK.

    • provider: Provider

      The Web3 provider object to use.

    • artifacts: ContractObject[]

      A list of artifacts for other contracts in the project that may be relevant (e.g., providing needed struct or enum definitions, or appearing as a contract type).

      Contract constructor objects may be substituted for artifacts, so if you're not sure which you're dealing with, it's OK.

      Including the contract itself here is fine; so is excluding it.

      This parameter is intended to be made optional in the future.

    Returns Promise<ContractDecoder>

forArtifactAt

  • forArtifactAt(artifact: ContractObject, provider: Provider, address: string, artifacts: ContractObject[]): Promise<ContractInstanceDecoder>
  • This function is asynchronous.

    Constructs a contract instance decoder for a contract instance at a given address.

    Parameters

    • artifact: ContractObject

      The artifact corresponding to the type of the contract.

      A contract constructor object may be substituted for the artifact, so if you're not sure which you're dealing with, it's OK.

    • provider: Provider

      The Web3 provider object to use.

    • address: string

      The address of the contract instance to decode.

      Address must either be checksummed, or in all one case to circumvent the checksum. Mixed-case with bad checksum will cause this function to throw an exception.

    • artifacts: ContractObject[]

      A list of artifacts for other contracts in the project that may be relevant (e.g., providing needed struct or enum definitions, or appearing as a contract type).

      See the artifacts parameter documentation on forArtifact for more detail.

      This parameter is intended to be made optional in the future.

    Returns Promise<ContractInstanceDecoder>

forDeployedArtifact

  • forDeployedArtifact(artifact: ContractObject, provider: Provider, artifacts: ContractObject[]): Promise<ContractInstanceDecoder>
  • This function is asynchronous.

    Constructs a contract instance decoder for a deployed contract instance.

    Parameters

    • artifact: ContractObject

      The artifact corresponding to the type of the contract.

      A contract constructor object may be substituted for the artifact, so if you're not sure which you're dealing with, it's OK.

    • provider: Provider

      The Web3 provider object to use.

    • artifacts: ContractObject[]

      A list of artifacts for other contracts in the project that may be relevant (e.g., providing needed struct or enum definitions, or appearing as a contract type).

      See the artifacts parameter documentation on forArtifact for more detail.

      This parameter is intended to be made optional in the future.

    Returns Promise<ContractInstanceDecoder>

forProject

  • forProject(provider: Provider, artifacts: ContractObject[]): Promise<WireDecoder>
  • This function is asynchronous.

    Constructs a wire decoder for the project.

    Parameters

    • provider: Provider

      The Web3 provider object to use.

    • artifacts: ContractObject[]

      A list of contract artifacts for contracts in the project.

      Contract constructor objects may be substituted for artifacts, so if you're not sure which you're dealing with, it's OK.

      This parameter is intended to be made optional in the future.

    Returns Promise<WireDecoder>

Truffle Contract-based Constructor Functions

forContract

  • forContract(contract: ContractConstructorObject, artifacts: ContractObject[]): Promise<ContractDecoder>
  • This function is asynchronous.

    Constructs a contract decoder for a given contract.

    Parameters

    • contract: ContractConstructorObject

      The contract constructor object corresponding to the type of the contract.

    • artifacts: ContractObject[]

      A list of artifacts for other contracts in the project that may be relevant (e.g., providing needed struct or enum definitions, or appearing as a contract type).

      See the artifacts parameter documentation on forArtifact for more detail.

      This parameter is intended to be made optional in the future.

    Returns Promise<ContractDecoder>

forContractAt

  • forContractAt(contract: ContractConstructorObject, address: string, artifacts: ContractObject[]): Promise<ContractInstanceDecoder>
  • This function is asynchronous.

    Constructs a contract instance decoder for a contract instance at a given address.

    Parameters

    • contract: ContractConstructorObject

      The contract constructor object corresponding to the type of the contract.

    • address: string

      The address of the contract instance to decode.

      Address must either be checksummed, or in all one case to circumvent the checksum. Mixed-case with bad checksum will cause this function to throw an exception.

    • artifacts: ContractObject[]

      A list of artifacts for other contracts in the project that may be relevant (e.g., providing needed struct or enum definitions, or appearing as a contract type).

      See the artifacts parameter documentation on forArtifact for more detail.

      This parameter is intended to be made optional in the future.

    Returns Promise<ContractInstanceDecoder>

forContractInstance

  • forContractInstance(contract: ContractInstanceObject, artifacts: ContractObject[]): Promise<ContractInstanceDecoder>
  • This function is asynchronous.

    Constructs a contract instance decoder for a given contract instance.

    Parameters

    • contract: ContractInstanceObject

      The contract abstraction object corresponding to the contract instance.

    • artifacts: ContractObject[]

      A list of artifacts for other contracts in the project that may be relevant (e.g., providing needed struct or enum definitions, or appearing as a contract type).

      See the artifacts parameter documentation on forArtifact for more detail.

      This parameter is intended to be made optional in the future.

    Returns Promise<ContractInstanceDecoder>

forDeployedContract

  • forDeployedContract(contract: ContractConstructorObject, artifacts: ContractObject[]): Promise<ContractInstanceDecoder>
  • This function is asynchronous.

    Constructs a contract instance decoder for a deployed contract instance.

    Parameters

    • contract: ContractConstructorObject

      The contract constructor object corresponding to the type of the contract.

    • artifacts: ContractObject[]

      A list of artifacts for other contracts in the project that may be relevant (e.g., providing needed struct or enum definitions, or appearing as a contract type).

      See the artifacts parameter documentation on forArtifact for more detail.

      This parameter is intended to be made optional in the future.

    Returns Promise<ContractInstanceDecoder>

Generated using TypeDoc