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

    }

}
相关推荐
无心水14 分钟前
【OpenClaw:赚钱】案例19、内容产量5倍、广告收入翻4倍:播客转多平台内容矩阵全自动化实战(OpenAI Whisper + Claude)
java·人工智能·python·ai编程·openclaw·养龙虾·java.time
云烟成雨TD23 分钟前
Spring AI 1.x 系列【42】MCP 服务端 Spring Boot 启动器
java·人工智能·spring
云烟成雨TD28 分钟前
Spring AI 1.x 系列【38】模型上下文协议(MCP)
java·人工智能·spring
Alson_Code30 分钟前
Spring AI-1.1.0
java·人工智能·后端·spring·ai编程
ANnianStriver31 分钟前
PetLumina 08 — 通知系统与搜索功能修复(广播机制 + 已读状态 + 参数对齐)
java·ai·ai编程·广播机制
ggaofeng33 分钟前
试用zeroclaw
java·开发语言
就叫_这个吧37 分钟前
servlet整合tomcat项目启动报错解决,org.apache.tomcat.util.descriptor.web.WebXml.setVersion
java·servlet·tomcat·apache
Wenzar_38 分钟前
用 JAX 构建可微分光子神经网络仿真器
java·人工智能·深度学习·神经网络
cfm_29141 小时前
RocksDB 初步了解
java
凡人叶枫1 小时前
Effective C++ 条款02:宁可以编译器替换预处理器
java·linux·c语言·开发语言·c++