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

The following examples show how to use org.web3j.protocol.core.methods.response.EthGetTransactionCount. 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: EthereumRestController.java    From tutorials with MIT License 6 votes vote down vote up
@RequestMapping(value = Constants.API_TRANSACTIONS, method = RequestMethod.GET)
public Future<ResponseTransfer> getTransactions() {
    ResponseTransfer responseTransfer = new ResponseTransfer();
    Instant start = TimeHelper.start();
    return CompletableFuture.supplyAsync(() -> {
        try {
            EthGetTransactionCount result = web3Service.getTransactionCount();
            responseTransfer.setMessage(result.toString());
        } catch (Exception e) {
            responseTransfer.setMessage(GENERIC_EXCEPTION);
        }
        return responseTransfer;
    }).thenApplyAsync(result -> {
        result.setPerformance(TimeHelper.stop(start));
        return result;
    });
}
 
Example #2
Source File: TransactionRepository.java    From ETHWallet with GNU General Public License v3.0 6 votes vote down vote up
public Single<String> createTransaction(Wallet from, String toAddress, BigInteger subunitAmount, BigInteger gasPrice, BigInteger gasLimit, String data, String password) {
    final Web3j web3j = Web3jFactory.build(new HttpService(networkRepository.getDefaultNetwork().rpcServerUrl));

    return Single.fromCallable(() -> {
        EthGetTransactionCount ethGetTransactionCount = web3j
                .ethGetTransactionCount(from.getAddress(), DefaultBlockParameterName.LATEST)
                .send();
        return ethGetTransactionCount.getTransactionCount();
    })
            .flatMap(
                    nonce -> accountKeystoreService.signTransaction(from, password, toAddress, subunitAmount, gasPrice, gasLimit, nonce.longValue(), data, networkRepository.getDefaultNetwork().chainId))
            .flatMap(
                    signedMessage -> Single.fromCallable(() -> {
                        EthSendTransaction raw = web3j
                                .ethSendRawTransaction(Numeric.toHexString(signedMessage))
                                .send();
                        if (raw.hasError()) {
                            throw new ServiceException(raw.getError().getMessage());
                        }
                        return raw.getTransactionHash();

            })).subscribeOn(Schedulers.io());
}
 
Example #3
Source File: TransactionRepository.java    From trust-wallet-android-source with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Single<String> createTransaction(Wallet from, String toAddress, BigInteger subunitAmount, BigInteger gasPrice, BigInteger gasLimit, byte[] data, String password) {
	final Web3j web3j = Web3jFactory.build(new HttpService(networkRepository.getDefaultNetwork().rpcServerUrl));

	return Single.fromCallable(() -> {
		EthGetTransactionCount ethGetTransactionCount = web3j
				.ethGetTransactionCount(from.address, DefaultBlockParameterName.LATEST)
				.send();
		return ethGetTransactionCount.getTransactionCount();
	})
	.flatMap(nonce -> accountKeystoreService.signTransaction(from, password, toAddress, subunitAmount, gasPrice, gasLimit, nonce.longValue(), data, networkRepository.getDefaultNetwork().chainId))
	.flatMap(signedMessage -> Single.fromCallable( () -> {
		EthSendTransaction raw = web3j
				.ethSendRawTransaction(Numeric.toHexString(signedMessage))
				.send();
		if (raw.hasError()) {
			throw new ServiceException(raw.getError().getMessage());
		}
		return raw.getTransactionHash();
	})).subscribeOn(Schedulers.io());
}
 
Example #4
Source File: EthereumNetworkRepository.java    From Upchain-wallet with GNU Affero General Public License v3.0 5 votes vote down vote up
public Single<BigInteger> getLastTransactionNonce(Web3j web3j, String walletAddress)
{
    return Single.fromCallable(() -> {
        EthGetTransactionCount ethGetTransactionCount = web3j
                .ethGetTransactionCount(walletAddress, DefaultBlockParameterName.PENDING)   // or DefaultBlockParameterName.LATEST
                .send();
        return ethGetTransactionCount.getTransactionCount();
    });
}
 
Example #5
Source File: Web3Service.java    From tutorials with MIT License 5 votes vote down vote up
public EthGetTransactionCount getTransactionCount() {
    EthGetTransactionCount result = new EthGetTransactionCount();
    try {
         result = this.web3j.ethGetTransactionCount(DEFAULT_ADDRESS, DefaultBlockParameter.valueOf("latest")).sendAsync().get();
    } catch (Exception ex) {
        System.out.println(GENERIC_EXCEPTION);
    }
    return result;
}
 
Example #6
Source File: ResponseTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEthGetTransactionCount() {
    buildResponse(
            "{\n"
                    + "  \"id\":1,\n"
                    + "  \"jsonrpc\": \"2.0\",\n"
                    + "  \"result\": \"0x1\"\n"
                    + "}");

    EthGetTransactionCount ethGetTransactionCount =
            deserialiseResponse((EthGetTransactionCount.class));
    assertEquals(ethGetTransactionCount.getTransactionCount(), (BigInteger.valueOf(1L)));
}
 
Example #7
Source File: ManagedTransactionTester.java    From web3j with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
void prepareNonceRequest() throws IOException {
    EthGetTransactionCount ethGetTransactionCount = new EthGetTransactionCount();
    ethGetTransactionCount.setResult("0x1");

    Request<?, EthGetTransactionCount> transactionCountRequest = mock(Request.class);
    when(transactionCountRequest.send()).thenReturn(ethGetTransactionCount);
    when(web3j.ethGetTransactionCount(SampleKeys.ADDRESS, DefaultBlockParameterName.PENDING))
            .thenReturn((Request) transactionCountRequest);
}
 
Example #8
Source File: JsonRpc2_0Web3j.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Override
public Request<?, EthGetTransactionCount> ethGetTransactionCount(
        String address, DefaultBlockParameter defaultBlockParameter) {
    return new Request<>(
            "eth_getTransactionCount",
            Arrays.asList(address, defaultBlockParameter.getValue()),
            web3jService,
            EthGetTransactionCount.class);
}
 
Example #9
Source File: RawTransactionManager.java    From web3j with Apache License 2.0 5 votes vote down vote up
protected BigInteger getNonce() throws IOException {
    EthGetTransactionCount ethGetTransactionCount =
            web3j.ethGetTransactionCount(
                            credentials.getAddress(), DefaultBlockParameterName.PENDING)
                    .send();

    return ethGetTransactionCount.getTransactionCount();
}
 
Example #10
Source File: JsonRpc2_0Besu.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Override
public Request<?, EthGetTransactionCount> privGetTransactionCount(
        final String address, final Base64String privacyGroupId) {
    return new Request<>(
            "priv_getTransactionCount",
            Arrays.asList(address, privacyGroupId.toString()),
            web3jService,
            EthGetTransactionCount.class);
}
 
Example #11
Source File: CoreIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEthGetTransactionCount() throws Exception {
    EthGetTransactionCount ethGetTransactionCount =
            web3j.ethGetTransactionCount(
                            config.validAccount(), DefaultBlockParameter.valueOf("latest"))
                    .send();
    assertTrue(ethGetTransactionCount.getTransactionCount().signum() == 1);
}
 
Example #12
Source File: CreateRawTransactionIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
BigInteger getNonce(String address) throws Exception {
    EthGetTransactionCount ethGetTransactionCount =
            web3j.ethGetTransactionCount(address, DefaultBlockParameterName.LATEST)
                    .sendAsync()
                    .get();

    return ethGetTransactionCount.getTransactionCount();
}
 
Example #13
Source File: Scenario.java    From web3j with Apache License 2.0 5 votes vote down vote up
BigInteger getNonce(String address) throws Exception {
    EthGetTransactionCount ethGetTransactionCount =
            web3j.ethGetTransactionCount(address, DefaultBlockParameterName.LATEST)
                    .sendAsync()
                    .get();

    return ethGetTransactionCount.getTransactionCount();
}
 
Example #14
Source File: Web3jUtils.java    From web3j_demo with Apache License 2.0 5 votes vote down vote up
/**
 * Return the nonce (tx count) for the specified address.
 */
public static BigInteger getNonce(Web3j web3j, String address) throws InterruptedException, ExecutionException {
	EthGetTransactionCount ethGetTransactionCount = 
			web3j.ethGetTransactionCount(address, DefaultBlockParameterName.LATEST).sendAsync().get();

	return ethGetTransactionCount.getTransactionCount();
}
 
Example #15
Source File: EthereumUtils.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
public static BigInteger getNextNonce(String address,
                                      Web3j web3j) throws Exception {
    EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
            address,
            DefaultBlockParameterName.LATEST).sendAsync().get();
    return ethGetTransactionCount.getTransactionCount();
}
 
Example #16
Source File: EthereumNetworkBase.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
@Override
public Single<BigInteger> getLastTransactionNonce(Web3j web3j, String walletAddress)
{
    return Single.fromCallable(() -> {
        EthGetTransactionCount ethGetTransactionCount = web3j
                .ethGetTransactionCount(walletAddress, DefaultBlockParameterName.PENDING)
                .send();
        return ethGetTransactionCount.getTransactionCount();
    });
}
 
Example #17
Source File: EthGetTransactionCountTransaction.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public BigInteger execute(final NodeRequests node) {
  try {
    EthGetTransactionCount result =
        node.eth()
            .ethGetTransactionCount(accountAddress, DefaultBlockParameterName.LATEST)
            .send();
    assertThat(result).isNotNull();
    return result.getTransactionCount();
  } catch (final IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #18
Source File: EthereumNetworkManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private BigInteger getNonce() {
    BigInteger nonce = null;
    try {
        EthGetTransactionCount txCount = web3jConnection.ethGetTransactionCount(walletManager.getWalletFriendlyAddress(),
                DefaultBlockParameterName.LATEST).send();
        nonce = txCount.getTransactionCount();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return nonce;
}
 
Example #19
Source File: EthereumNetworkManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private BigInteger getNonce() {
    BigInteger nonce = null;
    try {
        EthGetTransactionCount txCount = web3jConnection.ethGetTransactionCount(walletManager.getWalletFriendlyAddress(),
                DefaultBlockParameterName.LATEST).send();
        nonce = txCount.getTransactionCount();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return nonce;
}
 
Example #20
Source File: BaseIntegrationTest.java    From eventeum with Apache License 2.0 5 votes vote down vote up
protected BigInteger getNonce() throws ExecutionException, InterruptedException {
    final EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
            CREDS.getAddress(), DefaultBlockParameterName.LATEST).sendAsync().get();

    final BigInteger nonce = ethGetTransactionCount.getTransactionCount();

    return nonce;
}
 
Example #21
Source File: BaseIntegrationTest.java    From eventeum with Apache License 2.0 5 votes vote down vote up
protected String sendTransaction() throws ExecutionException, InterruptedException, IOException {
    EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
            CREDS.getAddress(), DefaultBlockParameterName.LATEST).sendAsync().get();
    BigInteger nonce = ethGetTransactionCount.getTransactionCount();

    final Transaction tx = Transaction.createEtherTransaction(CREDS.getAddress(),
            nonce, GAS_PRICE, GAS_LIMIT, ZERO_ADDRESS, BigInteger.ONE);

    EthSendTransaction response = web3j.ethSendTransaction(tx).send();

    return response.getTransactionHash();
}
 
Example #22
Source File: ResponseTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testEthGetTransactionCount() {
    buildResponse(
            "{\n"
                    + "  \"id\":1,\n"
                    + "  \"jsonrpc\": \"2.0\",\n"
                    + "  \"result\": \"0x1\"\n"
                    + "}"
    );

    EthGetTransactionCount ethGetTransactionCount =
            deserialiseResponse((EthGetTransactionCount.class));
    assertThat(ethGetTransactionCount.getTransactionCount(), equalTo(BigInteger.valueOf(1L)));
}
 
Example #23
Source File: ManagedTransactionTester.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
void prepareNonceRequest() throws IOException {
    EthGetTransactionCount ethGetTransactionCount = new EthGetTransactionCount();
    ethGetTransactionCount.setResult("0x1");

    Request<?, EthGetTransactionCount> transactionCountRequest = mock(Request.class);
    when(transactionCountRequest.send())
            .thenReturn(ethGetTransactionCount);
    when(web3j.ethGetTransactionCount(SampleKeys.ADDRESS, DefaultBlockParameterName.PENDING))
            .thenReturn((Request) transactionCountRequest);
}
 
Example #24
Source File: EthereumNetworkManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private BigInteger getNonce() {
    BigInteger nonce = null;
    try {
        EthGetTransactionCount txCount = web3jConnection.ethGetTransactionCount(walletManager.getWalletFriendlyAddress(),
                DefaultBlockParameterName.LATEST).send();
        nonce = txCount.getTransactionCount();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return nonce;
}
 
Example #25
Source File: JsonRpc2_0Web3j.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Request<?, EthGetTransactionCount> ethGetTransactionCount(
        String address, DefaultBlockParameter defaultBlockParameter) {
    return new Request<>(
            "eth_getTransactionCount",
            Arrays.asList(address, defaultBlockParameter.getValue()),
            web3jService,
            EthGetTransactionCount.class);
}
 
Example #26
Source File: CoreIT.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testEthGetTransactionCount() throws Exception {
    EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
            config.validAccount(),
            DefaultBlockParameter.valueOf("latest")).send();
    assertTrue(ethGetTransactionCount.getTransactionCount().signum() == 1);
}
 
Example #27
Source File: Besu.java    From web3j with Apache License 2.0 4 votes vote down vote up
Request<?, EthGetTransactionCount> privGetTransactionCount(
final String address, final Base64String privacyGroupId);
 
Example #28
Source File: Scenario.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
BigInteger getNonce(String address) throws Exception {
    EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
            address, DefaultBlockParameterName.LATEST).sendAsync().get();

    return ethGetTransactionCount.getTransactionCount();
}
 
Example #29
Source File: CreateRawTransactionIT.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
BigInteger getNonce(String address) throws Exception {
    EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
            address, DefaultBlockParameterName.LATEST).sendAsync().get();

    return ethGetTransactionCount.getTransactionCount();
}
 
Example #30
Source File: RawTransactionManager.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
protected BigInteger getNonce() throws IOException {
    EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
            credentials.getAddress(), DefaultBlockParameterName.PENDING).send();

    return ethGetTransactionCount.getTransactionCount();
}