#abi #ethereum #solidity #solana #anchor #smart-contracts #event-log

无 std ethabi-solana

简单易用的以太坊合约调用到字节码的转换工具,也可用于 Solana 程序

1 个稳定版本

1.0.0 2024 年 3 月 1 日

#1707 in 神奇豆

Apache-2.0

225KB
6K SLoC

此项目不再积极维护。考虑以下替代方案

ethabi

ABI(应用二进制接口)基本上是你在合约中调用函数并获取数据的方式。

ABI 确定了诸如如何调用函数以及信息应从程序组件传递到下一个组件的哪种二进制格式等细节...

以太坊智能合约是字节码,EVM,在以太坊区块链上。在 EVM 中,一个合约中可能有多个函数。ABI 是必要的,这样你就可以指定调用合约中的哪个函数,以及确保该函数将以你期望的格式返回数据。 阅读更多

此库编码函数调用并解码其输出。

文档

免责声明

此库旨在仅支持由最新 Solidity 版本生成的有效 ABI。具体来说,我们不打算支持由于已知 Solidity 错误或未严格遵循规范的第三方库而无效的 ABI。在使用 ethabi 之前,请确保预处理您的 ABI,以防它不受支持。

安装

cargo install ethabi-cli

用法

Ethereum ABI coder.
  Copyright 2016-2017 Parity Technologies (UK) Limited

Usage:
    ethabi encode function <abi-path> <function-name-or-signature> [-p <param>]... [-l | --lenient]
    ethabi encode params [-v <type> <param>]... [-l | --lenient]
    ethabi decode function <abi-path> <function-name-or-signature> <data>
    ethabi decode params [-t <type>]... <data>
    ethabi decode log <abi-path> <event-name-or-signature> [-l <topic>]... <data>
    ethabi -h | --help

Options:
    -h, --help         Display this message and exit.
    -l, --lenient      Allow short representation of input params.

Commands:
    encode             Encode ABI call.
    decode             Decode ABI call result.
    function           Load function from json ABI file.
    params             Specify types of input params inline.
    log                Decode event log.

示例

ethabi encode params -v bool 1

0000000000000000000000000000000000000000000000000000000000000001

--

ethabi encode params -v bool 1 -v string gavofyork -v bool 0

00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000096761766f66796f726b0000000000000000000000000000000000000000000000

--

ethabi encode params -v bool[] [1,0,false]

00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

--

ethabi encode params -v '(string,bool,string)' '(test,1,cyborg)'

00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000004746573740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000066379626f72670000000000000000000000000000000000000000000000000000

--

ethabi encode function examples/test.json foo -p 1
[{
	"type":"function",
	"inputs": [{
		"name":"a",
		"type":"bool"
	}],
	"name":"foo",
	"outputs": []
}]

455575780000000000000000000000000000000000000000000000000000000000000001

--

ethabi encode function examples/test.json foo(bool) -p 1
[{
	"type":"function",
	"inputs": [{
		"name":"a",
		"type":"bool"
	}],
	"name":"foo",
	"outputs": []
}]

455575780000000000000000000000000000000000000000000000000000000000000001

--

ethabi encode function examples/test.json bar(bool) -p 1
[{
	"type":"function",
	"inputs": [{
		"name":"a",
		"type":"bool"
	}],
	"name":"foo",
	"outputs": []
}]

6fae94120000000000000000000000000000000000000000000000000000000000000001

--

ethabi encode function examples/test.json bar(string):(uint256) -p 1
[{
	"type":"function",
	"inputs": [{
		"type":"string"
	}],
	"name":"foo",
	"outputs": [{
		"type": "uint256"
	}]
}]

d473a8ed000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000013100000000000000000000000000000000000000000000000000000000000000

--

ethabi decode params -t bool 0000000000000000000000000000000000000000000000000000000000000001

布尔值 真值

--

ethabi decode params -t bool -t string -t bool 00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000096761766f66796f726b0000000000000000000000000000000000000000000000

布尔值 真值
字符串 gavofyork
布尔值 假值

--

ethabi decode params -t bool[] 00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

布尔数组 [true,false,false]

--

ethabi decode params -t '(string,bool,string)' 00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000673706972616c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000067175617361720000000000000000000000000000000000000000000000000000```

(字符串,布尔值,字符串) (spiral,true,quasar)

--

ethabi decode function ./examples/foo.json bar 0000000000000000000000000000000000000000000000000000000000000001
[{
	"constant":false,
	"inputs":[{
		"name":"hello",
		"type":"address"
	}],
	"name":"bar",
	"outputs":[{
		"name":"",
		"type":"bool"
	}],
	"type":"function"
}]

布尔值 真值

--

ethabi decode log ./examples/event.json Event -l 0000000000000000000000000000000000000000000000000000000000000001 0000000000000000000000004444444444444444444444444444444444444444

a 布尔值 真值
b 地址 4444444444444444444444444444444444444444

依赖关系

~1.4–3MB
~42K SLoC