AbiCoder

AbiCoder是编码器的集合,可用于在EVM和更高级别库之间通过二进制数据格式进行编码和解码的操作。

大多数开发人员不需要直接使用这个类,因为Interface类极大地简化了这些操作。

创建实例

在大多数情况下,都不需要手动创建AbiCoder的实例,因为当库被加载时,一个实例是用默认的强制函数创建的,它可以被普遍使用。

这可能只有那些有特殊需求的人才需要,通过二进制格式解码后将值进行强制更改。

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 ethers.utils.AbiCoder( [ coerceFunc ] )

创建一个新的AbiCoder实例,在每一个需要解码的地方都会调用coerceFunc,调用的结果将在Result中使用。

函数签名是`(type, value)`,type字段是是描述类型的字符串,value 字段是来自底层Coder的处理过的值。

如果回调抛出错误,Result将包含一个被访问时将抛出的属性,从而允许更高级别的库从数据错误中恢复。

ethers.utils.defaultAbiCoder AbiCoder

Interface使用的库在被导入时将创建一个AbiCoder

Coding Methods

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

根据数组中的types对数组values进行编码,每个类型可以是字符串或ParamType

// 对简单的类型进行编码 abiCoder.encode([ "uint", "string" ], [ 1234, "Hello World" ]); // '0x00000000000000000000000000000000000000000000000000000000000004d20000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000' // 对数组的类型进行编码 abiCoder.encode([ "uint[]", "string" ], [ [ 1234, 5678 ] , "Hello World" ]); // '0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000004d2000000000000000000000000000000000000000000000000000000000000162e000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000' // 对复杂的结构体进行编码 (使用 positional 属性) abiCoder.encode( [ "uint", "tuple(uint256, string)" ], [ 1234, [ 5678, "Hello World" ] ] ); // '0x00000000000000000000000000000000000000000000000000000000000004d20000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000162e0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000' // 对复杂的结构体进行编码 (使用 keyword 属性) abiCoder.encode( [ "uint a", "tuple(uint256 b, string c) d" ], [ 1234, { b: 5678, c: "Hello World" } ] ); // '0x00000000000000000000000000000000000000000000000000000000000004d20000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000162e0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000'
abiCoder.decode( types , data ) Result

根据数组中的typesdata进行编码,每个类型可以是字符串或ParamType

// 对简单的类型进行解码 data = "0x00000000000000000000000000000000000000000000000000000000000004d20000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000"; abiCoder.decode([ "uint", "string" ], data); // [ // { BigNumber: "1234" }, // 'Hello World' // ] // 对数组的类型进行解码 data = "0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000004d2000000000000000000000000000000000000000000000000000000000000162e000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000"; abiCoder.decode([ "uint[]", "string" ], data); // [ // [ // { BigNumber: "1234" }, // { BigNumber: "5678" } // ], // 'Hello World' // ] // 对复杂的结构体进行解码; 参数如果未命名,只允许按照位置(positional)方式去访问值(values) data = "0x00000000000000000000000000000000000000000000000000000000000004d20000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000162e0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000"; abiCoder.decode([ "uint", "tuple(uint256, string)" ], data); // [ // { BigNumber: "1234" }, // [ // { BigNumber: "5678" }, // 'Hello World' // ] // ] // 对复杂的结构体进行解码; 参数如果已命名,可通过位置(positional)方式和关键词(keyword)方式去访问值(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' // ] // ]