Java Code Examples for org.web3j.protocol.core.methods.request.Transaction#createFunctionCallTransaction()

The following examples show how to use org.web3j.protocol.core.methods.request.Transaction#createFunctionCallTransaction() . 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: EventFilterIT.java    From web3j with Apache License 2.0 6 votes vote down vote up
private String sendTransaction(
        Credentials credentials, String contractAddress, BigInteger gas, String encodedFunction)
        throws Exception {
    BigInteger nonce = getNonce(credentials.getAddress());
    Transaction transaction =
            Transaction.createFunctionCallTransaction(
                    credentials.getAddress(),
                    nonce,
                    Transaction.DEFAULT_GAS,
                    gas,
                    contractAddress,
                    encodedFunction);

    org.web3j.protocol.core.methods.response.EthSendTransaction transactionResponse =
            web3j.ethSendTransaction(transaction).sendAsync().get();

    assertFalse(transactionResponse.hasError());

    return transactionResponse.getTransactionHash();
}
 
Example 2
Source File: SmartContractAcceptanceTest.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeContract() {
  final Transaction contract =
      Transaction.createContractTransaction(
          richBenefactor().address(),
          richBenefactor().nextNonceAndIncrement(),
          GAS_PRICE,
          GAS_LIMIT,
          BigInteger.ZERO,
          SIMPLE_STORAGE_BINARY);

  final String hash = ethSigner().publicContracts().submit(contract);
  ethNode().publicContracts().awaitBlockContaining(hash);

  final String contractAddress = ethNode().publicContracts().address(hash);
  final Transaction valueBeforeChange =
      Transaction.createEthCallTransaction(
          richBenefactor().address(), contractAddress, SIMPLE_STORAGE_GET);
  final BigInteger startingValue = hex(ethSigner().publicContracts().call(valueBeforeChange));
  final Transaction changeValue =
      Transaction.createFunctionCallTransaction(
          richBenefactor().address(),
          richBenefactor().nextNonceAndIncrement(),
          GAS_PRICE,
          GAS_LIMIT,
          contractAddress,
          SIMPLE_STORAGE_SET_7);

  final String valueUpdate = ethSigner().publicContracts().submit(changeValue);
  ethNode().publicContracts().awaitBlockContaining(valueUpdate);

  final Transaction valueAfterChange =
      Transaction.createEthCallTransaction(
          richBenefactor().address(), contractAddress, SIMPLE_STORAGE_GET);
  final BigInteger endValue = hex(ethSigner().publicContracts().call(valueAfterChange));
  assertThat(endValue).isEqualTo(startingValue.add(BigInteger.valueOf(7)));
}
 
Example 3
Source File: EventFilterIT.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private String sendTransaction(
        Credentials credentials, String contractAddress, BigInteger gas,
        String encodedFunction) throws Exception {
    BigInteger nonce = getNonce(credentials.getAddress());
    Transaction transaction = Transaction.createFunctionCallTransaction(
            credentials.getAddress(), nonce, Transaction.DEFAULT_GAS, gas, contractAddress,
            encodedFunction);

    org.web3j.protocol.core.methods.response.EthSendTransaction transactionResponse =
            web3j.ethSendTransaction(transaction).sendAsync().get();

    assertFalse(transactionResponse.hasError());

    return transactionResponse.getTransactionHash();
}
 
Example 4
Source File: RequestTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testTraceCall() throws Exception {
    Transaction transaction = Transaction.createFunctionCallTransaction(
            "0xc171033d5cbff7175f29dfd3a63dda3d6f8f385e",
            BigInteger.ONE,
            Numeric.toBigInt("0x9184e72a000"),
            Numeric.toBigInt("0x76c0"),
            "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
            Numeric.toBigInt("0x9184e72a"),
            "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb"
                + "970870f072445675058bb8eb970870f072445675"
    );
    web3j.traceCall(
            transaction,
            Arrays.asList("trace", "vmTrace", "stateDiff"),
            DefaultBlockParameterName.LATEST
    ).send();

    //CHECKSTYLE:OFF
    verifyResult("{\"jsonrpc\":\"2.0\",\"method\":\"trace_call\","
            + "\"params\":["
            + "{\"from\":\"0xc171033d5cbff7175f29dfd3a63dda3d6f8f385e\","
            + "\"to\":\"0xb60e8dd61c5d32be8058bb8eb970870f07233155\","
            + "\"gas\":\"0x76c0\","
            + "\"gasPrice\":\"0x9184e72a000\","
            + "\"value\":\"0x9184e72a\","
            + "\"data\":\"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675\","
            + "\"nonce\":\"0x1\"},"
            + "[\"trace\",\"vmTrace\",\"stateDiff\"],"
            + "\"latest\"],"
            + "\"id\":1}");
    //CHECKSTYLE:ON
}
 
Example 5
Source File: RequestTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testTraceCall() throws Exception {
    Transaction transaction =
            Transaction.createFunctionCallTransaction(
                    "0xc171033d5cbff7175f29dfd3a63dda3d6f8f385e",
                    BigInteger.ONE,
                    Numeric.toBigInt("0x9184e72a000"),
                    Numeric.toBigInt("0x76c0"),
                    "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
                    Numeric.toBigInt("0x9184e72a"),
                    "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb"
                            + "970870f072445675058bb8eb970870f072445675");
    web3j.traceCall(
                    transaction,
                    Arrays.asList("trace", "vmTrace", "stateDiff"),
                    DefaultBlockParameterName.LATEST)
            .send();

    verifyResult(
            "{\"jsonrpc\":\"2.0\",\"method\":\"trace_call\","
                    + "\"params\":["
                    + "{\"from\":\"0xc171033d5cbff7175f29dfd3a63dda3d6f8f385e\","
                    + "\"to\":\"0xb60e8dd61c5d32be8058bb8eb970870f07233155\","
                    + "\"gas\":\"0x76c0\","
                    + "\"gasPrice\":\"0x9184e72a000\","
                    + "\"value\":\"0x9184e72a\","
                    + "\"data\":\"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675\","
                    + "\"nonce\":\"0x1\"},"
                    + "[\"trace\",\"vmTrace\",\"stateDiff\"],"
                    + "\"latest\"],"
                    + "\"id\":1}");
}
 
Example 6
Source File: Web3Service.java    From tutorials with MIT License 5 votes vote down vote up
@Async
public String sendTx() {
    String transactionHash = "";

    try {
        List inputParams = new ArrayList();
        List outputParams = new ArrayList();
        Function function = new Function("fuctionName", inputParams, outputParams);

        String encodedFunction = FunctionEncoder.encode(function);

        BigInteger nonce = BigInteger.valueOf(100);
        BigInteger gasprice = BigInteger.valueOf(100);
        BigInteger gaslimit = BigInteger.valueOf(100);

        Transaction transaction = Transaction.createFunctionCallTransaction("FROM_ADDRESS", nonce, gasprice, gaslimit, "TO_ADDRESS", encodedFunction);

        org.web3j.protocol.core.methods.response.EthSendTransaction transactionResponse = web3j.ethSendTransaction(transaction).sendAsync().get();
        transactionHash = transactionResponse.getTransactionHash();

    } catch (Exception ex) {
        System.out.println(PLEASE_SUPPLY_REAL_DATA);
        return PLEASE_SUPPLY_REAL_DATA;
    }

    return transactionHash;
}
 
Example 7
Source File: EthereumUtils.java    From jbpm-work-items with Apache License 2.0 4 votes vote down vote up
public static TransactionReceipt transactExistingContract(Credentials credentials,
                                                          Web3j web3j,
                                                          int etherAmount,
                                                          BigInteger gasPrice,
                                                          BigInteger gasLimit,
                                                          String toAddress,
                                                          String methodName,
                                                          List<Type> methodInputTypes,
                                                          List<TypeReference<?>> methodOutputTypes,
                                                          boolean waitForReceipt,
                                                          int sleepDuration,
                                                          int attempts
) throws Exception {
    BigInteger etherAmountToSend = BigInteger.valueOf(etherAmount);

    Transaction transaction = Transaction.createFunctionCallTransaction(
            credentials.getAddress(),
            getNextNonce(credentials.getAddress(),
                         web3j),
            gasPrice,
            gasLimit,
            toAddress,
            etherAmountToSend,
            getEncodedFunction(methodName,
                               methodInputTypes,
                               methodOutputTypes));

    EthSendTransaction transactionResponse =
            web3j.ethSendTransaction(transaction).sendAsync().get();

    if (waitForReceipt) {
        TransactionReceipt transReceipt = waitForTransactionReceipt(
                transactionResponse.getTransactionHash(),
                sleepDuration,
                attempts,
                web3j
        );
        return transReceipt;
    }
    // we dont have a transaction receipt
    logger.warn("Unable to retrieve transaction receipt.");
    return null;
}