Skip to content

Commit

Permalink
Merge pull request hyperledger-web3j#239 from eepstein/master
Browse files Browse the repository at this point in the history
make generated classes extendable
  • Loading branch information
conor10 authored Nov 27, 2017
2 parents cf2448e + a22fdb6 commit 4078cb3
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ private void addAddressesSupport(TypeSpec.Builder classBuilder, Map<String, Stri
.build();
classBuilder.addMethod(getAddress);

MethodSpec getPreviousAddress = MethodSpec
.methodBuilder("getPreviouslyDeployedAddress")
.addModifiers(Modifier.PUBLIC)
.addModifiers(Modifier.STATIC)
.returns(stringType)
.addParameter(stringType, "networkId")
.addCode(
CodeBlock
.builder()
.addStatement("return _addresses.get(networkId)")
.build())
.build();
classBuilder.addMethod(getPreviousAddress);

}
}

Expand All @@ -156,7 +170,7 @@ private TypeSpec.Builder createClassBuilder(String className, String binary) {
String javadoc = CODEGEN_WARNING + getWeb3jVersion();

return TypeSpec.classBuilder(className)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addModifiers(Modifier.PUBLIC)
.addJavadoc(javadoc)
.superclass(Contract.class)
.addField(createBinaryDefinition(binary));
Expand Down Expand Up @@ -226,7 +240,7 @@ private List<MethodSpec> buildFunctionDefinitions(

private static MethodSpec buildConstructor(Class authType, String authName) {
return MethodSpec.constructorBuilder()
.addModifiers(Modifier.PRIVATE)
.addModifiers(Modifier.PROTECTED)
.addParameter(String.class, CONTRACT_ADDRESS)
.addParameter(Web3j.class, WEB3J)
.addParameter(authType, authName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ private void generate() throws IOException, ClassNotFoundException {
System.out.printf("Generating " + basePackageName + "." + className + " ... ");
Map<String, String> addresses;
if (c.networks != null && !c.networks.isEmpty()) {
addresses = c.networks.entrySet().stream().collect(Collectors.toMap(
Map.Entry::getKey, e -> e.getValue().getAddress()
));
addresses = c.networks.entrySet().stream()
.filter(e -> (e.getValue() != null && e.getValue().getAddress() != null))
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getAddress()
));
} else {
addresses = Collections.EMPTY_MAP;
}
Expand Down Expand Up @@ -235,7 +236,7 @@ public String getBytecode() {
}

public NetworkInfo getNetwork(String networkId) {
return networks.get(networkId);
return networks == null ? null : networks.get(networkId);
}

public String getAddress(String networkId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1422,8 +1422,7 @@
"events": {},
"links": {
"ConvertLib": "0xcfeb869f69431e42cdb54a4f4f105c19c080a601"
},
"address": "0x254dffcd3277c0b1660f6d42efbb754edababc2b"
}
}
},
"schemaVersion": "1.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public void cancel() {
protected abstract Optional<Request<?, EthLog>> getFilterLogs(BigInteger filterId);

void throwException(Response.Error error) {
throw new FilterException("Invalid request: " + error.getMessage());
throw new FilterException("Invalid request: "
+ (error == null ? "Unknown Error" : error.getMessage()));
}

void throwException(Throwable cause) {
Expand Down
19 changes: 12 additions & 7 deletions core/src/main/java/org/web3j/tx/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@
/**
* Solidity contract type abstraction for interacting with smart contracts via native Java types.
*/
@SuppressWarnings("WeakerAccess")
public abstract class Contract extends ManagedTransaction {

// https://www.reddit.com/r/ethereum/comments/5g8ia6/attention_miners_we_recommend_raising_gas_limit/
public static final BigInteger GAS_LIMIT = BigInteger.valueOf(4_300_000);

private final String contractBinary;
private String contractAddress;
private final BigInteger gasPrice;
private final BigInteger gasLimit;
private TransactionReceipt transactionReceipt;
private Map<String, String> deployedAddresses;
protected final String contractBinary;
protected String contractAddress;
protected BigInteger gasPrice;
protected BigInteger gasLimit;
protected TransactionReceipt transactionReceipt;
protected Map<String, String> deployedAddresses;

protected Contract(String contractBinary, String contractAddress,
Web3j web3j, TransactionManager transactionManager,
Expand Down Expand Up @@ -346,7 +347,7 @@ protected static <T extends Contract> RemoteCall<T> deployRemoteCall(
encodedConstructor, BigInteger.ZERO);
}

protected EventValues extractEventParameters(
public static EventValues staticExtractEventParameters(
Event event, Log log) {

List<String> topics = log.getTopics();
Expand All @@ -368,6 +369,10 @@ protected EventValues extractEventParameters(
return new EventValues(indexedValues, nonIndexedValues);
}

protected EventValues extractEventParameters(Event event, Log log) {
return staticExtractEventParameters(event, log);
}

protected List<EventValues> extractEventParameters(
Event event, TransactionReceipt transactionReceipt) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.web3j.contracts.token;

import java.math.BigInteger;
import java.util.List;

import rx.Observable;

import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

/**
* Describes the Ethereum "Basic" subset of the ERC-20 token standard.
* <p>
* Implementations should provide the concrete <code>TransferEventResponse</code>
* from their token as the generic type "T".
* </p>
*
* @see <a href="https://github.com/ethereum/EIPs/issues/179">ERC: Simpler Token Standard #179</a>
* @see <a href="https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/token/ERC20Basic.sol">OpenZeppelin's zeppelin-solidity reference implementation</a>
*/
@SuppressWarnings("unused")
public interface ERC20BasicInterface<T> {

RemoteCall<BigInteger> totalSupply();

RemoteCall<BigInteger> balanceOf(String who);

RemoteCall<TransactionReceipt> transfer(String to, BigInteger value);

List<T> getTransferEvents(TransactionReceipt transactionReceipt);

Observable<T> transferEventObservable(DefaultBlockParameter startBlock,
DefaultBlockParameter endBlock);

}
36 changes: 36 additions & 0 deletions core/src/test/java/org/web3j/contracts/token/ERC20Interface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.web3j.contracts.token;

import java.math.BigInteger;
import java.util.List;

import rx.Observable;

import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

/**
* The Ethereum ERC-20 token standard.
* <p>
* Implementations should provide the concrete <code>ApprovalEventResponse</code> and
* <code>TransferEventResponse</code> from their token as the generic types "R" amd "T".
* </p>
*
* @see <a href="https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md">EIPs/EIPS/eip-20-token-standard.md</a>
* @see <a href="https://github.com/ethereum/EIPs/issues/20">ERC: Token standard #20</a>
*/
@SuppressWarnings("unused")
public interface ERC20Interface<R, T> extends ERC20BasicInterface<T> {

RemoteCall<BigInteger> allowance(String owner, String spender);

RemoteCall<TransactionReceipt> approve(String spender, BigInteger value);

RemoteCall<TransactionReceipt> transferFrom(String from, String to, BigInteger value);

List<R> getApprovalEvents(TransactionReceipt transactionReceipt);

Observable<R> approvalEventObservable(DefaultBlockParameter startBlock,
DefaultBlockParameter endBlock);

}

0 comments on commit 4078cb3

Please sign in to comment.