解析 ETH 区块数据交易input

这里使用了一个开源项目: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,可以自己实现下。

示例:

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值