ethers.js/docs.wrm/api/utils/abi/coder.wrm

120 lines
3.5 KiB
Plaintext

_section: AbiCoder @<AbiCoder> @SRC<abi:class.AbiCoder>
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 [[Interface]] class greatly simplifies these operations.
_subsection: Creating Instance @<AbiCoder--creating>
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.
_property: new ethers.utils.AbiCoder([coerceFunc]) @SRC<abi:constructor.AbiCoder>
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.
_property: ethers.utils.defaultAbiCoder => [[AbiCoder]]
An [[AbiCoder]] created when the library is imported which is used by
the [[Interface]].
_subsection: Coding Methods @<AbiCoder--methods>
_property: abiCoder.encode(types, values) => string<[[DataHexString]]> @<AbiCoder-encode> @SRC<abi/abi-coder>
Encode the array //values// according to the array of //types//, each of which may be a
string or a [[ParamType]].
_code: @lang<javascript>
//_hide: const abiCoder = utils.defaultAbiCoder;
// Encoding simple types
//_result:
abiCoder.encode([ "uint", "string" ], [ 1234, "Hello World" ]);
//_log:
//_hide: _page.example1 = _;
// Encoding with arrays types
//_result:
abiCoder.encode([ "uint[]", "string" ], [ [ 1234, 5678 ] , "Hello World" ]);
//_log:
//_hide: _page.example2 = _;
// Encoding complex structs (using positional properties)
//_result:
abiCoder.encode(
[ "uint", "tuple(uint256, string)" ],
[
1234,
[ 5678, "Hello World" ]
]
);
//_log:
//_hide: _page.example3 = _;
// Encoding complex structs (using keyword properties)
//_result:
abiCoder.encode(
[ "uint a", "tuple(uint256 b, string c) d" ],
[
1234,
{ b: 5678, c: "Hello World" }
]
);
//_log:
_property: abiCoder.decode(types, data) => [[Result]] @<AbiCoder-decode> @SRC<abi/abi-coder>
Decode the //data// according to the array of //types//, each of which may be a
string or [[ParamType]].
_code: @lang<javascript>
//_hide: const abiCoder = utils.defaultAbiCoder;
// Decoding simple types
//_hide: data = _page.example1;
//_verbatim: `data = ${ JSON.stringify(data) };`
//_result:
abiCoder.decode([ "uint", "string" ], data);
//_log:
// Decoding with arrays types
//_hide: data = _page.example2;
//_verbatim: `data = ${ JSON.stringify(data) };`
//_result:
abiCoder.decode([ "uint[]", "string" ], data);
//_log:
// Decoding complex structs; unnamed parameters allows ONLY
// positional access to values
//_hide: data = _page.example3;
//_verbatim: `data = ${ JSON.stringify(data) };`
//_result:
abiCoder.decode([ "uint", "tuple(uint256, string)" ], data);
//_log:
// Decoding complex structs; named parameters allows positional
// or keyword access to values
//_result:
abiCoder.decode([ "uint a", "tuple(uint256 b, string c) d" ], data);
//_log: