想在web3j实现 合约的abi.encode() 方法,下面是简单实例,如何组装结构体嵌套结构体列表的问题?
目的:得到与 solidity端, abi.encode(struct detail),同样的值。
import org.web3j.abi.DefaultFunctionEncoder;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import java.util.ArrayList;
import java.util.List;
/**
* 结构A
*/
public class A {
private String address;
private Long tokenId;
}
/**
* 结构体B
*/
public class B {
private String owner;
private List<A> aList;
private Long data;
}
/**
* abi.encode
*/
public class Encode() {
public void main() {
A a = new A();
B b = new B();
List<Type> typeList = new ArrayList<>();
Type<Address> address = new Address(b.getOwner());
typeList.add(address);
/**
* List<A>
* 如何组装嵌套结构体?
*/
List<List<Type>> structList = new ArrayList<>();
for (B b : bList) {
List<Type> bTtypeList = new ArrayList<>();
bTtypeList.add(new Address(b.getAddress()));
bTtypeList.add(new Uint256(b.getTokenId()));
structList.add(bTtypeList);
}
typeList.add(structList); // error : java.util.ArrayList cannot be cast to org.web3j.abi.datatypes.Type
Type<Uint> uint = new Uint256(b.getdata());
typeList.add(uint);
DefaultFunctionEncoder encoder = new DefaultFunctionEncoder();
String abiStr = encoder.encodeParameters(typeList);
}
}
执行到:typeList.add(structList); error : java.util.ArrayList cannot be cast to org.web3j.abi.datatypes.Type
求指点,感谢!