public String resolve(String contractId) { if (isValidEnsName(contractId)) { PublicResolver resolver = obtainPublicResolver(contractId); byte[] nameHash = NameHash.nameHashAsBytes(contractId); String contractAddress = null; try { contractAddress = resolver.addr(nameHash).send(); } catch (Exception e) { throw new RuntimeException("Unable to execute Ethereum request", e); } if (!WalletUtils.isValidAddress(contractAddress)) { throw new RuntimeException("Unable to resolve address for name: " + contractId); } else { return contractAddress; } } else { return contractId; } }
@Test public void testSimpleStorageContract() throws Exception { BigInteger value = BigInteger.valueOf(1000L); SimpleStorage simpleStorage = SimpleStorage.deploy( web3j, ALICE, GAS_PRICE, GAS_LIMIT).send(); assertNotNull(simpleStorage.set(value).send()); assertThat(simpleStorage.get().send(), is(value)); } }
void testErrorScenario() throws Throwable { try { contract.performTransaction( new Address(BigInteger.TEN), new Uint256(BigInteger.ONE)).send(); } catch (InterruptedException e) { throw e; } catch (ExecutionException e) { throw e.getCause(); } }
/** * Reverse name resolution as documented in the * <a href="https://docs.ens.domains/en/latest/userguide.html#reverse-name-resolution">specification</a>. * @param address an ethereum address, example: "0x314159265dd8dbb310642f98f50c066173c1259b" * @return a EnsName registered for provided address */ public String reverseResolve(String address) { if (WalletUtils.isValidAddress(address)) { String reverseName = Numeric.cleanHexPrefix(address) + REVERSE_NAME_SUFFIX; PublicResolver resolver = obtainPublicResolver(reverseName); byte[] nameHash = NameHash.nameHashAsBytes(reverseName); String name = null; try { name = resolver.name(nameHash).send(); } catch (Exception e) { throw new RuntimeException("Unable to execute Ethereum request", e); } if (!isValidEnsName(name)) { throw new RuntimeException("Unable to resolve name for address: " + address); } else { return name; } } else { throw new EnsResolutionException("Address is invalid: " + address); } }
@Test public void testTransfer() throws Exception { TransactionReceipt transactionReceipt = Transfer.sendFunds( web3j, ALICE, BOB.getAddress(), BigDecimal.valueOf(0.2), Convert.Unit.ETHER) .send(); assertFalse(transactionReceipt.getBlockHash().isEmpty()); } }
protected TransactionReceipt sendFunds(Credentials credentials, String toAddress, BigDecimal value, Convert.Unit unit) throws Exception { return new Transfer(web3j, getVerifiedTransactionManager(credentials)) .sendFunds(toAddress, value, unit).send(); } }
@Test public void testFibonacci() throws Exception { Fibonacci fibonacci = Fibonacci.load( "0x3c05b2564139fb55820b18b72e94b2178eaace7d", Web3j.build(new HttpService()), ALICE, STATIC_GAS_PROVIDER); BigInteger result = fibonacci.fibonacci(BigInteger.valueOf(10)).send(); assertThat(result, equalTo(BigInteger.valueOf(55))); }
@Test public void testCallMultipleValue() throws Exception { EthCall ethCall = new EthCall(); ethCall.setResult("0x0000000000000000000000000000000000000000000000000000000000000037" + "0000000000000000000000000000000000000000000000000000000000000007"); prepareCall(ethCall); assertThat(contract.callMultipleValue().send(), equalTo(Arrays.asList( new Uint256(BigInteger.valueOf(55)), new Uint256(BigInteger.valueOf(7))))); }
@Test public void testCallMultipleValueEmpty() throws Exception { EthCall ethCall = new EthCall(); ethCall.setResult("0x"); prepareCall(ethCall); assertThat(contract.callMultipleValue().send(), equalTo(emptyList())); }
@Test public void testFibonacciNotify() throws Exception { Fibonacci fibonacci = Fibonacci.load( "0x3c05b2564139fb55820b18b72e94b2178eaace7d", Web3j.build(new HttpService()), ALICE, STATIC_GAS_PROVIDER); TransactionReceipt transactionReceipt = fibonacci.fibonacciNotify( BigInteger.valueOf(15)).send(); Fibonacci.NotifyEventResponse result = fibonacci.getNotifyEvents(transactionReceipt).get(0); assertThat(result.input, equalTo(new Uint256(BigInteger.valueOf(15)))); assertThat(result.result, equalTo(new Uint256(BigInteger.valueOf(610)))); } }
private Contract deployContract(TransactionReceipt transactionReceipt) throws Exception { prepareTransaction(transactionReceipt); String encodedConstructor = FunctionEncoder.encodeConstructor( Arrays.<Type>asList(new Uint256(BigInteger.TEN))); return TestContract.deployRemoteCall( TestContract.class, web3j, getVerifiedTransactionManager(SampleKeys.CREDENTIALS), ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT, "0xcafed00d", encodedConstructor, BigInteger.ZERO).send(); }
PublicResolver lookupResolver(String ensName) throws Exception { NetVersion netVersion = web3j.netVersion().send(); String registryContract = Contracts.resolveRegistryContract(netVersion.getNetVersion()); ENS ensRegistry = ENS.load( registryContract, web3j, transactionManager, DefaultGasProvider.GAS_PRICE, DefaultGasProvider.GAS_LIMIT); byte[] nameHash = NameHash.nameHashAsBytes(ensName); String resolverAddress = ensRegistry.resolver(nameHash).send(); PublicResolver resolver = PublicResolver.load( resolverAddress, web3j, transactionManager, DefaultGasProvider.GAS_PRICE, DefaultGasProvider.GAS_LIMIT); return resolver; }
TransactionReceipt transactionReceipt = createTransaction(transfer, gasPrice).send(); pendingTransactions.put(transactionReceipt.getTransactionHash(), new Object());
@Test public void testCallSingleValue() throws Exception { // Example taken from FunctionReturnDecoderTest EthCall ethCall = new EthCall(); ethCall.setResult("0x0000000000000000000000000000000000000000000000000000000000000020" + "0000000000000000000000000000000000000000000000000000000000000000"); prepareCall(ethCall); assertThat(contract.callSingleValue().send(), equalTo(new Utf8String(""))); }
@Test(expected = RuntimeException.class) public void testDeployInvalidContractAddress() throws Throwable { TransactionReceipt transactionReceipt = new TransactionReceipt(); transactionReceipt.setTransactionHash(TRANSACTION_HASH); prepareTransaction(transactionReceipt); String encodedConstructor = FunctionEncoder.encodeConstructor( Arrays.<Type>asList(new Uint256(BigInteger.TEN))); try { TestContract.deployRemoteCall( TestContract.class, web3j, SampleKeys.CREDENTIALS, ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT, "0xcafed00d", encodedConstructor, BigInteger.ZERO).send(); } catch (InterruptedException e) { throw e; } catch (ExecutionException e) { throw e.getCause(); } }
@Test(expected = TxHashMismatchException.class) public void testTxHashMismatch() throws Exception { TransactionReceipt transactionReceipt = prepareTransfer(); prepareTransaction(transactionReceipt); TransactionManager transactionManager = new RawTransactionManager(web3j, SampleKeys.CREDENTIALS); Transfer transfer = new Transfer(web3j, transactionManager); transfer.sendFunds(ADDRESS, BigDecimal.ONE, Convert.Unit.ETHER).send(); } }
@Test public void testCallSingleValueEmpty() throws Exception { // Example taken from FunctionReturnDecoderTest EthCall ethCall = new EthCall(); ethCall.setResult("0x"); prepareCall(ethCall); assertNull(contract.callSingleValue().send()); }
@Test public void testTransaction() throws Exception { TransactionReceipt transactionReceipt = new TransactionReceipt(); transactionReceipt.setTransactionHash(TRANSACTION_HASH); transactionReceipt.setStatus("0x1"); prepareTransaction(transactionReceipt); assertThat(contract.performTransaction( new Address(BigInteger.TEN), new Uint256(BigInteger.ONE)).send(), is(transactionReceipt)); }
@Test public void testTransactionFailed() throws Exception { thrown.expect(TransactionException.class); thrown.expectMessage( "Transaction has failed with status: 0x0. Gas used: 1. (not-enough gas?)"); TransactionReceipt transactionReceipt = new TransactionReceipt(); transactionReceipt.setTransactionHash(TRANSACTION_HASH); transactionReceipt.setStatus("0x0"); transactionReceipt.setGasUsed("0x1"); prepareTransaction(transactionReceipt); contract.performTransaction( new Address(BigInteger.TEN), new Uint256(BigInteger.ONE)).send(); }
public TransactionResponse<ApprovalEventResponse> approve( List<String> privateFor, String contractAddress, String spender, BigInteger value) throws Exception { HumanStandardToken humanStandardToken = load(contractAddress, privateFor); try { TransactionReceipt transactionReceipt = humanStandardToken .approve(spender, value).send(); return processApprovalEventResponse(humanStandardToken, transactionReceipt); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException(e); } }