Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for multidimensional arrays arguments in the contract wrapper generator #404

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Adding and fixing compilation test
  • Loading branch information
fergarrui committed Mar 11, 2018
commit 20a916c98c278b4fe20a1873b4b2cf0417e04892
21 changes: 20 additions & 1 deletion abi/src/main/java/org/web3j/abi/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ public static List<TypeReference<Type>> convert(List<TypeReference<?>> input) {
return result;
}

public static <T, R extends Type<T>, E extends Type<T>> List<E> typeMap(
List<List<T>> input,
Class<E> outerDestType,
Class<R> innerType) {
List<E> result = new ArrayList<>();
try {
Constructor<E> constructor = outerDestType.getDeclaredConstructor(List.class);
for (List<T> ts : input) {
E e = constructor.newInstance(typeMap(ts, innerType));
result.add(e);
}
} catch (NoSuchMethodException
| IllegalAccessException
| InstantiationException
| InvocationTargetException e) {
throw new TypeMappingException(e);
}
return result;
}

public static <T, R extends Type<T>> List<R> typeMap(List<T> input, Class<R> destType)
throws TypeMappingException {

Expand All @@ -118,7 +138,6 @@ public static <T, R extends Type<T>> List<R> typeMap(List<T> input, Class<R> des
throw new TypeMappingException(e);
}
}

return result;
}
}
14 changes: 14 additions & 0 deletions abi/src/test/java/org/web3j/abi/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.web3j.abi.datatypes.Uint;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Int64;
import org.web3j.abi.datatypes.generated.StaticArray2;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.abi.datatypes.generated.Uint64;

Expand Down Expand Up @@ -60,6 +61,19 @@ public void testTypeMap() throws Exception {
new Uint256(BigInteger.TEN))));
}

@Test
public void testTypeMapNested() {
List<BigInteger> innerList1 = Arrays.asList(BigInteger.valueOf(1), BigInteger.valueOf(2));
List<BigInteger> innerList2 = Arrays.asList(BigInteger.valueOf(3), BigInteger.valueOf(4));
final List<List<BigInteger>> input = Arrays.asList(innerList1, innerList2);

StaticArray2<Uint256> staticArray1 = new StaticArray2<>(new Uint256(1), new Uint256(2));
StaticArray2<Uint256> staticArray2 = new StaticArray2<>(new Uint256(3), new Uint256(4));
List<StaticArray2<Uint256>> expectedList = Arrays.asList(staticArray1, staticArray2);
assertThat(typeMap(input, StaticArray2.class, Uint256.class),
equalTo(expectedList));
}

@Test
public void testTypeMapEmpty() {
assertThat(typeMap(new ArrayList<BigInteger>(), Uint256.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,24 @@ private String createMappedParameterTypes(ParameterSpec parameterSpec) {
throw new UnsupportedOperationException(
"Only a single parameterized type is supported");
} else {
String parameterSpecType = parameterSpec.type.toString();
TypeName typeName = typeNames.get(0);
return "new " + parameterSpec.type + "(\n"
String typeMapInput = typeName + ".class";
if (typeName instanceof ParameterizedTypeName) {
List<TypeName> typeArguments = ((ParameterizedTypeName) typeName).typeArguments;
if (typeArguments.size() != 1) {
throw new UnsupportedOperationException(
"Only a single parameterized type is supported");
}
TypeName innerTypeName = typeArguments.get(0);
parameterSpecType = ((ParameterizedTypeName) parameterSpec.type)
.rawType.toString();
typeMapInput = ((ParameterizedTypeName) typeName).rawType + ".class, "
+ innerTypeName + ".class";
}
return "new " + parameterSpecType + "(\n"
+ " org.web3j.abi.Utils.typeMap("
+ parameterSpec.name + ", " + typeName + ".class)" + ")";
+ parameterSpec.name + ", " + typeMapInput + "))";
}
} else {
return "new " + parameterSpec.type + "(" + parameterSpec.name + ")";
Expand Down
26 changes: 26 additions & 0 deletions codegen/src/test/resources/solidity/arrays/Arrays.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ contract Arrays {
return result;
}

function multiDynamic(uint[2][] input) returns (uint[] result) {
uint length = input.length;
result = new uint[](length*2);
uint resultIndex = 0;
for (uint i = 0; i < length; i++) {
result[resultIndex] = (input[i][0]);
resultIndex++;
result[resultIndex] = (input[i][1]);
resultIndex++;
}
return result;
}

function multiFixed(uint[2][6] input) returns (uint[] result) {
uint length = input.length;
result = new uint[](length*2);
uint resultIndex = 0;
for (uint i = 0; i < length; i++) {
result[resultIndex] = (input[i][0]);
resultIndex++;
result[resultIndex] = (input[i][1]);
resultIndex++;
}
return result;
}

function returnArray() constant returns (address[]) {
return new address[](0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"constant":true,"inputs":[],"name":"returnArray","outputs":[{"name":"","type":"address[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"input","type":"uint256[10]"}],"name":"fixedReverse","outputs":[{"name":"result","type":"uint256[10]"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"input","type":"uint256[]"}],"name":"dynamicReverse","outputs":[{"name":"result","type":"uint256[]"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]
[{"constant":false,"inputs":[{"name":"input","type":"uint256[2][]"}],"name":"multiDynamic","outputs":[{"name":"result","type":"uint256[]"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"returnArray","outputs":[{"name":"","type":"address[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"input","type":"uint256[2][6]"}],"name":"multiFixed","outputs":[{"name":"result","type":"uint256[]"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"input","type":"uint256[10]"}],"name":"fixedReverse","outputs":[{"name":"result","type":"uint256[10]"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"input","type":"uint256[]"}],"name":"dynamicReverse","outputs":[{"name":"result","type":"uint256[]"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6060604052341561000f57600080fd5b6102da8061001e6000396000f3006060604052600436106100565763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416633cac14c8811461005b578063b96f54d1146100c1578063beda363b14610136575b600080fd5b341561006657600080fd5b61006e610185565b60405160208082528190810183818151815260200191508051906020019060200280838360005b838110156100ad578082015183820152602001610095565b505050509050019250505060405180910390f35b34156100cc57600080fd5b6100fd600461014481600a6101406040519081016040529190828261014080828437509395506101b2945050505050565b604051808261014080838360005b8381101561012357808201518382015260200161010b565b5050505090500191505060405180910390f35b341561014157600080fd5b61006e60046024813581810190830135806020818102016040519081016040528093929190818152602001838360200280828437509496506101fe95505050505050565b61018d610274565b600060405180591061019c5750595b9080825280602002602001820160405250905090565b6101ba610286565b600a60005b818110156101f7578360001982840301600a81106101d957fe5b60200201518382600a81106101ea57fe5b60200201526001016101bf565b5050919050565b610206610274565b600080835191508160405180591061021b5750595b90808252806020026020018201604052509250600090505b818110156101f757838160010183038151811061024c57fe5b9060200190602002015183828151811061026257fe5b60209081029091010152600101610233565b60206040519081016040526000815290565b610140604051908101604052600a815b600081526020019060019003908161029657905050905600a165627a7a72305820305df0b85cbf8cf9c0720830cf03914a3044a0459d83288859a24d3dd311ef840029
6060604052341561000f57600080fd5b61054c8061001e6000396000f30060606040526004361061006c5763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416633a69db9481146100715780633cac14c814610149578063a30cc5f61461015c578063b96f54d1146101d1578063beda363b14610246575b600080fd5b341561007c57600080fd5b6100f660046024813581810190830135806020818102016040519081016040528181529291906000602085015b828210156100e85760408083028601906002908051908101604052809291908260026020028082843750505091835250506001909101906020016100a9565b505050505091905050610295565b60405160208082528190810183818151815260200191508051906020019060200280838360005b8381101561013557808201518382015260200161011d565b505050509050019250505060405180910390f35b341561015457600080fd5b6100f6610352565b341561016757600080fd5b6100f6600461018481600660c060405190810160405291906000835b828210156101c45783826040020160028060200260405190810160405280929190826002602002808284375050509183525050600190910190602001610183565b505050509190505061037f565b34156101dc57600080fd5b61020d600461014481600a610140604051908101604052919082826101408082843750939550610424945050505050565b604051808261014080838360005b8381101561023357808201518382015260200161021b565b5050505090500191505060405180910390f35b341561025157600080fd5b6100f6600460248135818101908301358060208181020160405190810160405280939291908181526020018383602002808284375094965061047095505050505050565b61029d6104e6565b600080600084519250826002026040518059106102b75750595b9080825280602002602001820160405250935060009150600090505b8281101561034a578481815181106102e757fe5b90602001906020020151518483815181106102fe57fe5b6020908102909101015260019091019084818151811061031a57fe5b906020019060200201516020015184838151811061033457fe5b60209081029091010152600191820191016102d3565b505050919050565b61035a6104e6565b60006040518059106103695750595b9080825280602002602001820160405250905090565b6103876104e6565b6006600080600c60405180591061039b5750595b9080825280602002602001820160405250935060009150600090505b8281101561034a578481600681106103cb57fe5b6020020151518483815181106103dd57fe5b602090810290910101526001909101908481600681106103f957fe5b60200201516020015184838151811061040e57fe5b60209081029091010152600191820191016103b7565b61042c6104f8565b600a60005b81811015610469578360001982840301600a811061044b57fe5b60200201518382600a811061045c57fe5b6020020152600101610431565b5050919050565b6104786104e6565b600080835191508160405180591061048d5750595b90808252806020026020018201604052509250600090505b818110156104695783816001018303815181106104be57fe5b906020019060200201518382815181106104d457fe5b602090810290910101526001016104a5565b60206040519081016040526000815290565b610140604051908101604052600a815b600081526020019060019003908161050857905050905600a165627a7a72305820b21a1abcb40c83cc0cd89be11e3cb3330d461cc4cc9dd66f66907c136e2b5f180029