Skip to content

Commit

Permalink
1. Renamed ContractResolver to EnsResolver.
Browse files Browse the repository at this point in the history
2. Added ENS support to command line tools transfer command.
  • Loading branch information
conor10 committed Nov 10, 2017
1 parent ab43a6e commit 1b07e85
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 21 deletions.
1 change: 1 addition & 0 deletions console/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ description 'web3j command line tools'
dependencies {
compile project(':codegen'),
project(':infura')
runtime "org.slf4j:slf4j-nop:$slf4jVersion" // prevent logging of the library to the console
testCompile project(path: ':crypto', configuration: 'archives'),
project(path: ':core', configuration: 'archives')
}
Expand Down
4 changes: 3 additions & 1 deletion console/src/main/java/org/web3j/console/WalletSendFunds.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.ens.EnsResolver;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
Expand Down Expand Up @@ -39,7 +40,8 @@ private void run(String walletFileLocation, String destinationAddress) {
Credentials credentials = getCredentials(walletFile);
console.printf("Wallet for address " + credentials.getAddress() + " loaded\n");

if (!WalletUtils.isValidAddress(destinationAddress)) {
if (!WalletUtils.isValidAddress(destinationAddress)
&& !EnsResolver.isValidEnsName(destinationAddress)) {
exitError("Invalid destination address specified");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
/**
* Resolution logic for contract addresses.
*/
public class ContractResolver {
public class EnsResolver {

static final long DEFAULT_SYNC_THRESHOLD = 1000 * 60 * 3;

private final Web3j web3j;
private final TransactionManager transactionManager;
private final long syncThreshold;

public ContractResolver(Web3j web3j, long syncThreshold) {
public EnsResolver(Web3j web3j, long syncThreshold) {
this.web3j = web3j;
transactionManager = new ClientTransactionManager(web3j, null); // don't use empty string
this.syncThreshold = syncThreshold;
}

public ContractResolver(Web3j web3j) {
public EnsResolver(Web3j web3j) {
this(web3j, DEFAULT_SYNC_THRESHOLD);
}

Expand Down Expand Up @@ -89,7 +89,7 @@ boolean isSynced() throws Exception {
}

public static boolean isValidEnsName(String input) {
return input != null
return input != null // will be set to null on new Contract creation
&& (input.contains(".") || !WalletUtils.isValidAddress(input));
}
}
5 changes: 1 addition & 4 deletions core/src/main/java/org/web3j/tx/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.crypto.Credentials;
import org.web3j.ens.ContractResolver;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.RemoteCall;
Expand Down Expand Up @@ -49,11 +48,9 @@ protected Contract(String contractBinary, String contractAddress,
BigInteger gasPrice, BigInteger gasLimit) {
super(web3j, transactionManager);

ContractResolver contractResolver = new ContractResolver(web3j);
this.contractAddress = contractResolver.resolve(contractAddress);
this.contractAddress = ensResolver.resolve(contractAddress);

this.contractBinary = contractBinary;
this.contractAddress = contractAddress;
this.gasPrice = gasPrice;
this.gasLimit = gasLimit;
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/org/web3j/tx/ManagedTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.math.BigInteger;

import org.web3j.ens.EnsResolver;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
Expand All @@ -20,9 +21,12 @@ public abstract class ManagedTransaction {

protected TransactionManager transactionManager;

protected EnsResolver ensResolver;

protected ManagedTransaction(Web3j web3j, TransactionManager transactionManager) {
this.transactionManager = transactionManager;
this.web3j = web3j;
this.ensResolver = new EnsResolver(web3j);
}

public BigInteger getGasPrice() throws IOException {
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/web3j/tx/Transfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ private TransactionReceipt send(
+ " = " + weiValue + " Wei");
}

return send(toAddress, "", weiValue.toBigIntegerExact(), gasPrice, gasLimit);
String resolvedAddress = ensResolver.resolve(toAddress);
return send(resolvedAddress, "", weiValue.toBigIntegerExact(), gasPrice, gasLimit);
}

public static RemoteCall<TransactionReceipt> sendFunds(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.web3j.ens.ContractResolver.DEFAULT_SYNC_THRESHOLD;
import static org.web3j.ens.ContractResolver.isValidEnsName;
import static org.web3j.ens.EnsResolver.DEFAULT_SYNC_THRESHOLD;
import static org.web3j.ens.EnsResolver.isValidEnsName;

public class ContractResolverTest {
public class EnsResolverTest {

private Web3j web3j;
private Web3jService web3jService;
private ContractResolver contractResolver;
private EnsResolver ensResolver;

@Before
public void setUp() {
web3jService = mock(Web3jService.class);
web3j = Web3j.build(web3jService);
contractResolver = new ContractResolver(web3j);
ensResolver = new EnsResolver(web3j);
}

@Test
Expand All @@ -63,31 +63,31 @@ public void testLookupAddress() throws Exception {
when(web3jService.send(any(Request.class), eq(EthCall.class)))
.thenReturn(contractAddressResponse);

assertThat(contractResolver.lookupAddress("web3j.eth"),
assertThat(ensResolver.lookupAddress("web3j.eth"),
is("0x19e03255f667bdfd50a32722df860b1eeaf4d635"));
}

@Test
public void testIsSyncedSyncing() throws Exception {
configureSyncing(true);

assertFalse(contractResolver.isSynced());
assertFalse(ensResolver.isSynced());
}

@Test
public void testIsSyncedFullySynced() throws Exception {
configureSyncing(false);
configureLatestBlock(System.currentTimeMillis() / 1000); // block timestamp is in seconds

assertTrue(contractResolver.isSynced());
assertTrue(ensResolver.isSynced());
}

@Test
public void testIsSyncedBelowThreshold() throws Exception {
configureSyncing(false);
configureLatestBlock((System.currentTimeMillis() / 1000) - DEFAULT_SYNC_THRESHOLD);

assertFalse(contractResolver.isSynced());
assertFalse(ensResolver.isSynced());
}

private void configureSyncing(boolean isSynced) throws IOException {
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/src/test/java/org/web3j/ens/EnsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class EnsIT {
public void testEns() throws Exception {

Web3j web3j = Web3j.build(new HttpService());
ContractResolver contractResolver = new ContractResolver(web3j);
EnsResolver ensResolver = new EnsResolver(web3j);

assertThat(contractResolver.resolve("web3j.test"),
assertThat(ensResolver.resolve("web3j.test"),
is("0x19e03255f667bdfd50a32722df860b1eeaf4d635"));
}
}

0 comments on commit 1b07e85

Please sign in to comment.