Skip to content

Commit

Permalink
Support payable functions in wrapper generator
Browse files Browse the repository at this point in the history
  • Loading branch information
jorpic committed Jul 19, 2017
1 parent adab270 commit f1dca13
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class SolidityFunctionWrapper {
private static final String GAS_LIMIT = "gasLimit";
private static final String START_BLOCK = "startBlock";
private static final String END_BLOCK = "endBlock";
private static final String WEI_VALUE = "weiValue";

private static final String CODEGEN_WARNING = "Auto generated code.<br>\n"
+ "<strong>Do not modify!</strong><br>\n"
Expand Down Expand Up @@ -355,6 +356,10 @@ private static void buildTransactionFunction(
MethodSpec.Builder methodBuilder,
String inputParams) throws ClassNotFoundException {

if (functionDefinition.isPayable()) {
methodBuilder.addParameter(BigInteger.class, WEI_VALUE);
}

String functionName = functionDefinition.getName();

methodBuilder.returns(ParameterizedTypeName.get(Future.class, TransactionReceipt.class));
Expand All @@ -364,7 +369,11 @@ private static void buildTransactionFunction(
Function.class, Function.class, functionName,
Arrays.class, Type.class, inputParams, Collections.class,
TypeReference.class);
methodBuilder.addStatement("return executeTransactionAsync(function)");
if (functionDefinition.isPayable()) {
methodBuilder.addStatement("return executeTransactionAsync(function, $N)", WEI_VALUE);
} else {
methodBuilder.addStatement("return executeTransactionAsync(function)");
}
}

static TypeSpec buildEventResponseObject(String className,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public AbiDefinition(boolean constant, List<NamedType> inputs, String name,
this.name = name;
this.outputs = outputs;
this.type = type;
this.payable = false;
this.payable = payable;
}

public boolean isConstant() {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/web3j/tx/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ protected CompletableFuture<TransactionReceipt> executeTransactionAsync(Function
return Async.run(() -> executeTransaction(function));
}

/**
* Execute the provided payable function as a transaction asynchronously.
*
* @param function to transact with
* @param weiValue in Wei to send in transaction
* @return {@link Future} containing executing transaction
*/
protected CompletableFuture<TransactionReceipt> executeTransactionAsync(
Function function, BigInteger weiValue) {
return Async.run(() -> executeTransaction(FunctionEncoder.encode(function), weiValue));
}

protected EventValues extractEventParameters(
Event event, Log log) {

Expand Down
26 changes: 26 additions & 0 deletions src/test/java/org/web3j/codegen/SolidityFunctionWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,32 @@ public void testBuildFunctionTransaction() throws Exception {
assertThat(methodSpec.toString(), is(expected));
}

@Test
public void testBuildPayabelFunctionTransaction() throws Exception {
AbiDefinition functionDefinition = new AbiDefinition(
false,
Arrays.asList(
new AbiDefinition.NamedType("param", "uint8")),
"functionName",
Collections.emptyList(),
"type",
true);

MethodSpec methodSpec = buildFunction(functionDefinition);

String expected = "public java.util.concurrent.Future<org.web3j.protocol.core.methods"
+ ".response.TransactionReceipt> functionName(org.web3j.abi.datatypes.generated"
+ ".Uint8 param, java.math.BigInteger weiValue) {\n"
+ " org.web3j.abi.datatypes.Function function = new org.web3j.abi.datatypes"
+ ".Function(\"functionName\", java.util.Arrays.<org.web3j.abi.datatypes"
+ ".Type>asList(param), java.util.Collections.<org.web3j.abi"
+ ".TypeReference<?>>emptyList());\n"
+ " return executeTransactionAsync(function, weiValue);\n"
+ "}\n";

assertThat(methodSpec.toString(), is(expected));
}

@Test
public void testBuildFunctionConstantSingleValueReturn() throws Exception {
AbiDefinition functionDefinition = new AbiDefinition(
Expand Down

0 comments on commit f1dca13

Please sign in to comment.