Java Code Examples for org.web3j.protocol.core.methods.response.TransactionReceipt#getContractAddress()

The following examples show how to use org.web3j.protocol.core.methods.response.TransactionReceipt#getContractAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HumanStandardTokenIT.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
private String createContract(
        Credentials credentials, BigInteger initialSupply) throws Exception {
    String createTransactionHash = sendCreateContractTransaction(credentials, initialSupply);
    assertFalse(createTransactionHash.isEmpty());

    TransactionReceipt createTransactionReceipt =
            waitForTransactionReceipt(createTransactionHash);

    assertThat(createTransactionReceipt.getTransactionHash(), is(createTransactionHash));

    assertFalse("Contract execution ran out of gas",
            createTransactionReceipt.getGasUsed().equals(GAS_LIMIT));

    String contractAddress = createTransactionReceipt.getContractAddress();

    assertNotNull(contractAddress);
    return contractAddress;
}
 
Example 2
Source File: HumanStandardTokenIT.java    From web3j with Apache License 2.0 6 votes vote down vote up
private String createContract(Credentials credentials, BigInteger initialSupply)
        throws Exception {
    String createTransactionHash = sendCreateContractTransaction(credentials, initialSupply);
    assertFalse(createTransactionHash.isEmpty());

    TransactionReceipt createTransactionReceipt =
            waitForTransactionReceipt(createTransactionHash);

    assertEquals(createTransactionReceipt.getTransactionHash(), (createTransactionHash));

    assertFalse(createTransactionReceipt.getGasUsed().equals(GAS_LIMIT));

    String contractAddress = createTransactionReceipt.getContractAddress();

    assertNotNull(contractAddress);
    return contractAddress;
}
 
Example 3
Source File: WasmContract.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private static <T extends WasmContract> T create(T contract, String encodedConstructor, BigInteger value)
		throws IOException, TransactionException {
	TransactionReceipt transactionReceipt = contract.executeTransaction(encodedConstructor, value);

	String contractAddress = transactionReceipt.getContractAddress();
	if (contractAddress == null) {
		throw new RuntimeException("Empty contract address returned");
	}
	contract.setContractAddress(contractAddress);
	contract.setTransactionReceipt(transactionReceipt);

	return contract;
}
 
Example 4
Source File: Contract.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private static <T extends Contract> T create(
        T contract, String binary, String encodedConstructor, BigInteger value)
        throws IOException, TransactionException {
    TransactionReceipt transactionReceipt =
            contract.executeTransaction(binary + encodedConstructor, value, FUNC_DEPLOY);

    String contractAddress = transactionReceipt.getContractAddress();
    if (contractAddress == null) {
        throw new RuntimeException("Empty contract address returned");
    }
    contract.setContractAddress(contractAddress);
    contract.setTransactionReceipt(transactionReceipt);

    return contract;
}
 
Example 5
Source File: DeployContractIT.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testContractCreation() throws Exception {
    boolean accountUnlocked = unlockAccount();
    assertTrue(accountUnlocked);

    String transactionHash = sendTransaction();
    assertFalse(transactionHash.isEmpty());

    TransactionReceipt transactionReceipt =
            waitForTransactionReceipt(transactionHash);

    assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));

    assertFalse("Contract execution ran out of gas",
            transactionReceipt.getGasUsed().equals(GAS_LIMIT));

    String contractAddress = transactionReceipt.getContractAddress();

    assertNotNull(contractAddress);

    Function function = createFibonacciFunction();

    String responseValue = callSmartContractFunction(function, contractAddress);
    assertFalse(responseValue.isEmpty());

    List<Type> uint = FunctionReturnDecoder.decode(
            responseValue, function.getOutputParameters());
    assertThat(uint.size(), is(1));
    assertThat(uint.get(0).getValue(), equalTo(BigInteger.valueOf(13)));
}
 
Example 6
Source File: GreeterContractIT.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testGreeterContract() throws Exception {
    boolean accountUnlocked = unlockAccount();
    assertTrue(accountUnlocked);

    // create our smart contract
    String createTransactionHash = sendCreateContractTransaction();
    assertFalse(createTransactionHash.isEmpty());

    TransactionReceipt createTransactionReceipt =
            waitForTransactionReceipt(createTransactionHash);

    assertThat(createTransactionReceipt.getTransactionHash(), is(createTransactionHash));

    assertFalse("Contract execution ran out of gas",
            createTransactionReceipt.getGasUsed().equals(GAS_LIMIT));

    String contractAddress = createTransactionReceipt.getContractAddress();

    assertNotNull(contractAddress);

    // call our getter
    Function getFunction = createGreetFunction();
    String responseValue = callSmartContractFunction(getFunction, contractAddress);
    assertFalse(responseValue.isEmpty());

    List<Type> response = FunctionReturnDecoder.decode(
            responseValue, getFunction.getOutputParameters());
    assertThat(response.size(), is(1));
    assertThat(response.get(0).getValue(), is(VALUE));
}
 
Example 7
Source File: SyncToDatabaseTask.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private List<Account> getAllAddresses(List<Account> accounts, Map<String, TransactionReceipt> transactionReceiptMap) {
    Set<Account> result = new HashSet<>();

    for(TransactionReceipt receipt : transactionReceiptMap.values()) {
        if(null != receipt.getContractAddress()) {
            result.add(new Account.Builder()
                .hash(receipt.getContractAddress())
                .type(1)
                .build());
        }
    }
    result.addAll(accounts);

    return new ArrayList<>(result);
}
 
Example 8
Source File: Contract.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private static <T extends Contract> T create(
        T contract, String binary, String encodedConstructor, BigInteger value)
        throws IOException, TransactionException {
    TransactionReceipt transactionReceipt =
            contract.executeTransaction(binary + encodedConstructor, value);

    String contractAddress = transactionReceipt.getContractAddress();
    if (contractAddress == null) {
        throw new RuntimeException("Empty contract address returned");
    }
    contract.setContractAddress(contractAddress);
    contract.setTransactionReceipt(transactionReceipt);

    return contract;
}
 
Example 9
Source File: EthereumUtils.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
public static String deployContract(Credentials credentials,
                                    Web3j web3j,
                                    String contractBinary,
                                    int toSendEther,
                                    boolean waitForReceipt,
                                    int sleepDuration,
                                    int attempts) throws Exception {

    BigInteger depositEtherAmountToSend = BigInteger.valueOf(toSendEther);

    RawTransaction rawTransaction = RawTransaction.createContractTransaction(
            getNextNonce(credentials.getAddress(),
                         web3j),
            DEFAULT_GAS_PRICE,
            DEFAULT_GAS_LIMIT,
            depositEtherAmountToSend,
            contractBinary);

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction,
                                                          credentials);

    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();

    if (waitForReceipt) {
        TransactionReceipt transReceipt = waitForTransactionReceipt(
                ethSendTransaction.getTransactionHash(),
                sleepDuration,
                attempts,
                web3j
        );
        if (transReceipt != null) {
            return transReceipt.getContractAddress();
        }
    }
    // we dont have a contract address
    logger.warn("Unable to retrieve contract address.");
    return null;
}
 
Example 10
Source File: GreeterContractIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGreeterContract() throws Exception {
    boolean accountUnlocked = unlockAccount();
    assertTrue(accountUnlocked);

    // create our smart contract
    String createTransactionHash = sendCreateContractTransaction();
    assertFalse(createTransactionHash.isEmpty());

    TransactionReceipt createTransactionReceipt =
            waitForTransactionReceipt(createTransactionHash);

    assertEquals(createTransactionReceipt.getTransactionHash(), (createTransactionHash));

    assertFalse(createTransactionReceipt.getGasUsed().equals(GAS_LIMIT));

    String contractAddress = createTransactionReceipt.getContractAddress();

    assertNotNull(contractAddress);

    // call our getter
    Function getFunction = createGreetFunction();
    String responseValue = callSmartContractFunction(getFunction, contractAddress);
    assertFalse(responseValue.isEmpty());

    List<Type> response =
            FunctionReturnDecoder.decode(responseValue, getFunction.getOutputParameters());
    assertEquals(response.size(), (1));
    assertEquals(response.get(0).getValue(), (VALUE));
}
 
Example 11
Source File: Contract.java    From web3j with Apache License 2.0 5 votes vote down vote up
private static <T extends Contract> T create(
        T contract, String binary, String encodedConstructor, BigInteger value)
        throws IOException, TransactionException {
    TransactionReceipt transactionReceipt =
            contract.executeTransaction(binary + encodedConstructor, value, FUNC_DEPLOY, true);

    String contractAddress = transactionReceipt.getContractAddress();
    if (contractAddress == null) {
        throw new RuntimeException("Empty contract address returned");
    }
    contract.setContractAddress(contractAddress);
    contract.setTransactionReceipt(transactionReceipt);

    return contract;
}
 
Example 12
Source File: DeployContractIT.java    From web3j with Apache License 2.0 3 votes vote down vote up
@Test
public void testContractCreation() throws Exception {
    boolean accountUnlocked = unlockAccount();
    assertTrue(accountUnlocked);

    String transactionHash = sendTransaction();
    assertFalse(transactionHash.isEmpty());

    TransactionReceipt transactionReceipt = waitForTransactionReceipt(transactionHash);

    assertEquals(transactionReceipt.getTransactionHash(), (transactionHash));

    assertFalse(transactionReceipt.getGasUsed().equals(GAS_LIMIT));

    String contractAddress = transactionReceipt.getContractAddress();

    assertNotNull(contractAddress);

    Function function = createFibonacciFunction();

    String responseValue = callSmartContractFunction(function, contractAddress);
    assertFalse(responseValue.isEmpty());

    List<Type> uint =
            FunctionReturnDecoder.decode(responseValue, function.getOutputParameters());
    assertEquals(uint.size(), (1));
    assertEquals(uint.get(0).getValue(), (BigInteger.valueOf(13)));
}