• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java EthSendTransaction类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.web3j.protocol.core.methods.response.EthSendTransaction的典型用法代码示例。如果您正苦于以下问题:Java EthSendTransaction类的具体用法?Java EthSendTransaction怎么用?Java EthSendTransaction使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



EthSendTransaction类属于org.web3j.protocol.core.methods.response包,在下文中一共展示了EthSendTransaction类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: createTransaction

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
@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());
}
 
开发者ID:TrustWallet,项目名称:trust-wallet-android,代码行数:22,代码来源:TransactionRepository.java


示例2: testCreateAccountFromScratch

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
@Test
public void testCreateAccountFromScratch() throws Exception {
	
	// create new private/public key pair
	ECKeyPair keyPair = Keys.createEcKeyPair();
	
	BigInteger publicKey = keyPair.getPublicKey();
	String publicKeyHex = Numeric.toHexStringWithPrefix(publicKey);
	
	BigInteger privateKey = keyPair.getPrivateKey();
	String privateKeyHex = Numeric.toHexStringWithPrefix(privateKey);
	
	// create credentials + address from private/public key pair
	Credentials credentials = Credentials.create(new ECKeyPair(privateKey, publicKey));
	String address = credentials.getAddress();
	
	// print resulting data of new account
	System.out.println("private key: '" + privateKeyHex + "'");
	System.out.println("public key: '" + publicKeyHex + "'");
	System.out.println("address: '" + address + "'\n");
	
	// test (1) check if it's possible to transfer funds to new address
	BigInteger amountWei = Convert.toWei("0.131313", Convert.Unit.ETHER).toBigInteger();
	transferWei(getCoinbase(), address, amountWei);

	BigInteger balanceWei = getBalanceWei(address);
	BigInteger nonce = getNonce(address);
	
	assertEquals("Unexpected nonce for 'to' address", BigInteger.ZERO, nonce);
	assertEquals("Unexpected balance for 'to' address", amountWei, balanceWei);

	// test (2) funds can be transferred out of the newly created account
	BigInteger txFees = Web3jConstants.GAS_LIMIT_ETHER_TX.multiply(Web3jConstants.GAS_PRICE);
	RawTransaction txRaw = RawTransaction
			.createEtherTransaction(
					nonce, 
					Web3jConstants.GAS_PRICE, 
					Web3jConstants.GAS_LIMIT_ETHER_TX, 
					getCoinbase(), 
					amountWei.subtract(txFees));

	// sign raw transaction using the sender's credentials
	byte[] txSignedBytes = TransactionEncoder.signMessage(txRaw, credentials);
	String txSigned = Numeric.toHexString(txSignedBytes);

	// send the signed transaction to the ethereum client
	EthSendTransaction ethSendTx = web3j
			.ethSendRawTransaction(txSigned)
			.sendAsync()
			.get();

	Error error = ethSendTx.getError();
	String txHash = ethSendTx.getTransactionHash();
	assertNull(error);		
	assertFalse(txHash.isEmpty());
	
	waitForReceipt(txHash);

	assertEquals("Unexpected nonce for 'to' address", BigInteger.ONE, getNonce(address));
	assertTrue("Balance for 'from' address too large: " + getBalanceWei(address), getBalanceWei(address).compareTo(txFees) < 0);
}
 
开发者ID:matthiaszimmermann,项目名称:web3j_demo,代码行数:62,代码来源:CreateAccountTest.java


示例3: execute

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
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();
}
 
开发者ID:web3j,项目名称:web3j,代码行数:22,代码来源:HumanStandardTokenIT.java


示例4: doSendTransaction

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
public SentTransaction doSendTransaction(FunctionCall call, BigInteger value, TransactionParams params) throws IOException {
    RawTransaction rawTransaction = RawTransaction.createTransaction(
        params.getNonce(),
        params.getGasPrice(),
        params.getGasLimit(),
        call.contractAddress,
        value,
        call.data
    );

    boolean success = false;
    try {
        EthSendTransaction transaction = signAndSend(rawTransaction);
        CallUtil.checkError(transaction);
        success = true;
        return new SentTransaction(transaction.getTransactionHash(), params);
    } finally {
        if (!success) this.nonce = BigInteger.valueOf(-1);
    }
}
 
开发者ID:papyrusglobal,项目名称:state-channels,代码行数:21,代码来源:ThreadsafeTransactionManager.java


示例5: transferFromCoinbaseAndWait

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
/**
 * Transfers the specified amount of Wei from the coinbase to the specified account.
 * The method waits for the transfer to complete using method {@link waitForReceipt}.  
 */
public static TransactionReceipt transferFromCoinbaseAndWait(Web3j web3j, String to, BigInteger amountWei) 
		throws Exception 
{
	String coinbase = getCoinbase(web3j).getResult();
	BigInteger nonce = getNonce(web3j, coinbase);
	// this is a contract method call -> gas limit higher than simple fund transfer
	BigInteger gasLimit = Web3jConstants.GAS_LIMIT_ETHER_TX.multiply(BigInteger.valueOf(2)); 
	Transaction transaction = Transaction.createEtherTransaction(
			coinbase, 
			nonce, 
			Web3jConstants.GAS_PRICE, 
			gasLimit, 
			to, 
			amountWei);

	EthSendTransaction ethSendTransaction = web3j
			.ethSendTransaction(transaction)
			.sendAsync()
			.get();

	String txHash = ethSendTransaction.getTransactionHash();
	
	return waitForReceipt(web3j, txHash);
}
 
开发者ID:matthiaszimmermann,项目名称:web3j_demo,代码行数:29,代码来源:Web3jUtils.java


示例6: send

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
public Transaction send(Transaction tx) {
  LOG.info("Sending TX ...");
  EthSendTransaction ethSendTransaction = null;

  try {
    ethSendTransaction = getWeb3j().ethSendRawTransaction(tx.getSignedContent()).sendAsync().get();
  }
  catch (Exception e) {
    throw new ProcessingException("Failed to send transaction " + tx.getSignedContent(), e);
  }

  checkResponseFromSending(ethSendTransaction);

  tx.setSent(new Date());
  tx.setError(ethSendTransaction.getError());
  tx.setHash(ethSendTransaction.getTransactionHash());
  tx.setStatus(Transaction.PENDING);
  LOG.info("Successfully sent TX. Hash: " + tx.getHash());

  save(tx);

  return tx;
}
 
开发者ID:BSI-Business-Systems-Integration-AG,项目名称:trading-network,代码行数:24,代码来源:EthereumService.java


示例7: testTransferEther

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
@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));
}
 
开发者ID:web3j,项目名称:web3j,代码行数:21,代码来源:CreateRawTransactionIT.java


示例8: testDeploySmartContract

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
@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()));
}
 
开发者ID:web3j,项目名称:web3j,代码行数:23,代码来源:CreateRawTransactionIT.java


示例9: sendTransaction

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
@Override
public EthSendTransaction sendTransaction(
        BigInteger gasPrice, BigInteger gasLimit, String to,
        String data, BigInteger value) throws IOException {

    BigInteger nonce = getNonce();

    RawTransaction rawTransaction = RawTransaction.createTransaction(
            nonce,
            gasPrice,
            gasLimit,
            to,
            value,
            data);

    return signAndSend(rawTransaction);
}
 
开发者ID:web3j,项目名称:web3j,代码行数:18,代码来源:RawTransactionManager.java


示例10: deployContract

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
public Address deployContract(TransactionManager transactionManager, String binary, Map<String, Address> libraries, Type... constructorArgs) throws IOException, InterruptedException, TransactionException {
    ContractLinker linker = new ContractLinker(binary);
    if (libraries != null && !libraries.isEmpty()) {
        libraries.forEach(linker::link);
    }
    linker.assertLinked();
    String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(constructorArgs));
    String data = linker.getBinary() + encodedConstructor;

    EthSendTransaction transactionResponse = transactionManager.sendTransaction(
        properties.getGasPrice(), properties.getGasLimit(), null, data, BigInteger.ZERO);

    if (transactionResponse.hasError()) {
        throw new RuntimeException("Error processing transaction request: "
            + transactionResponse.getError().getMessage());
    }

    String transactionHash = transactionResponse.getTransactionHash();

    Optional<TransactionReceipt> receiptOptional =
        sendTransactionReceiptRequest(transactionHash, web3j);

    long millis = properties.getSleep().toMillis();
    int attempts = properties.getAttempts();
    for (int i = 0; i < attempts; i++) {
        if (!receiptOptional.isPresent()) {
            Thread.sleep(millis);
            receiptOptional = sendTransactionReceiptRequest(transactionHash, web3j);
        } else {
            String contractAddress = receiptOptional.get().getContractAddress();
            return new Address(contractAddress);
        }
    }
    throw new TransactionException("Transaction receipt was not generated after " + (millis * attempts / 1000)
        + " seconds for transaction: " + transactionHash);
}
 
开发者ID:papyrusglobal,项目名称:state-channels,代码行数:37,代码来源:DeployService.java


示例11: sendTransaction

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
@Override
public synchronized EthSendTransaction sendTransaction(
    BigInteger gasPrice, BigInteger gasLimit, String to,
    String data, BigInteger value) throws IOException {

    FunctionCall call = FunctionCall.fromData(to, data);
    TransactionParams params = detectCurrentParams(gasPrice, gasLimit, call, value);
    SentTransaction transaction = doSendTransaction(call, value, params);
    EthSendTransaction tr = new EthSendTransaction();
    tr.setResult(transaction.getHash());
    return tr;
}
 
开发者ID:papyrusglobal,项目名称:state-channels,代码行数:13,代码来源:ThreadsafeTransactionManager.java


示例12: transferWei

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
String transferWei(String from, String to, BigInteger amountWei) throws Exception {
	BigInteger nonce = getNonce(from);
	Transaction transaction = Transaction.createEtherTransaction(
			from, nonce, Web3jConstants.GAS_PRICE, Web3jConstants.GAS_LIMIT_ETHER_TX, to, amountWei);

	EthSendTransaction ethSendTransaction = web3j.ethSendTransaction(transaction).sendAsync().get();
	System.out.println("transferEther. nonce: " + nonce + " amount: " + amountWei + " to: " + to);

	String txHash = ethSendTransaction.getTransactionHash(); 
	waitForReceipt(txHash);
	
	return txHash;
}
 
开发者ID:matthiaszimmermann,项目名称:web3j_demo,代码行数:14,代码来源:AbstractEthereumTest.java


示例13: sendTransaction

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
@Override
public EthSendTransaction sendTransaction(
        BigInteger gasPrice, BigInteger gasLimit, String to,
        String data, BigInteger value)
        throws IOException {

    PrivateTransaction transaction = new PrivateTransaction(
            fromAddress, null, gasLimit, to, value, data, privateFor);

    return quorum.ethSendTransaction(transaction).send();
}
 
开发者ID:web3j,项目名称:quorum,代码行数:12,代码来源:ClientTransactionManager.java


示例14: ethSendTransaction

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
@Override
public Request<?, EthSendTransaction> ethSendTransaction(
        PrivateTransaction transaction) {
    return new Request<>(
            "eth_sendTransaction",
            Collections.singletonList(transaction),
            web3jService,
            EthSendTransaction.class);
}
 
开发者ID:web3j,项目名称:quorum,代码行数:10,代码来源:JsonRpc2_0Quorum.java


示例15: testEthSendTransaction

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
@Test
public void testEthSendTransaction() {
    buildResponse("{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"0x0d9e7e34fd4db216a3f66981a467d9d990954e6ed3128aff4ec51a50fa175663\"}");

    EthSendTransaction transaction = deserialiseResponse(EthSendTransaction.class);
    assertThat(transaction.getTransactionHash(), is("0x0d9e7e34fd4db216a3f66981a467d9d990954e6ed3128aff4ec51a50fa175663"));
}
 
开发者ID:web3j,项目名称:quorum,代码行数:8,代码来源:ResponseTest.java


示例16: transfer

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
public static String transfer(String from, String to, int amount, Unit unit) throws InterruptedException, ExecutionException {
	BigInteger nonce = getNonce(from);
	BigInteger weiAmount = Convert.toWei(BigDecimal.valueOf(amount), unit).toBigInteger();
	Transaction transaction = new Transaction(from, nonce, GAS_PRICE_DEFAULT, GAS_LIMIT_DEFAULT, to, weiAmount, null);
	EthSendTransaction txRequest = getWeb3j().ethSendTransaction(transaction).sendAsync().get();
	return txRequest.getTransactionHash();
}
 
开发者ID:BSI-Business-Systems-Integration-AG,项目名称:trading-network,代码行数:8,代码来源:Web3jHelper.java


示例17: transferEther

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
private String transferEther(String from, String to, BigInteger amount) throws Exception {
  BigInteger nonce = getNonce(from);

  org.web3j.protocol.core.methods.request.Transaction transaction = new org.web3j.protocol.core.methods.request.Transaction(from, nonce, GAS_PRICE_DEFAULT, GAS_LIMIT_DEFAULT, to, amount, null);
  EthSendTransaction txRequest = getWeb3j().ethSendTransaction(transaction).sendAsync().get();
  String txHash = txRequest.getTransactionHash();

  System.out.println("tx hash: " + txHash);
  System.out.println(String.format("amount: %d from: %s to %s", amount, from, to));

  return txHash;
}
 
开发者ID:BSI-Business-Systems-Integration-AG,项目名称:trading-network,代码行数:13,代码来源:EthereumService.java


示例18: checkResponseFromSending

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
private void checkResponseFromSending(EthSendTransaction response) {
  Error error = response.getError();
  String result = response.getResult();

  if (error != null) {
    String message = "Failed to send transaction: " + error.getMessage();
    LOG.error(message);
    throw new ProcessingException(message);
  }
  else {
    LOG.info("result:" + result);
  }
}
 
开发者ID:BSI-Business-Systems-Integration-AG,项目名称:trading-network,代码行数:14,代码来源:EthereumService.java


示例19: transferEther

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
public static CompletableFuture<EthSendTransaction> transferEther(String from, String to, double ethAmount) {
    return web3j.ethGetTransactionCount(from, DefaultBlockParameterName.LATEST).sendAsync()
            .thenCompose(transactionCount -> parity.ethSendTransaction(
                            Transaction.createEtherTransaction(from, transactionCount.getTransactionCount(),
                                    GAS_PRICE, GAS_LIMIT, to, toWei(ethAmount))).sendAsync()
            );
}
 
开发者ID:papyrusglobal,项目名称:smartcontracts,代码行数:8,代码来源:PapyrusUtils.java


示例20: sendCreateContractTransaction

import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入依赖的package包/类
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();
}
 
开发者ID:web3j,项目名称:web3j,代码行数:28,代码来源:HumanStandardTokenIT.java



注:本文中的org.web3j.protocol.core.methods.response.EthSendTransaction类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java PrefetchCompleteEvent类代码示例发布时间:2022-05-22
下一篇:
Java JsonSchema类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap