solidity的struct对象,web3j java解析输出参数

solidity的struct对象,web3j java解析输出参数

  • 先决条件
solidity 复制代码
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

contract StructDemo {
    struct Student {
        uint256 id;
        string name;
    }

    // 初始化一个结构体
    Student public student;

    function initStudent5(Student memory _stu) public  {
        student = _stu;
    }
}
xml 复制代码
<dependency>
    <groupId>org.web3j</groupId>
    <artifactId>core</artifactId>
    <version>4.12.0</version>
</dependency>
  • 使用Java进行输出参数的解析
java 复制代码
package com.kevin;

import org.junit.Before;
import org.junit.Test;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.FunctionReturnDecoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthCall;
import org.web3j.protocol.http.HttpService;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class StructDemoTest2 {

    private Web3j web3jClint;
    private Long chainId = 5777L;
    @Before
    public void init(){
        web3jClint = Web3j.build(new HttpService("http://127.0.0.1:8545", true));
    }

    @Test
    public void getStudent() throws IOException, ExecutionException, InterruptedException {

        // 构建输入参数
        List<TypeReference<?>> outputParameters = new ArrayList<>();
        outputParameters.add(new TypeReference<Uint256>() {});
        outputParameters.add(new TypeReference<Utf8String>() {});


        Function newStu = new Function("student", Collections.emptyList(), outputParameters);
        String encode = FunctionEncoder.encode(newStu);

        String from = "0x148F0cF0d183027D3f35dF8553012bD980F99ae5";
        String contractAddress = "0x57BF94B4Cca86d6e83347F520Ba2cDfC4FA318F9";

        Transaction ethCallTransaction = Transaction.createEthCallTransaction(from, contractAddress, encode);

        CompletableFuture<EthCall> ethCallCompletableFuture = web3jClint.ethCall(ethCallTransaction, DefaultBlockParameterName.LATEST).sendAsync();
        EthCall ethCall = ethCallCompletableFuture.get();

        String input = ethCall.getValue();
        System.out.println(input);

        List<Type> decode = FunctionReturnDecoder.decode(ethCall.getValue(), newStu.getOutputParameters());

        Student stu = new Student(((BigInteger) decode.get(0).getValue()).longValue(), (String) decode.get(1).getValue());

        System.out.println("stu-->" + stu);

    }

}
相关推荐
蜗牛互联网4 小时前
Java HttpClient 调用 Gemini 图像理解与金额字段校验
java·开发语言·人工智能·后端·python
开开心心就好4 小时前
视频里的图片怎么提取?双击一下就导出
java·前端·人工智能·spring·智能手机·intellij-idea·excel
蜗牛互联网4 小时前
Python Responses API视觉输入与本地金额校验最小实现
java·开发语言·人工智能·后端·python
程序猿_极客4 小时前
【免费】分享一套优质的基于Java的智慧养老院管理系统的设计与实现(带可视化),源码+文档+视频详解(讲解)
java·spring boot·后端·养老院管理系统
ly76894 小时前
从 TF-IDF 到 BM25:Elasticsearch 相关性评分的字段长度归一化与 boost 调参边界
java·elasticsearch·query dsl·bm25·相关性评分
程序猿_极客4 小时前
【免费】分享一套优质的基于SpringBoot的旅游管理系统的设计与实现(带智能推荐算法功能),源码+文档+视频详解(讲解)
java·spring boot·课程设计·旅游管理系统·计算机专业项目
Wang's Blog4 小时前
Java 中间件之 RabbitMQ 快速入门: RabbitMQ 介绍与安装部署
java·中间件·java-rabbitmq
sibylyue5 小时前
SQLite 运行模式
java·jvm·sqlite
专业程序开发源5 小时前
django便利店外卖后台管理系统19650-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·django·课程设计
JAVA面经实录9175 小时前
Java高级后端 · 架构组件开发 + CodeReview(本岗位差异化核心)
java·架构·代码复审