org.web3j.protocol.core.Request.sendAsync()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(162)

本文整理了Java中org.web3j.protocol.core.Request.sendAsync方法的一些代码示例,展示了Request.sendAsync的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Request.sendAsync方法的具体详情如下:
包路径:org.web3j.protocol.core.Request
类名称:Request
方法名:sendAsync

Request.sendAsync介绍

暂无

代码示例

代码示例来源:origin: web3j/web3j

  1. private Optional<TransactionReceipt> sendTransactionReceiptRequest(
  2. String transactionHash) throws Exception {
  3. EthGetTransactionReceipt transactionReceipt =
  4. web3j.ethGetTransactionReceipt(transactionHash).sendAsync().get();
  5. return transactionReceipt.getTransactionReceipt();
  6. }

代码示例来源:origin: web3j/web3j

  1. BigInteger getNonce(String address) throws Exception {
  2. EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
  3. address, DefaultBlockParameterName.LATEST).sendAsync().get();
  4. return ethGetTransactionCount.getTransactionCount();
  5. }

代码示例来源:origin: web3j/web3j

  1. BigInteger getNonce(String address) throws Exception {
  2. EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
  3. address, DefaultBlockParameterName.LATEST).sendAsync().get();
  4. return ethGetTransactionCount.getTransactionCount();
  5. }
  6. }

代码示例来源:origin: web3j/web3j

  1. private BigInteger estimateGas(String encodedFunction) throws Exception {
  2. EthEstimateGas ethEstimateGas = web3j.ethEstimateGas(
  3. Transaction.createEthCallTransaction(ALICE.getAddress(), null, encodedFunction))
  4. .sendAsync().get();
  5. // this was coming back as 50,000,000 which is > the block gas limit of 4,712,388
  6. // see eth.getBlock("latest")
  7. return ethEstimateGas.getAmountUsed().divide(BigInteger.valueOf(100));
  8. }

代码示例来源:origin: web3j/web3j

  1. boolean unlockAccount() throws Exception {
  2. PersonalUnlockAccount personalUnlockAccount =
  3. web3j.personalUnlockAccount(
  4. ALICE.getAddress(), WALLET_PASSWORD, ACCOUNT_UNLOCK_DURATION)
  5. .sendAsync().get();
  6. return personalUnlockAccount.accountUnlocked();
  7. }

代码示例来源:origin: web3j/web3j

  1. Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get();
  2. if (web3ClientVersion.hasError()) {
  3. exitError("Unable to process response from client: "

代码示例来源:origin: web3j/web3j

  1. @Test(expected = RuntimeException.class)
  2. @SuppressWarnings("unchecked")
  3. public void testInvalidTransactionResponse() throws Throwable {
  4. prepareNonceRequest();
  5. EthSendTransaction ethSendTransaction = new EthSendTransaction();
  6. ethSendTransaction.setError(new Response.Error(1, "Invalid transaction"));
  7. Request<?, EthSendTransaction> rawTransactionRequest = mock(Request.class);
  8. when(rawTransactionRequest.sendAsync()).thenReturn(Async.run(() -> ethSendTransaction));
  9. when(web3j.ethSendRawTransaction(any(String.class)))
  10. .thenReturn((Request) rawTransactionRequest);
  11. testErrorScenario();
  12. }

代码示例来源:origin: web3j/web3j

  1. @Test(expected = RuntimeException.class)
  2. @SuppressWarnings("unchecked")
  3. public void testInvalidTransactionReceipt() throws Throwable {
  4. prepareNonceRequest();
  5. prepareTransactionRequest();
  6. EthGetTransactionReceipt ethGetTransactionReceipt = new EthGetTransactionReceipt();
  7. ethGetTransactionReceipt.setError(new Response.Error(1, "Invalid transaction receipt"));
  8. Request<?, EthGetTransactionReceipt> getTransactionReceiptRequest = mock(Request.class);
  9. when(getTransactionReceiptRequest.sendAsync())
  10. .thenReturn(Async.run(() -> ethGetTransactionReceipt));
  11. when(web3j.ethGetTransactionReceipt(TRANSACTION_HASH))
  12. .thenReturn((Request) getTransactionReceiptRequest);
  13. testErrorScenario();
  14. }

代码示例来源:origin: web3j/web3j

  1. private String callSmartContractFunction(
  2. Function function, String contractAddress) throws Exception {
  3. String encodedFunction = FunctionEncoder.encode(function);
  4. org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall(
  5. Transaction.createEthCallTransaction(
  6. ALICE.getAddress(), contractAddress, encodedFunction),
  7. DefaultBlockParameterName.LATEST)
  8. .sendAsync().get();
  9. return response.getValue();
  10. }

代码示例来源:origin: web3j/web3j

  1. private String callSmartContractFunction(
  2. Function function, String contractAddress) throws Exception {
  3. String encodedFunction = FunctionEncoder.encode(function);
  4. org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall(
  5. Transaction.createEthCallTransaction(
  6. ALICE.getAddress(), contractAddress, encodedFunction),
  7. DefaultBlockParameterName.LATEST)
  8. .sendAsync().get();
  9. return response.getValue();
  10. }

代码示例来源:origin: web3j/web3j

  1. private String callSmartContractFunction(
  2. Function function, String contractAddress) throws Exception {
  3. String encodedFunction = FunctionEncoder.encode(function);
  4. org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall(
  5. Transaction.createEthCallTransaction(
  6. ALICE.getAddress(), contractAddress, encodedFunction),
  7. DefaultBlockParameterName.LATEST)
  8. .sendAsync().get();
  9. return response.getValue();
  10. }
  11. }

代码示例来源:origin: web3j/web3j

  1. private String sendTransaction() throws Exception {
  2. BigInteger nonce = getNonce(ALICE.getAddress());
  3. Transaction transaction = Transaction.createContractTransaction(
  4. ALICE.getAddress(),
  5. nonce,
  6. GAS_PRICE,
  7. GAS_LIMIT,
  8. BigInteger.ZERO,
  9. getFibonacciSolidityBinary());
  10. org.web3j.protocol.core.methods.response.EthSendTransaction
  11. transactionResponse = web3j.ethSendTransaction(transaction)
  12. .sendAsync().get();
  13. return transactionResponse.getTransactionHash();
  14. }

代码示例来源:origin: web3j/web3j

  1. private String sendTransaction(
  2. Credentials credentials, String contractAddress, BigInteger gas,
  3. String encodedFunction) throws Exception {
  4. BigInteger nonce = getNonce(credentials.getAddress());
  5. Transaction transaction = Transaction.createFunctionCallTransaction(
  6. credentials.getAddress(), nonce, Transaction.DEFAULT_GAS, gas, contractAddress,
  7. encodedFunction);
  8. org.web3j.protocol.core.methods.response.EthSendTransaction transactionResponse =
  9. web3j.ethSendTransaction(transaction).sendAsync().get();
  10. assertFalse(transactionResponse.hasError());
  11. return transactionResponse.getTransactionHash();
  12. }

代码示例来源:origin: web3j/web3j

  1. private String sendCreateContractTransaction() throws Exception {
  2. BigInteger nonce = getNonce(ALICE.getAddress());
  3. String encodedConstructor =
  4. FunctionEncoder.encodeConstructor(Collections.singletonList(new Utf8String(VALUE)));
  5. Transaction transaction = Transaction.createContractTransaction(
  6. ALICE.getAddress(),
  7. nonce,
  8. GAS_PRICE,
  9. GAS_LIMIT,
  10. BigInteger.ZERO,
  11. getGreeterSolidityBinary() + encodedConstructor);
  12. org.web3j.protocol.core.methods.response.EthSendTransaction
  13. transactionResponse = web3j.ethSendTransaction(transaction)
  14. .sendAsync().get();
  15. return transactionResponse.getTransactionHash();
  16. }

代码示例来源:origin: web3j/web3j

  1. @Test
  2. public void testTransferEther() throws Exception {
  3. unlockAccount();
  4. BigInteger nonce = getNonce(ALICE.getAddress());
  5. BigInteger value = Convert.toWei("0.5", Convert.Unit.ETHER).toBigInteger();
  6. Transaction transaction = Transaction.createEtherTransaction(
  7. ALICE.getAddress(), nonce, GAS_PRICE, GAS_LIMIT, BOB.getAddress(), value);
  8. EthSendTransaction ethSendTransaction =
  9. web3j.ethSendTransaction(transaction).sendAsync().get();
  10. String transactionHash = ethSendTransaction.getTransactionHash();
  11. assertFalse(transactionHash.isEmpty());
  12. TransactionReceipt transactionReceipt =
  13. waitForTransactionReceipt(transactionHash);
  14. assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));
  15. }

代码示例来源:origin: web3j/web3j

  1. @Test
  2. public void testTransferEther() throws Exception {
  3. BigInteger nonce = getNonce(ALICE.getAddress());
  4. RawTransaction rawTransaction = createEtherTransaction(
  5. nonce, BOB.getAddress());
  6. byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE);
  7. String hexValue = Numeric.toHexString(signedMessage);
  8. EthSendTransaction ethSendTransaction =
  9. web3j.ethSendRawTransaction(hexValue).sendAsync().get();
  10. String transactionHash = ethSendTransaction.getTransactionHash();
  11. assertFalse(transactionHash.isEmpty());
  12. TransactionReceipt transactionReceipt =
  13. waitForTransactionReceipt(transactionHash);
  14. assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));
  15. }

代码示例来源:origin: web3j/web3j

  1. @Test
  2. public void testDeploySmartContract() throws Exception {
  3. BigInteger nonce = getNonce(ALICE.getAddress());
  4. RawTransaction rawTransaction = createSmartContractTransaction(nonce);
  5. byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE);
  6. String hexValue = Numeric.toHexString(signedMessage);
  7. EthSendTransaction ethSendTransaction =
  8. web3j.ethSendRawTransaction(hexValue).sendAsync().get();
  9. String transactionHash = ethSendTransaction.getTransactionHash();
  10. assertFalse(transactionHash.isEmpty());
  11. TransactionReceipt transactionReceipt =
  12. waitForTransactionReceipt(transactionHash);
  13. assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));
  14. assertFalse("Contract execution ran out of gas",
  15. rawTransaction.getGasLimit().equals(transactionReceipt.getGasUsed()));
  16. }

代码示例来源:origin: web3j/web3j

  1. private String execute(
  2. Credentials credentials, Function function, String contractAddress) throws Exception {
  3. BigInteger nonce = getNonce(credentials.getAddress());
  4. String encodedFunction = FunctionEncoder.encode(function);
  5. RawTransaction rawTransaction = RawTransaction.createTransaction(
  6. nonce,
  7. GAS_PRICE,
  8. GAS_LIMIT,
  9. contractAddress,
  10. encodedFunction);
  11. byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
  12. String hexValue = Numeric.toHexString(signedMessage);
  13. EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue)
  14. .sendAsync().get();
  15. return transactionResponse.getTransactionHash();
  16. }

代码示例来源:origin: web3j/web3j

  1. @Test
  2. public void testSignTransaction() throws Exception {
  3. boolean accountUnlocked = unlockAccount();
  4. assertTrue(accountUnlocked);
  5. RawTransaction rawTransaction = createTransaction();
  6. byte[] encoded = TransactionEncoder.encode(rawTransaction);
  7. byte[] hashed = Hash.sha3(encoded);
  8. EthSign ethSign = web3j.ethSign(ALICE.getAddress(), Numeric.toHexString(hashed))
  9. .sendAsync().get();
  10. String signature = ethSign.getSignature();
  11. assertNotNull(signature);
  12. assertFalse(signature.isEmpty());
  13. }

代码示例来源:origin: web3j/web3j

  1. private String sendCreateContractTransaction(
  2. Credentials credentials, BigInteger initialSupply) throws Exception {
  3. BigInteger nonce = getNonce(credentials.getAddress());
  4. String encodedConstructor =
  5. FunctionEncoder.encodeConstructor(
  6. Arrays.asList(
  7. new Uint256(initialSupply),
  8. new Utf8String("web3j tokens"),
  9. new Uint8(BigInteger.TEN),
  10. new Utf8String("w3j$")));
  11. RawTransaction rawTransaction = RawTransaction.createContractTransaction(
  12. nonce,
  13. GAS_PRICE,
  14. GAS_LIMIT,
  15. BigInteger.ZERO,
  16. getHumanStandardTokenBinary() + encodedConstructor);
  17. byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
  18. String hexValue = Numeric.toHexString(signedMessage);
  19. EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue)
  20. .sendAsync().get();
  21. return transactionResponse.getTransactionHash();
  22. }

相关文章