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);

    }

}
相关推荐
pianmian12 小时前
类(JavaBean类)和对象
java
我叫小白菜3 小时前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
Albert Edison3 小时前
【最新版】IntelliJ IDEA 2025 创建 SpringBoot 项目
java·spring boot·intellij-idea
超级小忍4 小时前
JVM 中的垃圾回收算法及垃圾回收器详解
java·jvm
weixin_446122464 小时前
JAVA内存区域划分
java·开发语言·redis
勤奋的小王同学~4 小时前
(javaEE初阶)计算机是如何组成的:CPU基本工作流程 CPU介绍 CPU执行指令的流程 寄存器 程序 进程 进程控制块 线程 线程的执行
java·java-ee
TT哇4 小时前
JavaEE==网站开发
java·redis·java-ee
2401_826097624 小时前
JavaEE-Linux环境部署
java·linux·java-ee
缘来是庄5 小时前
设计模式之访问者模式
java·设计模式·访问者模式
Bug退退退1236 小时前
RabbitMQ 高级特性之死信队列
java·分布式·spring·rabbitmq