Hethers
  • Documentation
  • Getting Started
  • Application Programming Interface
    • Providers
      • Provider
        • Accounts Methods
        • Logs Methods
        • Network Status Methods
        • Transactions Methods
        • Event Emitter Methods
        • Base Provider
        • HederaProvider
      • Types
    • Contract Interaction
      • Contract
      • ContractFactory
      • Example: ERC-20 Contract
    • Utilities
      • Accounts
      • Addresses
      • Application Binary Interface
        • AbiCoder
        • ABI Formats
        • Fragments
        • Interface
      • BigNumber
      • Byte Manipulation
      • Constants
      • Display Logic and Input
      • Encoding Utilities
      • FixedNumber
      • Hashing Algorithms
      • HD Wallet
      • Logging
      • Property Utilities
      • Signing Key
      • Strings
      • Transactions
      • Web Utilities
      • Wordlists
    • Signers
  • Contributing
  • Other Resources
Powered by GitBook
On this page
  • Creating Instance
  • Coding Methods
  1. Application Programming Interface
  2. Utilities
  3. Application Binary Interface

AbiCoder

PreviousApplication Binary InterfaceNextABI Formats

Last updated 3 years ago

The AbiCoder is a collection of Coders which can be used to encode and decode the binary data formats used to interoperate between the EVM and higher level libraries.

Most developers will never need to use this class directly, since the class greatly simplifies these operations.

Creating Instance

For the most part, there should never be a need to manually create an instance of an AbiCoder, since one is created with the default coercion function when the library is loaded which can be used universally.

This is likely only needed by those with specific needs to override how values are coerced after they are decoded from their binary format.

new hethers.utils.AbiCoder( [ coerceFunc ] )

Create a new AbiCoder instance, which will call the coerceFunc on every decode, where the result of the call will be used in the Result.

The function signature is `(type, value)`, where the type is the string describing the type and the value is the processed value from the underlying Coder.

If the callback throws, the Result will contain a property that when accessed will throw, allowing for higher level libraries to recover from data errors.

hethers.utils.defaultAbiCoder ⇒ AbiCoder

An AbiCoder created when the library is imported which is used by the .

Coding Methods

abiCoder.encode( types , values ) ⇒ string<>

// Encoding simple types
abiCoder.encode([ "uint", "string" ], [ 1234, "Hello World" ]);
// '0x00000000000000000000000000000000000000000000000000000000000004d20000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000'

// Encoding with arrays types
abiCoder.encode([ "uint[]", "string" ], [ [ 1234, 5678 ] , "Hello World" ]);
// '0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000004d2000000000000000000000000000000000000000000000000000000000000162e000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000'

// Encoding complex structs (using positional properties)
abiCoder.encode(
  [ "uint", "tuple(uint256, string)" ],
  [
    1234,
    [ 5678, "Hello World" ]
  ]
);
// '0x00000000000000000000000000000000000000000000000000000000000004d20000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000162e0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000'

// Encoding complex structs (using keyword properties)
abiCoder.encode(
  [ "uint a", "tuple(uint256 b, string c) d" ],
  [
    1234,
    { b: 5678, c: "Hello World" }
  ]
);
// '0x00000000000000000000000000000000000000000000000000000000000004d20000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000162e0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000'

// Decoding simple types
data = "0x00000000000000000000000000000000000000000000000000000000000004d20000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000";
abiCoder.decode([ "uint", "string" ], data);
// [
//   { BigNumber: "1234" },
//   'Hello World'
// ]

// Decoding with arrays types
data = "0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000004d2000000000000000000000000000000000000000000000000000000000000162e000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000";
abiCoder.decode([ "uint[]", "string" ], data);
// [
//   [
//     { BigNumber: "1234" },
//     { BigNumber: "5678" }
//   ],
//   'Hello World'
// ]

// Decoding complex structs; unnamed parameters allows ONLY
// positional access to values
data = "0x00000000000000000000000000000000000000000000000000000000000004d20000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000162e0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000";
abiCoder.decode([ "uint", "tuple(uint256, string)" ], data);
// [
//   { BigNumber: "1234" },
//   [
//     { BigNumber: "5678" },
//     'Hello World'
//   ]
// ]

// Decoding complex structs; named parameters allows positional
// or keyword access to values
abiCoder.decode([ "uint a", "tuple(uint256 b, string c) d" ], data);
// [
//   { BigNumber: "1234" },
//   [
//     { BigNumber: "5678" },
//     'Hello World',
//     b: { BigNumber: "5678" },
//     c: 'Hello World'
//   ],
//   a: { BigNumber: "1234" },
//   d: [
//     { BigNumber: "5678" },
//     'Hello World',
//     b: { BigNumber: "5678" },
//     c: 'Hello World'
//   ]
// ]

Encode the array values according to the array of types, each of which may be a string or a .

abiCoder.decode( types , data ) ⇒

Decode the data according to the array of types, each of which may be a string or .

Interface
Interface
ParamType
ParamType
DataHexString
Result