Web3j 继承StaticStruct的类所有属性必须为Public <DynamicArray<StaticStruct>>

Web3j 继承StaticStruct的类所有属性必须为Public,属性的顺序和数量必须和solidity 里面的struct 属性相同,否则属性少了或者多了的时候会出现错位

Web3j 继承StaticStruct的类所有属性不能为private,因为web3j 是通过长度去截取返回值解析成对应的属性进行赋值的。要获取一个list对象时,web3j是按一个类的所有public属性个数去截取总长度的,再进行解析赋值到没一个属性里

StaticStruct类

复制代码
import lombok.Data;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.StaticStruct;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;

@Data
public class ChildStaticStruct extends StaticStruct {

    public Uint256 attr1;
    public Uint256 attr2;
    public Address attr3;

    public ChildStaticStruct(Uint256 attr1, Uint256 attr2, Address attr3) {
         super(new Type[]{attr1,attr2,attr3});
    }
}

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.FunctionReturnDecoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.DynamicArray;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
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 java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class TestContract {
    private static final Logger logger = LoggerFactory.getLogger(TestContract.class);
    private String address;
    private Web3j web3j;
    public TestContract(String address, Web3j web3j) {
        this.address = address;
        this.web3j = web3j;
    }

    public List<ChildStaticStruct> getStaticStructList(String address) throws IOException {
        List<Type> inputParameters = Arrays.asList( new Address(address));
        List<TypeReference<?>> outputParameters = Arrays.asList(new TypeReference<DynamicArray<ChildStaticStruct>>(){});
        Function function = new Function("getStaticStructList",inputParameters,outputParameters);
        Transaction transaction = Transaction.createEthCallTransaction(null,address, FunctionEncoder.encode(function));
        EthCall response = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send();
        List<Type> output = FunctionReturnDecoder.decode(response.getValue(),function.getOutputParameters());
        List<ChildStaticStruct> results = (List<ChildStaticStruct>)output.get(0).getValue();
        return results;
    }
}

web3j的TypeDecoder 里的decodeArrayElements 验证了是否为StructType子类,

在currOffset += getSingleElementLength(input, currOffset, cls) * 64)判断了截取的长度

复制代码
if (StructType.class.isAssignableFrom(cls)) {
                elements = new ArrayList(length);
                currOffset = 0;

                for(currOffset = offset; currOffset < length; currOffset += getSingleElementLength(input, currOffset, cls) * 64) {
                    if (DynamicStruct.class.isAssignableFrom(cls)) {
                        value = decodeDynamicStruct(input, offset + DefaultFunctionReturnDecoder.getDataOffset(input, currOffset, typeReference), TypeReference.create(cls));
                    } else {
                        value = decodeStaticStruct(input, currOffset, TypeReference.create(cls));
                    }

                    elements.add(value);
                    ++currOffset;
                }

                String typeName = Utils.getSimpleTypeName(cls);
                return (Type)consumer.apply(elements, typeName);
            } 

getSingleElementLength 验证了截取长度是根据public属性数量去截取Utils.staticStructNestedPublicFieldsFlatList(type).size(),pirvate计算长度

复制代码
static <T extends Type> int getSingleElementLength(String input, int offset, Class<T> type) {
        if (input.length() == offset) {
            return 0;
        } else if (!DynamicBytes.class.isAssignableFrom(type) && !Utf8String.class.isAssignableFrom(type)) {
            return StaticStruct.class.isAssignableFrom(type) ? Utils.staticStructNestedPublicFieldsFlatList(type).size() : 1;
        } else {
            return decodeUintAsInt(input, offset) / 32 + 2;
        }
    }

staticStructNestedPublicFieldsFlatList方面里面通过Modifier.isPublic(field.getModifiers())过滤了public

复制代码
public static List<Field> staticStructNestedPublicFieldsFlatList(Class<Type> classType) {
        return (List)staticStructsNestedFieldsFlatList(classType).stream().filter((field) -> {
            return Modifier.isPublic(field.getModifiers());
        }).collect(Collectors.toList());
    }

Modifier 里面比较了Public

复制代码
public static boolean isPublic(int mod) {
        return (mod & PUBLIC) != 0;
    }
相关推荐
web4x11 小时前
Web4X:站在Web4.0时代的起点,定义AI商业新生态
web3·aigc
电报号dapp1192 天前
区块链钱包开发全解析:从架构设计到安全生态构建
安全·web3·去中心化·区块链·智能合约
猫头虎2 天前
5G-A来了!5G信号多个A带来哪些改变?
5g·机器人·web3·aigc·社交电子·能源·量子计算
忆林5203 天前
关于web3
web3
OneBlock Community3 天前
Xcavate 上线 Polkadot |开启 Web3 房地产投资新时代
web3
战族狼魂3 天前
Web3 初学者的第一个实战项目:留言上链 DApp
web3·区块链
ClonBrowser4 天前
Facebook的元宇宙新次元:社交互动如何改变?
web3·互联网·facebook·tiktok·instagram·指纹浏览器·clonbrowser
Sui_Network4 天前
Sui Basecamp 2025 全栈出击
大数据·游戏·web3·去中心化·区块链
电报号dapp1195 天前
DeFi开发系统软件开发:技术架构与生态重构
重构·架构·web3·去中心化·区块链·智能合约
电报号dapp1196 天前
公链钱包开发:技术逻辑与产品设计实践
web3·去中心化·区块链·智能合约