这里使用了一个开源项目:https://github.com/rvullriede/evm-abi-decoder
在pom中添加依赖即可:
<dependency>
<groupId>net.osslabz.evm</groupId>
<artifactId>evm-abi-decoder</artifactId>
<version>0.0.6</version>
</dependency>
这个jar需要jdk11,想使用jdk8的同学可以把项目中net.osslabz.evm.abi.definition.AbiDefinition#fromJson(java.lang.String) 的入参 Files.readString(Path.of(abiFilePath)) 改一下,这个Files.readString(Path.of(abiFilePath)) 方法只有jdk11有,是读取文件中所有内容为一个string,可以自己实现下。
示例:文章来源:https://www.toymoban.com/news/detail-836015.html
package com.mathieu.blockchain;
import com.alibaba.fastjson.JSONObject;
import net.osslabz.evm.abi.decoder.AbiDecoder;
import net.osslabz.evm.abi.decoder.DecodedFunctionCall;
import java.io.IOException;
/**
* 解析不同的input需要使用其对应的abi
* abi查询网址 : https://www.smartcontracttoolkit.com/abi
* <p>
* 一条input对应的api类型可以通过txn的token去查询tokenMeta获取,tokenMeta中的 tokenType就是abi类型
* https://docs.nodereal.io/reference/nr_gettokenmeta#example
* <p>
* curl https://bsc-mainnet.nodereal.io/v1/your-api-key \
* -X POST \
* -H "Content-Type: application/json" \
* -d '{"jsonrpc":"2.0","method":"nr_getTokenMeta","params":["0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d"],"id": 0 }'
* <p>
* {
* "id": "0",
* "jsonrpc": "2.0",
* "result": {
* "name": "USD Coin",
* "symbol": "USDC",
* "decimails": 18,
* "tokenType": "erc20"
* }
* }
*
* @author wangxianghu
*/
public class AbiDecodeDemo {
public static void main(String[] args) throws IOException {
AbiDecoder abiDecoder = new AbiDecoder("abiFiles/ERC20.json");
String input = "0xa9059cbb0000000000000000000000006eba81f430ff5ab8daee861832c015701db87b2400000000000000000000000000000000000000000000001ffbe5120bc8780000";
// 解析input
DecodedFunctionCall decodedFunctionCall = abiDecoder.decodeFunctionCall(input);
System.out.println(JSONObject.toJSONString(decodedFunctionCall.getParams()));
// 结果
// [{"name":"_to","type":"address","value":"0x6eba81f430ff5ab8daee861832c015701db87b24"},{"name":"_value","type":"uint256","value":590000000000000000000}]
}
}
:https://github.com/wangxianghu/learn-blockchain/blob/main/src/main/java/com/mathieu/blockchain/AbiDecodeDemo.java文章来源地址https://www.toymoban.com/news/detail-836015.html
到了这里,关于解析 ETH 区块数据交易input的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!