org.web3j.protocol.core.methods.response.EthCall Java Examples

The following examples show how to use org.web3j.protocol.core.methods.response.EthCall. 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: PrivacyRequestFactory.java    From besu with Apache License 2.0 6 votes vote down vote up
public Request<?, EthCall> privCall(
    final String privacyGroupId,
    final Contract contract,
    final String encoded,
    final String blockNumberLatestPending) {

  final org.web3j.protocol.core.methods.request.Transaction transaction =
      org.web3j.protocol.core.methods.request.Transaction.createEthCallTransaction(
          null, contract.getContractAddress(), encoded);

  return new Request<>(
      "priv_call",
      Arrays.asList(privacyGroupId, transaction, blockNumberLatestPending),
      web3jService,
      EthCall.class);
}
 
Example #2
Source File: TokenRepository.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
private String callSmartContractFunction(
        Function function, String contractAddress, NetworkInfo network, Wallet wallet) throws Exception
{
    try
    {
        String encodedFunction = FunctionEncoder.encode(function);

        org.web3j.protocol.core.methods.request.Transaction transaction
                = createEthCallTransaction(wallet.address, contractAddress, encodedFunction);
        EthCall response = getService(network.chainId).ethCall(transaction, DefaultBlockParameterName.LATEST).send();

        return response.getValue();
    }
    catch (InterruptedIOException|UnknownHostException e)
    {
        //expected to happen when user switches wallets
        return "0x";
    }
}
 
Example #3
Source File: TokenRepository.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
/**
 * Call smart contract function on custom network contract. This would be used for things like ENS lookup
 * Currently because it's tied to a mainnet contract address there's no circumstance it would work
 * outside of mainnet. Users may be confused if their namespace doesn't work, even if they're currently
 * using testnet.
 *
 * @param function
 * @param contractAddress
 * @param wallet
 * @return
 */
private String callCustomNetSmartContractFunction(
        Function function, String contractAddress, Wallet wallet, int chainId)  {
    String encodedFunction = FunctionEncoder.encode(function);

    try
    {
        org.web3j.protocol.core.methods.request.Transaction transaction
                = createEthCallTransaction(wallet.address, contractAddress, encodedFunction);
        EthCall response = getService(chainId).ethCall(transaction, DefaultBlockParameterName.LATEST).send();

        return response.getValue();
    }
    catch (Exception e)
    {
        e.printStackTrace();
        return null;
    }
}
 
Example #4
Source File: EnsResolver.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
private String callSmartContractFunction(
        Function function, String contractAddress, int chainId) throws Exception
{
    try
    {
        String encodedFunction = FunctionEncoder.encode(function);

        org.web3j.protocol.core.methods.request.Transaction transaction
                = createEthCallTransaction(TokenscriptFunction.ZERO_ADDRESS, contractAddress, encodedFunction);
        EthCall response = TokenRepository.getWeb3jService(chainId).ethCall(transaction, DefaultBlockParameterName.LATEST).send();

        return response.getValue();
    }
    catch (InterruptedIOException | UnknownHostException e)
    {
        //expected to happen when user switches wallets
        return "0x";
    }
}
 
Example #5
Source File: ContractTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void prepareCall(String result) throws IOException {
    EthCall ethCall = new EthCall();
    ethCall.setResult(result);

    Request<?, EthCall> request = mock(Request.class);
    when(request.send()).thenReturn(ethCall);

    when(web3j.ethCall(any(Transaction.class), any(DefaultBlockParameter.class)))
            .thenReturn((Request) request);
}
 
Example #6
Source File: EnsResolverTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReverseResolve() throws Exception {
    configureSyncing(false);
    configureLatestBlock(System.currentTimeMillis() / 1000); // block timestamp is in seconds

    NetVersion netVersion = new NetVersion();
    netVersion.setResult(Long.toString(ChainIdLong.MAINNET));

    String resolverAddress =
            "0x0000000000000000000000004c641fb9bad9b60ef180c31f56051ce826d21a9a";
    String contractName =
            "0x0000000000000000000000000000000000000000000000000000000000000020"
                    + TypeEncoder.encode(new Utf8String("web3j.eth"));
    System.err.println(contractName);

    EthCall resolverAddressResponse = new EthCall();
    resolverAddressResponse.setResult(resolverAddress);

    EthCall contractNameResponse = new EthCall();
    contractNameResponse.setResult(contractName);

    when(web3jService.send(any(Request.class), eq(NetVersion.class))).thenReturn(netVersion);
    when(web3jService.send(any(Request.class), eq(EthCall.class)))
            .thenReturn(resolverAddressResponse);
    when(web3jService.send(any(Request.class), eq(EthCall.class)))
            .thenReturn(contractNameResponse);

    assertEquals(
            ensResolver.reverseResolve("0x19e03255f667bdfd50a32722df860b1eeaf4d635"),
            ("web3j.eth"));
}
 
Example #7
Source File: EnsResolverTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolve() throws Exception {
    configureSyncing(false);
    configureLatestBlock(System.currentTimeMillis() / 1000); // block timestamp is in seconds

    NetVersion netVersion = new NetVersion();
    netVersion.setResult(Long.toString(ChainIdLong.MAINNET));

    String resolverAddress =
            "0x0000000000000000000000004c641fb9bad9b60ef180c31f56051ce826d21a9a";
    String contractAddress =
            "0x00000000000000000000000019e03255f667bdfd50a32722df860b1eeaf4d635";

    EthCall resolverAddressResponse = new EthCall();
    resolverAddressResponse.setResult(resolverAddress);

    EthCall contractAddressResponse = new EthCall();
    contractAddressResponse.setResult(contractAddress);

    when(web3jService.send(any(Request.class), eq(NetVersion.class))).thenReturn(netVersion);
    when(web3jService.send(any(Request.class), eq(EthCall.class)))
            .thenReturn(resolverAddressResponse);
    when(web3jService.send(any(Request.class), eq(EthCall.class)))
            .thenReturn(contractAddressResponse);

    assertEquals(
            ensResolver.resolve("web3j.eth"), ("0x19e03255f667bdfd50a32722df860b1eeaf4d635"));
}
 
Example #8
Source File: ReadonlyTransactionManager.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Override
public String sendCall(String to, String data, DefaultBlockParameter defaultBlockParameter)
        throws IOException {
    EthCall ethCall =
            web3j.ethCall(
                            Transaction.createEthCallTransaction(fromAddress, to, data),
                            defaultBlockParameter)
                    .send();

    assertCallNotReverted(ethCall);
    return ethCall.getValue();
}
 
Example #9
Source File: RawTransactionManager.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Override
public String sendCall(String to, String data, DefaultBlockParameter defaultBlockParameter)
        throws IOException {
    EthCall ethCall =
            web3j.ethCall(
                            Transaction.createEthCallTransaction(getFromAddress(), to, data),
                            defaultBlockParameter)
                    .send();

    assertCallNotReverted(ethCall);
    return ethCall.getValue();
}
 
Example #10
Source File: ClientTransactionManager.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Override
public String sendCall(String to, String data, DefaultBlockParameter defaultBlockParameter)
        throws IOException {
    EthCall ethCall =
            web3j.ethCall(
                            Transaction.createEthCallTransaction(getFromAddress(), to, data),
                            defaultBlockParameter)
                    .send();

    assertCallNotReverted(ethCall);
    return ethCall.getValue();
}
 
Example #11
Source File: JsonRpc2_0Besu.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Override
public Request<?, EthCall> privCall(
        String privacyGroupId,
        final Transaction transaction,
        final DefaultBlockParameter defaultBlockParameter) {
    return new Request<>(
            "priv_call",
            Arrays.asList(privacyGroupId, transaction, defaultBlockParameter),
            web3jService,
            org.web3j.protocol.core.methods.response.EthCall.class);
}
 
Example #12
Source File: CoreIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEthCall() throws Exception {
    EthCall ethCall =
            web3j.ethCall(config.buildTransaction(), DefaultBlockParameter.valueOf("latest"))
                    .send();

    assertEquals(DefaultBlockParameterName.LATEST.getValue(), ("latest"));
    assertEquals(ethCall.getValue(), ("0x"));
}
 
Example #13
Source File: EthCallIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
private EthCall ethCall(BigInteger value) throws java.io.IOException {
    final Function function =
            new Function(
                    Revert.FUNC_SET,
                    Collections.singletonList(new Uint256(value)),
                    Collections.emptyList());
    String encodedFunction = FunctionEncoder.encode(function);

    return web3j.ethCall(
                    Transaction.createEthCallTransaction(
                            ALICE.getAddress(), contract.getContractAddress(), encodedFunction),
                    DefaultBlockParameterName.LATEST)
            .send();
}
 
Example #14
Source File: EthCallIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRevertWithMessage() throws Exception {
    EthCall ethCall = ethCall(BigInteger.valueOf(2L));

    assertTrue(ethCall.isReverted());
    assertTrue(ethCall.getRevertReason().endsWith("revert The reason for revert"));
}
 
Example #15
Source File: EthCallIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRevertWithoutMessage() throws Exception {
    EthCall ethCall = ethCall(BigInteger.valueOf(1L));

    assertTrue(ethCall.isReverted());
    assertTrue(ethCall.getRevertReason().endsWith("revert"));
}
 
Example #16
Source File: AsfWeb3jImpl.java    From asf-sdk with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Observable<String> call(org.web3j.protocol.core.methods.request.Transaction transaction) {
  return Observable.fromCallable(
      () -> web3j.ethCall(transaction, DefaultBlockParameterName.LATEST)
          .send())
      .map(EthCall::getValue);
}
 
Example #17
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 #18
Source File: ResponseTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEthCall() {
    buildResponse(
            "{\n"
                    + "  \"id\":1,\n"
                    + "  \"jsonrpc\": \"2.0\",\n"
                    + "  \"result\": \"0x\"\n"
                    + "}");

    EthCall ethCall = deserialiseResponse(EthCall.class);
    assertEquals(ethCall.getValue(), ("0x"));
    assertFalse(ethCall.isReverted());
    assertNull(ethCall.getRevertReason());
}
 
Example #19
Source File: PrivCallTransaction.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public EthCall execute(final NodeRequests node) {
  try {
    final EthCall response =
        node.privacy()
            .privCall(privacyGroupId, contract, encoded, blockNumberLatestPending)
            .send();
    assertThat(response).as("check response is not null").isNotNull();
    assertThat(response.getResult()).as("check result in response isn't null").isNotNull();
    return response;
  } catch (final IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #20
Source File: ResponseTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEthCallReverted() {
    buildResponse(
            "{\n"
                    + "  \"id\":1,\n"
                    + "  \"jsonrpc\": \"2.0\",\n"
                    + "  \"result\": \"0x08c379a0"
                    + "0000000000000000000000000000000000000000000000000000000000000020"
                    + "00000000000000000000000000000000000000000000000000000000000000ee"
                    + "536f6c696469747920757365732073746174652d726576657274696e67206578"
                    + "63657074696f6e7320746f2068616e646c65206572726f72732e205468652072"
                    + "6571756972652066756e6374696f6e2073686f756c6420626520757365642074"
                    + "6f20656e737572652076616c696420636f6e646974696f6e732c207375636820"
                    + "617320696e707574732c206f7220636f6e747261637420737461746520766172"
                    + "6961626c657320617265206d65742c206f7220746f2076616c69646174652072"
                    + "657475726e2076616c7565732066726f6d2063616c6c7320746f206578746572"
                    + "6e616c20636f6e7472616374732e000000000000000000000000000000000000\"\n"
                    + "}");

    EthCall ethCall = deserialiseResponse(EthCall.class);
    assertTrue(ethCall.isReverted());
    assertEquals(
            ethCall.getRevertReason(),
            ("Solidity uses state-reverting exceptions to "
                    + "handle errors. The require function should be "
                    + "used to ensure valid conditions, such as inputs, "
                    + "or contract state variables are met, or to "
                    + "validate return values from calls to "
                    + "external contracts."));
}
 
Example #21
Source File: ContractTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@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("")));
}
 
Example #22
Source File: OnChainPrivacyAcceptanceTest.java    From besu with Apache License 2.0 5 votes vote down vote up
private void checkEmitterValue(
    final List<PrivacyNode> nodes,
    final String privacyGroupId,
    final EventEmitter eventEmitter,
    final int valueWhileBobMember) {
  for (final PrivacyNode node : nodes) {
    final EthCall response =
        node.execute(
            privacyTransactions.privCall(
                privacyGroupId, eventEmitter, eventEmitter.value().encodeFunctionCall()));

    assertThat(new BigInteger(response.getValue().substring(2), 16))
        .isEqualByComparingTo(BigInteger.valueOf(valueWhileBobMember));
  }
}
 
Example #23
Source File: PrivCallAcceptanceTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void mustReturnCorrectValue() throws Exception {

  final String privacyGroupId =
      minerNode.execute(
          privacyTransactions.createPrivacyGroup(
              "myGroupName", "my group description", minerNode));

  final EventEmitter eventEmitter =
      minerNode.execute(
          privateContractTransactions.createSmartContractWithPrivacyGroupId(
              EventEmitter.class,
              minerNode.getTransactionSigningKey(),
              POW_CHAIN_ID,
              minerNode.getEnclaveKey(),
              privacyGroupId));

  privateContractVerifier
      .validPrivateContractDeployed(
          eventEmitter.getContractAddress(), minerNode.getAddress().toString())
      .verify(eventEmitter);

  final Request<Object, EthCall> priv_call = privCall(privacyGroupId, eventEmitter, false, false);

  EthCall resp = priv_call.send();

  String value = resp.getValue();
  assertThat(new BigInteger(value.substring(2), 16)).isEqualByComparingTo(BigInteger.ZERO);

  final TransactionReceipt receipt = eventEmitter.store(BigInteger.valueOf(VALUE)).send();
  assertThat(receipt).isNotNull();

  resp = priv_call.send();
  value = resp.getValue();
  assertThat(new BigInteger(value.substring(2), 16))
      .isEqualByComparingTo(BigInteger.valueOf(VALUE));
}
 
Example #24
Source File: PrivCallAcceptanceTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnEmptyResultWithNonExistingPrivacyGroup() throws IOException {

  final String privacyGroupId =
      minerNode.execute(
          privacyTransactions.createPrivacyGroup(
              "myGroupName", "my group description", minerNode));

  final EventEmitter eventEmitter =
      minerNode.execute(
          privateContractTransactions.createSmartContractWithPrivacyGroupId(
              EventEmitter.class,
              minerNode.getTransactionSigningKey(),
              POW_CHAIN_ID,
              minerNode.getEnclaveKey(),
              privacyGroupId));

  privateContractVerifier
      .validPrivateContractDeployed(
          eventEmitter.getContractAddress(), minerNode.getAddress().toString())
      .verify(eventEmitter);

  final String invalidPrivacyGroup = constructInvalidString(privacyGroupId);
  final Request<Object, EthCall> privCall =
      privCall(invalidPrivacyGroup, eventEmitter, false, false);

  final EthCall result = privCall.send();

  assertThat(result.getResult()).isEqualTo("0x");
}
 
Example #25
Source File: PrivCallAcceptanceTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void mustNotSucceedWithWronglyEncodedFunction() {

  final String privacyGroupId =
      minerNode.execute(
          privacyTransactions.createPrivacyGroup(
              "myGroupName", "my group description", minerNode));

  final EventEmitter eventEmitter =
      minerNode.execute(
          privateContractTransactions.createSmartContractWithPrivacyGroupId(
              EventEmitter.class,
              minerNode.getTransactionSigningKey(),
              POW_CHAIN_ID,
              minerNode.getEnclaveKey(),
              privacyGroupId));

  privateContractVerifier
      .validPrivateContractDeployed(
          eventEmitter.getContractAddress(), minerNode.getAddress().toString())
      .verify(eventEmitter);

  final Request<Object, EthCall> priv_call = privCall(privacyGroupId, eventEmitter, true, false);

  assertThatExceptionOfType(ClientConnectionException.class)
      .isThrownBy(() -> priv_call.send())
      .withMessageContaining("Invalid params");
}
 
Example #26
Source File: PrivCallAcceptanceTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void mustReturn0xUsingInvalidContractAddress() throws IOException {

  final String privacyGroupId =
      minerNode.execute(
          privacyTransactions.createPrivacyGroup(
              "myGroupName", "my group description", minerNode));

  final EventEmitter eventEmitter =
      minerNode.execute(
          privateContractTransactions.createSmartContractWithPrivacyGroupId(
              EventEmitter.class,
              minerNode.getTransactionSigningKey(),
              POW_CHAIN_ID,
              minerNode.getEnclaveKey(),
              privacyGroupId));

  privateContractVerifier
      .validPrivateContractDeployed(
          eventEmitter.getContractAddress(), minerNode.getAddress().toString())
      .verify(eventEmitter);

  final Request<Object, EthCall> priv_call = privCall(privacyGroupId, eventEmitter, false, true);

  final EthCall result = priv_call.send();

  assertThat(result).isNotNull();
  assertThat(result.getResult()).isEqualTo("0x");
}
 
Example #27
Source File: CoreIT.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testEthCall() throws Exception {
    EthCall ethCall = web3j.ethCall(config.buildTransaction(),
            DefaultBlockParameter.valueOf("latest")).send();

    assertThat(DefaultBlockParameterName.LATEST.getValue(), is("latest"));
    assertThat(ethCall.getValue(), is("0x"));
}
 
Example #28
Source File: EnsResolverTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testResolve() throws Exception {
    configureSyncing(false);
    configureLatestBlock(System.currentTimeMillis() / 1000);  // block timestamp is in seconds

    NetVersion netVersion = new NetVersion();
    netVersion.setResult(Byte.toString(ChainId.MAINNET));

    String resolverAddress =
            "0x0000000000000000000000004c641fb9bad9b60ef180c31f56051ce826d21a9a";
    String contractAddress =
            "0x00000000000000000000000019e03255f667bdfd50a32722df860b1eeaf4d635";

    EthCall resolverAddressResponse = new EthCall();
    resolverAddressResponse.setResult(resolverAddress);

    EthCall contractAddressResponse = new EthCall();
    contractAddressResponse.setResult(contractAddress);

    when(web3jService.send(any(Request.class), eq(NetVersion.class)))
            .thenReturn(netVersion);
    when(web3jService.send(any(Request.class), eq(EthCall.class)))
            .thenReturn(resolverAddressResponse);
    when(web3jService.send(any(Request.class), eq(EthCall.class)))
            .thenReturn(contractAddressResponse);

    assertThat(ensResolver.resolve("web3j.eth"),
            is("0x19e03255f667bdfd50a32722df860b1eeaf4d635"));
}
 
Example #29
Source File: EnsResolverTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testReverseResolve() throws Exception {
    configureSyncing(false);
    configureLatestBlock(System.currentTimeMillis() / 1000);  // block timestamp is in seconds

    NetVersion netVersion = new NetVersion();
    netVersion.setResult(Byte.toString(ChainId.MAINNET));

    String resolverAddress =
            "0x0000000000000000000000004c641fb9bad9b60ef180c31f56051ce826d21a9a";
    String contractName =
            "0x0000000000000000000000000000000000000000000000000000000000000020"
            + TypeEncoder.encode(new Utf8String("web3j.eth"));
    System.err.println(contractName);

    EthCall resolverAddressResponse = new EthCall();
    resolverAddressResponse.setResult(resolverAddress);

    EthCall contractNameResponse = new EthCall();
    contractNameResponse.setResult(contractName);

    when(web3jService.send(any(Request.class), eq(NetVersion.class)))
            .thenReturn(netVersion);
    when(web3jService.send(any(Request.class), eq(EthCall.class)))
            .thenReturn(resolverAddressResponse);
    when(web3jService.send(any(Request.class), eq(EthCall.class)))
            .thenReturn(contractNameResponse);

    assertThat(ensResolver.reverseResolve("0x19e03255f667bdfd50a32722df860b1eeaf4d635"),
            is("web3j.eth"));
}
 
Example #30
Source File: ContractTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testCallSingleValueEmpty() throws Exception {
    // Example taken from FunctionReturnDecoderTest

    EthCall ethCall = new EthCall();
    ethCall.setResult("0x");
    prepareCall(ethCall);

    assertNull(contract.callSingleValue().send());
}