org.web3j.protocol.http.HttpService Java Examples
The following examples show how to use
org.web3j.protocol.http.HttpService.
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: CreateTransactionInteract.java From Upchain-wallet with GNU Affero General Public License v3.0 | 7 votes |
public Single<String> createEthTransaction(ETHWallet from, String to, BigInteger amount, BigInteger gasPrice, BigInteger gasLimit, String password) { final Web3j web3j = Web3j.build(new HttpService(networkRepository.getDefaultNetwork().rpcServerUrl)); return networkRepository.getLastTransactionNonce(web3j, from.address) .flatMap(nonce -> Single.fromCallable( () -> { Credentials credentials = WalletUtils.loadCredentials(password, from.getKeystorePath()); RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, to, amount); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send(); return ethSendTransaction.getTransactionHash(); } ).subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread())); }
Example #2
Source File: EthereumWeb3jProvider.java From presto-ethereum with Apache License 2.0 | 6 votes |
@Inject public EthereumWeb3jProvider(EthereumConnectorConfig config) { if (config.getEthereumJsonRpc() == null && config.getEthereumIpc() == null && config.getInfuraRpc() == null) { this.web3j = Web3j.build(new HttpService(EthereumConnectorConfig.DEFAULT_JSON_RPC)); } else if (config.getEthereumJsonRpc() != null && config.getEthereumIpc() == null && config.getInfuraRpc() == null) { this.web3j = Web3j.build(new HttpService(config.getEthereumJsonRpc())); } else if (config.getEthereumJsonRpc() == null && config.getEthereumIpc() != null && config.getInfuraRpc() == null) { this.web3j = Web3j.build(new UnixIpcService(config.getEthereumIpc())); } else if (config.getEthereumJsonRpc() == null && config.getEthereumIpc() == null && config.getInfuraRpc() != null) { this.web3j = Web3j.build(new InfuraHttpService(config.getInfuraRpc())); } else { throw new IllegalArgumentException("More than 1 Ethereum service providers found"); } }
Example #3
Source File: QuorumIT.java From web3j-quorum with Apache License 2.0 | 6 votes |
private void runPrivateGreeterTest(Node sourceNode, Node destNode, String requestId) throws Exception { Quorum quorum = Quorum.build(new HttpService(sourceNode.getUrl())); ClientTransactionManager transactionManager = new ClientTransactionManager( quorum, sourceNode.getAddress(), sourceNode.getPublicKeys().get(0), destNode.getPublicKeys()); String greeting = "Hello Quorum world! [" + requestId + "]"; Greeter contract = Greeter.deploy(quorum, transactionManager, GAS_PRICE, GAS_LIMIT, greeting).send(); assertThat(contract.greet().send(), is(greeting)); }
Example #4
Source File: Template.java From tutorials with MIT License | 6 votes |
private void deployContract() throws Exception{ Web3j web3j = Web3j.build(new HttpService("https://rinkeby.infura.io/<your_token>")); Credentials credentials = WalletUtils.loadCredentials( "<password>", "/path/to/<walletfile>"); Greeting contract = Greeting.deploy( web3j, credentials, ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT, "Hello blockchain world!").send(); String contractAddress = contract.getContractAddress(); l.debug("Smart contract deployed to address "+ contractAddress); l.debug("Value stored in remote smart contract: "+ contract.greet().send()); TransactionReceipt transactionReceipt = contract.setGreeting("Well hello again").send(); l.debug("New value stored in remote smart contract: "+ contract.greet().send()); }
Example #5
Source File: TransactionHandler.java From alpha-wallet-android with MIT License | 6 votes |
public TransactionHandler(int networkId) { String nodeURL = EthereumNetworkBase.getNetworkByChain(networkId).rpcServerUrl; OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.connectTimeout(20, TimeUnit.SECONDS); builder.readTimeout(20, TimeUnit.SECONDS); HttpService service = new HttpService(nodeURL, builder.build(), false); mWeb3 = Web3j.build(service); try { Web3ClientVersion web3ClientVersion = mWeb3.web3ClientVersion().sendAsync().get(); System.out.println(web3ClientVersion.getWeb3ClientVersion()); } catch (Exception e) { e.printStackTrace(); } }
Example #6
Source File: CreateTransactionInteract.java From Upchain-wallet with GNU Affero General Public License v3.0 | 6 votes |
public Single<String> createTransaction(ETHWallet from, String toAddress, BigInteger subunitAmount, BigInteger gasPrice, BigInteger gasLimit, String data, String password) { final Web3j web3j = Web3j.build(new HttpService(networkRepository.getDefaultNetwork().rpcServerUrl)); return networkRepository.getLastTransactionNonce(web3j, from.address) .flatMap(nonce -> getRawTransaction(nonce, gasPrice, gasLimit,toAddress, subunitAmount, data)) .flatMap(rawTx -> signEncodeRawTransaction(rawTx, password, from, networkRepository.getDefaultNetwork().chainId)) .flatMap(signedMessage -> Single.fromCallable( () -> { EthSendTransaction raw = web3j .ethSendRawTransaction(Numeric.toHexString(signedMessage)) .send(); if (raw.hasError()) { throw new Exception(raw.getError().getMessage()); } return raw.getTransactionHash(); })).subscribeOn(Schedulers.io()); }
Example #7
Source File: CreateTransactionInteract.java From Upchain-wallet with GNU Affero General Public License v3.0 | 6 votes |
public Single<String> createTransaction(ETHWallet from, BigInteger gasPrice, BigInteger gasLimit, String data, String password) { final Web3j web3j = Web3j.build(new HttpService(networkRepository.getDefaultNetwork().rpcServerUrl)); return networkRepository.getLastTransactionNonce(web3j, from.address) .flatMap(nonce -> getRawTransaction(nonce, gasPrice, gasLimit, BigInteger.ZERO, data)) .flatMap(rawTx -> signEncodeRawTransaction(rawTx, password, from, networkRepository.getDefaultNetwork().chainId)) .flatMap(signedMessage -> Single.fromCallable( () -> { EthSendTransaction raw = web3j .ethSendRawTransaction(Numeric.toHexString(signedMessage)) .send(); if (raw.hasError()) { throw new Exception(raw.getError().getMessage()); } return raw.getTransactionHash(); })).subscribeOn(Schedulers.io()); }
Example #8
Source File: TransactionRepository.java From trust-wallet-android-source with GNU General Public License v3.0 | 6 votes |
@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 #9
Source File: EthereumNetworkManager.java From guarda-android-wallets with GNU General Public License v3.0 | 6 votes |
private void provideNativeNodeConnection(final ConnectionCallback callback) { try { HttpService service = new HttpService(NODE_ADDRESS); web3jConnection = Web3jFactory.build(service); connectionAvailable = true; } catch (NetworkOnMainThreadException e) { e.printStackTrace(); } new Handler().postDelayed(new Runnable() { @Override public void run() { if (callback != null) { callback.onFinish(); } } }, CONNECTION_RETRY_PAUSE_MILLISECONDS); }
Example #10
Source File: FunctionWrappersIT.java From etherscan-explorer with GNU General Public License v3.0 | 6 votes |
@Test public void testFibonacciNotify() throws Exception { Fibonacci fibonacci = Fibonacci.load( "0x3c05b2564139fb55820b18b72e94b2178eaace7d", Web3j.build(new HttpService()), ALICE, GAS_PRICE, GAS_LIMIT); TransactionReceipt transactionReceipt = fibonacci.fibonacciNotify( BigInteger.valueOf(15)).send(); Fibonacci.NotifyEventResponse result = fibonacci.getNotifyEvents(transactionReceipt).get(0); assertThat(result.input, equalTo(new Uint256(BigInteger.valueOf(15)))); assertThat(result.result, equalTo(new Uint256(BigInteger.valueOf(610)))); }
Example #11
Source File: TransactionHandler.java From alpha-wallet-android with MIT License | 6 votes |
public TransactionHandler(int networkId) { String nodeURL = EthRPCNodes.getNodeURLByNetworkId(networkId); OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.connectTimeout(20, TimeUnit.SECONDS); builder.readTimeout(20, TimeUnit.SECONDS); HttpService service = new HttpService(nodeURL, builder.build(), false); mWeb3 = Web3j.build(service); try { Web3ClientVersion web3ClientVersion = mWeb3.web3ClientVersion().sendAsync().get(); System.out.println(web3ClientVersion.getWeb3ClientVersion()); } catch (Exception e) { e.printStackTrace(); } }
Example #12
Source File: BatchTester.java From web3j with Apache License 2.0 | 5 votes |
@BeforeEach public void setUp() { interceptor = new HttpInterceptor(); httpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build(); httpService = new HttpService(httpClient); initWeb3Client(httpService); }
Example #13
Source File: BaseContractTest.java From client-sdk-java with Apache License 2.0 | 5 votes |
@Before public void init() { credentials = Credentials.create(privateKey); address = credentials.getAddress(); web3jService = new HttpService(nodeUrl); web3j = Web3j.build(web3jService); transactionManager = new RawTransactionManager(web3j, credentials, chainId); gasProvider = new ContractGasProvider(GAS_PRICE, GAS_LIMIT); }
Example #14
Source File: FetchGasSettingsInteract.java From Upchain-wallet with GNU Affero General Public License v3.0 | 5 votes |
public BigInteger getTransactionGasLimit(Transaction transaction) { final Web3j web3j = Web3j.build(new HttpService(networkRepository.getDefaultNetwork().rpcServerUrl)); try { EthEstimateGas ethEstimateGas = web3j.ethEstimateGas(transaction).send(); if (ethEstimateGas.hasError()){ throw new RuntimeException(ethEstimateGas.getError().getMessage()); } return ethEstimateGas.getAmountUsed(); } catch (IOException e) { throw new RuntimeException("net error"); } }
Example #15
Source File: BesuNode.java From ethsigner with Apache License 2.0 | 5 votes |
@Override public void start() { LOG.info("Starting Besu Docker container: {}", besuContainerId); docker.startContainerCmd(besuContainerId).exec(); LOG.info("Querying for the Docker dynamically allocated RPC port numbers"); final InspectContainerResponse containerResponse = docker.inspectContainerCmd(besuContainerId).exec(); final Ports ports = containerResponse.getNetworkSettings().getPorts(); final int httpRpcPort = httpRpcPort(ports); final int wsRpcPort = wsRpcPort(ports); LOG.info("Http RPC port: {}, Web Socket RPC port: {}", httpRpcPort, wsRpcPort); final String httpRpcUrl = url(httpRpcPort); LOG.info("Besu Web3j service targeting: {} ", httpRpcUrl); final HttpService web3jHttpService = new HttpService(httpRpcUrl); this.jsonRpc = new JsonRpc2_0Web3j(web3jHttpService, pollingInterval, Async.defaultExecutorService()); final RawJsonRpcRequestFactory requestFactory = new RawJsonRpcRequestFactory(web3jHttpService); final JsonRpc2_0Besu besuJsonRpc = new JsonRpc2_0Besu(web3jHttpService); final Eth eth = new Eth(jsonRpc); final Besu besu = new Besu(besuJsonRpc); final Eea eea = new Eea(requestFactory); this.accounts = new Accounts(eth); this.publicContracts = new PublicContracts(eth); this.privateContracts = new PrivateContracts(besu, eea); this.transactions = new Transactions(eth); this.ports = new NodePorts(httpRpcPort, wsRpcPort); }
Example #16
Source File: BDSMicroRaidenjSingleton.java From asf-sdk with GNU General Public License v3.0 | 5 votes |
public static MicroRaidenBDS create(boolean debug) { Web3j web3j = Web3jFactory.build(new HttpService("https://ropsten.infura.io/1YsvKO0VH5aBopMYJzcy")); AsfWeb3jImpl asfWeb3j = new AsfWeb3jImpl(web3j); TransactionSender transactionSender = new DefaultTransactionSender(web3j, () -> BigInteger.valueOf(50000000000L), new DefaultNonceObtainer(asfWeb3j), new DefaultGasLimitEstimator(web3j)); return new DefaultMicroRaidenBDS( new DefaultMicroRaidenClient(channelManagerAddr, BigInteger.valueOf(13), new DefaultChannelBlockObtainer(web3j, 3, 1500), new MicroRaidenContract(channelManagerAddr, tokenAddr, transactionSender)), BDSMicroRaidenApi.create(debug)); }
Example #17
Source File: EnsIT.java From web3j with Apache License 2.0 | 5 votes |
@Test public void testEns() throws Exception { Web3j web3j = Web3j.build(new HttpService()); EnsResolver ensResolver = new EnsResolver(web3j); assertEquals( ensResolver.resolve("web3j.test"), ("0x19e03255f667bdfd50a32722df860b1eeaf4d635")); }
Example #18
Source File: WalletSendFunds.java From client-sdk-java with Apache License 2.0 | 5 votes |
private Web3j getEthereumClient() { String clientAddress = console.readLine( "Please confirm address of running Ethereum client you wish to send " + "the transfer request to [" + HttpService.DEFAULT_URL + "]: ") .trim(); Web3j web3j; if (clientAddress.equals("")) { web3j = Web3j.build(new HttpService()); } else { web3j = Web3j.build(new HttpService(clientAddress)); } try { Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get(); if (web3ClientVersion.hasError()) { exitError("Unable to process response from client: " + web3ClientVersion.getError()); } else { console.printf("Connected successfully to client: %s%n", web3ClientVersion.getWeb3ClientVersion()); return web3j; } } catch (InterruptedException | ExecutionException e) { exitError("Problem encountered verifying client: " + e.getMessage()); } throw new RuntimeException("Application exit failure"); }
Example #19
Source File: BesuOnChainPrivacyIntegrationTest.java From web3j with Apache License 2.0 | 5 votes |
@BeforeAll public static void setUpOnce() { nodeAlice = Besu.build(new HttpService("http://localhost:20000")); nodeBob = Besu.build(new HttpService("http://localhost:20002")); nodeCharlie = Besu.build(new HttpService("http://localhost:20004")); processor = new PollingPrivateTransactionReceiptProcessor(nodeAlice, 1000, 15); }
Example #20
Source File: FunctionWrappersIT.java From web3j with Apache License 2.0 | 5 votes |
@Test public void testFibonacci() throws Exception { Fibonacci fibonacci = Fibonacci.load( "0x3c05b2564139fb55820b18b72e94b2178eaace7d", Web3j.build(new HttpService()), ALICE, STATIC_GAS_PROVIDER); BigInteger result = fibonacci.fibonacci(BigInteger.valueOf(10)).send(); assertEquals(result, (BigInteger.valueOf(55))); }
Example #21
Source File: WalletRepository.java From ETHWallet with GNU General Public License v3.0 | 5 votes |
public Single<BigInteger> balanceInWei(Wallet wallet) { Log.d("aaron", "address:" + wallet.getAddress()); return Single.fromCallable(() -> Web3jFactory .build(new HttpService(networkRepository.getDefaultNetwork().rpcServerUrl, httpClient, false)) .ethGetBalance(wallet.getAddress(), DefaultBlockParameterName.LATEST) .send() .getBalance()) .subscribeOn(Schedulers.io()); }
Example #22
Source File: EnsIT.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
@Test public void testEns() throws Exception { Web3j web3j = Web3j.build(new HttpService()); EnsResolver ensResolver = new EnsResolver(web3j); assertThat(ensResolver.resolve("web3j.test"), is("0x19e03255f667bdfd50a32722df860b1eeaf4d635")); }
Example #23
Source File: JsonRpc2_0ApiTest.java From client-sdk-java with Apache License 2.0 | 5 votes |
@Before public void init() { chainId = 100L; // nodeUrl = "http://10.10.8.21:8804"; nodeUrl = "http://10.1.1.1:8801"; // privateKey = "11e20dc277fafc4bc008521adda4b79c2a9e403131798c94eacb071005d43532"; privateKey = "3e9516bc43b09dd2754040ad228b9a6c6253c87aa6895318438c7c46002050a6"; credentials = Credentials.create(privateKey); address = credentials.getAddress(); web3jService = new HttpService(nodeUrl); web3j = Web3j.build(web3jService); }
Example #24
Source File: FunctionWrappersIT.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
@Test public void testFibonacci() throws Exception { Fibonacci fibonacci = Fibonacci.load( "0x3c05b2564139fb55820b18b72e94b2178eaace7d", Web3j.build(new HttpService()), ALICE, GAS_PRICE, GAS_LIMIT); BigInteger result = fibonacci.fibonacci(BigInteger.valueOf(10)).send(); assertThat(result, equalTo(BigInteger.valueOf(55))); }
Example #25
Source File: BaseIntegrationTest.java From eventeum with Apache License 2.0 | 5 votes |
protected static void startParity() { parityContainer = new FixedHostPortGenericContainer("kauriorg/parity-docker:latest"); parityContainer.waitingFor(Wait.forListeningPort()); parityContainer.withFixedExposedPort(8545, 8545); parityContainer.withFixedExposedPort(8546, 8546); parityContainer.withFileSystemBind(PARITY_VOLUME_PATH, "/root/.local/share/io.parity.ethereum/", BindMode.READ_WRITE); parityContainer.addEnv("NO_BLOCKS", "true"); parityContainer.start(); waitForParityToStart(10000, Web3j.build(new HttpService("http://localhost:8545"))); }
Example #26
Source File: WalletSendFunds.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
private Web3j getEthereumClient() { String clientAddress = console.readLine( "Please confirm address of running Ethereum client you wish to send " + "the transfer request to [" + HttpService.DEFAULT_URL + "]: ") .trim(); Web3j web3j; if (clientAddress.equals("")) { web3j = Web3j.build(new HttpService()); } else if (clientAddress.contains("infura.io")) { web3j = Web3j.build(new InfuraHttpService(clientAddress)); } else { web3j = Web3j.build(new HttpService(clientAddress)); } try { Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get(); if (web3ClientVersion.hasError()) { exitError("Unable to process response from client: " + web3ClientVersion.getError()); } else { console.printf("Connected successfully to client: %s%n", web3ClientVersion.getWeb3ClientVersion()); return web3j; } } catch (InterruptedException | ExecutionException e) { exitError("Problem encountered verifying client: " + e.getMessage()); } throw new RuntimeException("Application exit failure"); }
Example #27
Source File: BaseIntegrationTest.java From eventeum with Apache License 2.0 | 5 votes |
@Before public void setUp() throws Exception { initRestTemplate(); this.web3j = Web3j.build(new HttpService("http://localhost:8545")); this.admin = Admin.build(new HttpService("http://localhost:8545")); this.web3j.ethSendTransaction(Transaction.createEtherTransaction( this.web3j.ethAccounts().send().getAccounts().get(0), this.web3j.ethGetTransactionCount( this.web3j.ethAccounts().send().getAccounts().get(0), DefaultBlockParameterName.fromString("latest") ).send().getTransactionCount(), BigInteger.valueOf(2000), BigInteger.valueOf(6721975), CREDS.getAddress(), new BigInteger("9460000000000000000")) ).send(); dummyEventFilterId = UUID.randomUUID().toString(); dummyEventNotOrderedFilterId = UUID.randomUUID().toString(); clearMessages(); }
Example #28
Source File: RequestTester.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
@Before public void setUp() { requestInterceptor = new RequestInterceptor(); httpClient = new OkHttpClient.Builder() .addInterceptor(requestInterceptor) .build(); httpService = new HttpService(httpClient); initWeb3Client(httpService); }
Example #29
Source File: RequestTester.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
protected void verifyResult(String expected) throws Exception { RequestBody requestBody = requestInterceptor.getRequestBody(); assertNotNull(requestBody); assertThat(requestBody.contentType(), is(HttpService.JSON_MEDIA_TYPE)); Buffer buffer = new Buffer(); requestBody.writeTo(buffer); assertThat(replaceRequestId(buffer.readUtf8()), is(replaceRequestId(expected))); }
Example #30
Source File: Signer.java From ethsigner with Apache License 2.0 | 5 votes |
public void start() { LOG.info("Starting EthSigner"); runner.start(PROCESS_NAME); final String httpJsonRpcUrl = getUrl(); LOG.info("Http requests being submitted to : {} ", httpJsonRpcUrl); final OkHttpClient httpClient = OkHttpClientHelpers.createOkHttpClient(clientTlsConfig); final HttpService web3jHttpService = new HttpService(httpJsonRpcUrl, httpClient); this.jsonRpc = new JsonRpc2_0Web3j( web3jHttpService, pollingInterval.toMillis(), Async.defaultExecutorService()); final JsonRpc2_0Besu besuJsonRpc = new JsonRpc2_0Besu(web3jHttpService); final Eth eth = new Eth(jsonRpc); final RawJsonRpcRequestFactory requestFactory = new RawJsonRpcRequestFactory(web3jHttpService); this.transactions = new Transactions(eth); final Besu besu = new Besu(besuJsonRpc); final Eea eea = new Eea(requestFactory); this.publicContracts = new PublicContracts(eth); this.privateContracts = new PrivateContracts(besu, eea); this.accounts = new Accounts(eth); this.rawJsonRpcRequests = new RawJsonRpcRequests(web3jHttpService, requestFactory); this.rawHttpRequests = new HttpRequest(httpJsonRpcUrl, httpClient); }