youchain-you-abi

这个包用来编码或解码 ABI 接口参数信息。


const AbiCoder = require('youchain-you-abi');
const abiCoder = new AbiCoder();
// 或者
const YOUChain = require('youchain');
const youchain = new YOUChain(YOUChain.giveProvider)
// 用法 -> youchain.you.abi

encodeFunctionSignature

描述:编码函数签名

参数值:functionName - String | Object 需编码的方法名称

返回值:String:ABI 签名后的方法

youchain.you.abi.encodeFunctionSignature(functionName);

示例:

// From a JSON interface object
youchain.you.abi.encodeFunctionSignature({
    name: 'myMethod',
    type: 'function',
    inputs: [{
        type: 'uint256',
        name: 'myNumber'
    },{
        type: 'string',
        name: 'myString'
    }]    
})

> 0x24ee0097
// Or string
youchain.you.abi.encodeFunctionSignature('myMethod(uint256,string)')
> '0x24ee0097'

encodeEventSignature

描述:编码事件签名

参数值:eventName - String | Object:需编码的时间名称

返回值:String ABI 签名后的事件

youchain.you.abi.encodeEventSignature(eventName);

示例:

youchain.you.abi.encodeEventSignature('myEvent(uint256,bytes32)')
> 0xf2eeb729e636a8cb783be044acf6b7b1e2c5863735b60d6daae84c366ee87d97
// or from a json interface object
    youchain.you.abi.encodeEventSignature({
    name: 'myEvent',
    type: 'event',
    inputs: [{
        type: 'uint256',
        name: 'myNumber'
    },{
        type: 'bytes32',
        name: 'myBytes'
    }]
})
> 0xf2eeb729e636a8cb783be044acf6b7b1e2c5863735b60d6daae84c366ee87d97

encodeParameter

描述:编码一个参数

参数值:

  • type - String | Object :JSON Interface 类型数组
  • parameter - Mixed: 需编码的参数

返回值:String 编码后的 ABI 参数

youchain.you.abi.encodeParameters(type, parameter);

示例:

youchain.you.abi.encodeParameter('uint256', '2345675643');
> "0x000000000000000000000000000000000000000000000000000000008bd02b7b"

youchain.you.abi.encodeParameter('uint256', '2345675643');
> "0x000000000000000000000000000000000000000000000000000000008bd02b7b"

youchain.you.abi.encodeParameter('bytes32', '0xdf3234');
> "0xdf32340000000000000000000000000000000000000000000000000000000000"

youchain.you.abi.encodeParameter('bytes32[]', ['0xdf3234', '0xfdfd']);
> "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002df32340000000000000000000000000000000000000000000000000000000000fdfd000000000000000000000000000000000000000000000000000000000000"

encodeParameters

描述:编码多个参数

参数值:

  • typesArray - Array|Object:JSON Interface 类型数组
  • parameters - Array: 需编码的参数

返回值:String 编码后的 ABI 参数

youchain.you.abi.encodeParameters(typesArray, parameters);

示例:

youchain.you.abi.encodeParameters(['uint256','string'], ['2345675643', 'Hello!%']);
> "0x000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000"
youchain.you.abi.encodeParameters(['uint8[]','bytes32'], [['34','434'], '0x324567fff']);

> "0x0"

encodeFunctionCall

描述:编码函数调用

参数值:

  • jsonInterface - Object:函数的 JSON Interface 对象
  • parameters - Array:编码参数

返回值:String - 编码后的 ABI 函数调用,即方法签名和参数

示例:

youchain.you.abi.encodeFunctionCall({
    name: 'myMethod',
    type: 'function',
    inputs: [{
        type: 'uint256',
        name: 'myNumber'
    },{
        type: 'string',
        name: 'myString'
    }]
}, ['2345675643', 'Hello!%']);
> "0x24ee0097000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000"

decodeParameter

描述:解码一个参数

参数值:

  • type - String | Object
  • hexString - String:需解码的 ABI 字节码

返回值:Mixed

youchain.you.abi.decodeParameter(type, hexString);

示例:

youchain.you.abi.decodeParameter('uint256', '0x0000000000000000000000000000000000000000000000000000000000000010');
> "16"
youchain.you.abi.decodeParameter('string', '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000');
> "Hello!%!"
youchain.you.abi.decodeParameter('string', '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000');
> "Hello!%!"

decodeParameters

描述:解码多个参数

参数值:

  • typesArray - Array|Object:类型数组或 JSON Interface 输出数组
  • hexString - String:需解码的 ABI 字节码

返回值:Object 解码后的参数

youchain.you.abi.decodeParameters(typesArray, hexString);

示例:

youchain.you.abi.decodeParameters(['string', 'uint256'], '0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000');
> Result { 
    '0': 'Hello!%!', 
    '1': '234' 
}
youchain.you.abi.decodeParameters([{
    type: 'string',
    name: 'myString'
    },{    
        type: 'uint256',
        name: 'myNumber'
    }], '0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000');
> Result {
    '0': 'Hello!%!',
    '1': '234',
    myString: 'Hello!%!',
    myNumber: '234'
}

decodeLog

描述:解码日志

参数值:

  • input - Array:JSON Interface 数组,可参考 Solidity 文档查看类型
  • hexString - String:ABI 字节码中的 data 字段
  • topics - Array:带有索引参数的日志数组

返回值:Object 返回解码参数

youchain.you.abi.decodeLog(inputs, hexString, topics);

示例:

youchain.you.abi.decodeLog([{
    type: 'string',
    name: 'myString'
    },{
        type: 'uint256',
        name: 'myNumber',
        indexed: true
    },{
    type: 'uint8',
    name: 'mySmallNumber',
    indexed: true
    }],
'0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000748656c6c6f252100000000000000000000000000000000000000000000000000',['0x000000000000000000000000000000000000000000000000000000000000f310', '0x0000000000000000000000000000000000000000000000000000000000000010']);
> Result {
    '0': 'Hello%!',
    '1': '62224',
    '2': '16',
    myString: 'Hello%!',
    myNumber: '62224',
    mySmallNumber: '16'
}