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

The following examples show how to use org.web3j.protocol.core.methods.request.Transaction#createEthCallTransaction() . 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: 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 2
Source File: BaseContract.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private GasProvider getDefaultGasProviderRemote(Function function) throws IOException {
    Transaction transaction = Transaction.createEthCallTransaction(transactionManager.getFromAddress(), contractAddress,  EncoderUtils.functionEncoder(function));
    BigInteger gasLimit = web3j.platonEstimateGas(transaction).send().getAmountUsed();
    BigInteger gasPrice = getDefaultGasPrice(function.getType());
    GasProvider gasProvider = new ContractGasProvider(gasPrice, gasLimit);
    return  gasProvider;
}
 
Example 3
Source File: EthereumUtils.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
public static Object queryExistingContract(Credentials credentials,
                                           Web3j web3j,
                                           String contractAddress,
                                           String contractMethodName,
                                           List<Type> contractMethodInputTypes,
                                           List<TypeReference<?>> contractMethodOutputTypes
) throws Exception {

    Function function = getFunction(contractMethodName,
                                    contractMethodInputTypes,
                                    contractMethodOutputTypes);

    Transaction transaction = Transaction.createEthCallTransaction(credentials.getAddress(),
                                                                   contractAddress,
                                                                   getEncodedFunction(function));

    EthCall response = web3j.ethCall(
            transaction,
            DefaultBlockParameterName.LATEST).sendAsync().get();

    List<Type> responseTypeList = FunctionReturnDecoder.decode(
            response.getValue(),
            function.getOutputParameters());

    if (responseTypeList != null && responseTypeList.size() > 0) {
        return responseTypeList.get(0).getValue();
    } else {
        return null;
    }
}
 
Example 4
Source File: DefaultGasLimitEstimator.java    From asf-sdk with GNU General Public License v3.0 5 votes vote down vote up
@Override public BigInteger estimate(Address from, Address to, byte[] data)
    throws EstimateGasException {

  Transaction transaction =
      Transaction.createEthCallTransaction(from.toHexString(true), to.toHexString(true),
          Hex.toHexString(data));

  try {
    return web3j.ethEstimateGas(transaction)
        .send()
        .getAmountUsed();
  } catch (IOException e) {
    throw new EstimateGasException("Failed to estimate gas!", e);
  }
}
 
Example 5
Source File: PrivCallAcceptanceTest.java    From besu with Apache License 2.0 4 votes vote down vote up
@NotNull
private Request<Object, EthCall> privCall(
    final String privacyGroupId,
    final Contract eventEmitter,
    final boolean useInvalidParameters,
    final boolean useInvalidContractAddress) {

  final Uint256 invalid = new Uint256(BigInteger.TEN);

  @SuppressWarnings("rawtypes")
  final List<Type> inputParameters =
      useInvalidParameters ? Arrays.asList(invalid) : Collections.emptyList();

  final Function function =
      new Function(
          "value",
          inputParameters,
          Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));

  final String encoded = FunctionEncoder.encode(function);

  final HttpService httpService =
      new HttpService(
          "http://"
              + minerNode.getBesu().getHostName()
              + ":"
              + minerNode.getBesu().getJsonRpcSocketPort().get());

  final String validContractAddress = eventEmitter.getContractAddress();
  final String invalidContractAddress = constructInvalidString(validContractAddress);
  final String contractAddress =
      useInvalidContractAddress ? invalidContractAddress : validContractAddress;

  final Transaction transaction =
      Transaction.createEthCallTransaction(null, contractAddress, encoded);

  return new Request<>(
      "priv_call",
      Arrays.asList(privacyGroupId, transaction, "latest"),
      httpService,
      EthCall.class);
}