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

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

本文整理了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

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

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

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

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

BigInteger getNonce(String address) throws Exception {
    EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
        address, DefaultBlockParameterName.LATEST).sendAsync().get();

    return ethGetTransactionCount.getTransactionCount();
  }
}

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

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

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

boolean unlockAccount() throws Exception {
  PersonalUnlockAccount personalUnlockAccount =
      web3j.personalUnlockAccount(
          ALICE.getAddress(), WALLET_PASSWORD, ACCOUNT_UNLOCK_DURATION)
          .sendAsync().get();
  return personalUnlockAccount.accountUnlocked();
}

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

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

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

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

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

@Test(expected = RuntimeException.class)
@SuppressWarnings("unchecked")
public void testInvalidTransactionReceipt() throws Throwable {
  prepareNonceRequest();
  prepareTransactionRequest();
  EthGetTransactionReceipt ethGetTransactionReceipt = new EthGetTransactionReceipt();
  ethGetTransactionReceipt.setError(new Response.Error(1, "Invalid transaction receipt"));
  Request<?, EthGetTransactionReceipt> getTransactionReceiptRequest = mock(Request.class);
  when(getTransactionReceiptRequest.sendAsync())
      .thenReturn(Async.run(() -> ethGetTransactionReceipt));
  when(web3j.ethGetTransactionReceipt(TRANSACTION_HASH))
      .thenReturn((Request) getTransactionReceiptRequest);
  testErrorScenario();
}

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

private String callSmartContractFunction(
    Function function, String contractAddress) throws Exception {
  String encodedFunction = FunctionEncoder.encode(function);
  org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall(
      Transaction.createEthCallTransaction(
          ALICE.getAddress(), contractAddress, encodedFunction),
      DefaultBlockParameterName.LATEST)
      .sendAsync().get();
  return response.getValue();
}

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

private String callSmartContractFunction(
    Function function, String contractAddress) throws Exception {
  String encodedFunction = FunctionEncoder.encode(function);
  org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall(
      Transaction.createEthCallTransaction(
          ALICE.getAddress(), contractAddress, encodedFunction),
      DefaultBlockParameterName.LATEST)
      .sendAsync().get();
  return response.getValue();
}

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

private String callSmartContractFunction(
      Function function, String contractAddress) throws Exception {

    String encodedFunction = FunctionEncoder.encode(function);

    org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall(
        Transaction.createEthCallTransaction(
            ALICE.getAddress(), contractAddress, encodedFunction),
        DefaultBlockParameterName.LATEST)
        .sendAsync().get();

    return response.getValue();
  }
}

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

private String sendTransaction() throws Exception {
  BigInteger nonce = getNonce(ALICE.getAddress());
  Transaction transaction = Transaction.createContractTransaction(
      ALICE.getAddress(),
      nonce,
      GAS_PRICE,
      GAS_LIMIT,
      BigInteger.ZERO,
      getFibonacciSolidityBinary());
  org.web3j.protocol.core.methods.response.EthSendTransaction
      transactionResponse = web3j.ethSendTransaction(transaction)
      .sendAsync().get();
  return transactionResponse.getTransactionHash();
}

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

private String sendTransaction(
    Credentials credentials, String contractAddress, BigInteger gas,
    String encodedFunction) throws Exception {
  BigInteger nonce = getNonce(credentials.getAddress());
  Transaction transaction = Transaction.createFunctionCallTransaction(
      credentials.getAddress(), nonce, Transaction.DEFAULT_GAS, gas, contractAddress,
      encodedFunction);
  org.web3j.protocol.core.methods.response.EthSendTransaction transactionResponse =
      web3j.ethSendTransaction(transaction).sendAsync().get();
  assertFalse(transactionResponse.hasError());
  return transactionResponse.getTransactionHash();
}

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

private String sendCreateContractTransaction() throws Exception {
  BigInteger nonce = getNonce(ALICE.getAddress());
  String encodedConstructor =
      FunctionEncoder.encodeConstructor(Collections.singletonList(new Utf8String(VALUE)));
  Transaction transaction = Transaction.createContractTransaction(
      ALICE.getAddress(),
      nonce,
      GAS_PRICE,
      GAS_LIMIT,
      BigInteger.ZERO,
      getGreeterSolidityBinary() + encodedConstructor);
  org.web3j.protocol.core.methods.response.EthSendTransaction
      transactionResponse = web3j.ethSendTransaction(transaction)
      .sendAsync().get();
  return transactionResponse.getTransactionHash();
}

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

@Test
public void testTransferEther() throws Exception {
  unlockAccount();
  BigInteger nonce = getNonce(ALICE.getAddress());
  BigInteger value = Convert.toWei("0.5", Convert.Unit.ETHER).toBigInteger();
  Transaction transaction = Transaction.createEtherTransaction(
      ALICE.getAddress(), nonce, GAS_PRICE, GAS_LIMIT, BOB.getAddress(), value);
  EthSendTransaction ethSendTransaction =
      web3j.ethSendTransaction(transaction).sendAsync().get();
  String transactionHash = ethSendTransaction.getTransactionHash();
  assertFalse(transactionHash.isEmpty());
  TransactionReceipt transactionReceipt =
      waitForTransactionReceipt(transactionHash);
  assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));
}

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

@Test
public void testTransferEther() throws Exception {
  BigInteger nonce = getNonce(ALICE.getAddress());
  RawTransaction rawTransaction = createEtherTransaction(
      nonce, BOB.getAddress());
  byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE);
  String hexValue = Numeric.toHexString(signedMessage);
  EthSendTransaction ethSendTransaction =
      web3j.ethSendRawTransaction(hexValue).sendAsync().get();
  String transactionHash = ethSendTransaction.getTransactionHash();
  assertFalse(transactionHash.isEmpty());
  TransactionReceipt transactionReceipt =
      waitForTransactionReceipt(transactionHash);
  assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));
}

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

@Test
public void testDeploySmartContract() throws Exception {
  BigInteger nonce = getNonce(ALICE.getAddress());
  RawTransaction rawTransaction = createSmartContractTransaction(nonce);
  byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE);
  String hexValue = Numeric.toHexString(signedMessage);
  EthSendTransaction ethSendTransaction =
      web3j.ethSendRawTransaction(hexValue).sendAsync().get();
  String transactionHash = ethSendTransaction.getTransactionHash();
  assertFalse(transactionHash.isEmpty());
  TransactionReceipt transactionReceipt =
      waitForTransactionReceipt(transactionHash);
  assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));
  assertFalse("Contract execution ran out of gas",
      rawTransaction.getGasLimit().equals(transactionReceipt.getGasUsed()));
}

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

private String execute(
    Credentials credentials, Function function, String contractAddress) throws Exception {
  BigInteger nonce = getNonce(credentials.getAddress());
  String encodedFunction = FunctionEncoder.encode(function);
  RawTransaction rawTransaction = RawTransaction.createTransaction(
      nonce,
      GAS_PRICE,
      GAS_LIMIT,
      contractAddress,
      encodedFunction);
  byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
  String hexValue = Numeric.toHexString(signedMessage);
  EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue)
      .sendAsync().get();
  return transactionResponse.getTransactionHash();
}

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

@Test
public void testSignTransaction() throws Exception {
  boolean accountUnlocked = unlockAccount();
  assertTrue(accountUnlocked);
  RawTransaction rawTransaction = createTransaction();
  byte[] encoded = TransactionEncoder.encode(rawTransaction);
  byte[] hashed = Hash.sha3(encoded);
  EthSign ethSign = web3j.ethSign(ALICE.getAddress(), Numeric.toHexString(hashed))
      .sendAsync().get();
  String signature = ethSign.getSignature();
  assertNotNull(signature);
  assertFalse(signature.isEmpty());
}

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

private String sendCreateContractTransaction(
    Credentials credentials, BigInteger initialSupply) throws Exception {
  BigInteger nonce = getNonce(credentials.getAddress());
  String encodedConstructor =
      FunctionEncoder.encodeConstructor(
          Arrays.asList(
              new Uint256(initialSupply),
              new Utf8String("web3j tokens"),
              new Uint8(BigInteger.TEN),
              new Utf8String("w3j$")));
  RawTransaction rawTransaction = RawTransaction.createContractTransaction(
      nonce,
      GAS_PRICE,
      GAS_LIMIT,
      BigInteger.ZERO,
      getHumanStandardTokenBinary() + encodedConstructor);
  byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
  String hexValue = Numeric.toHexString(signedMessage);
  EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue)
      .sendAsync().get();
  return transactionResponse.getTransactionHash();
}

相关文章

微信公众号

最新文章

更多