当前位置: 首页>>代码示例>>Java>>正文


Java Transaction.createEtherTransaction方法代码示例

本文整理汇总了Java中org.web3j.protocol.core.methods.request.Transaction.createEtherTransaction方法的典型用法代码示例。如果您正苦于以下问题:Java Transaction.createEtherTransaction方法的具体用法?Java Transaction.createEtherTransaction怎么用?Java Transaction.createEtherTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.web3j.protocol.core.methods.request.Transaction的用法示例。


在下文中一共展示了Transaction.createEtherTransaction方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: transferFromCoinbaseAndWait

import org.web3j.protocol.core.methods.request.Transaction; //导入方法依赖的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

示例2: transferWei

import org.web3j.protocol.core.methods.request.Transaction; //导入方法依赖的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

示例3: demoTransfer

import org.web3j.protocol.core.methods.request.Transaction; //导入方法依赖的package包/类
/**
 * Implementation of the Ethers transfer.
 * <ol>
 *   <li>Get nonce for for the sending account</li>
 *   <li>Create the transaction object</li>
 *   <li>Send the transaction to the network</li>
 *   <li>Wait for the confirmation</li>
 * </ol>
 */
void demoTransfer(String fromAddress, String toAddress, BigInteger amountWei)
		throws Exception
{
	System.out.println("Accounts[1] (to address) " + toAddress + "\n" + 
			"Balance before Tx: " + Web3jUtils.getBalanceEther(web3j, toAddress) + "\n");

	System.out.println("Transfer " + Web3jUtils.weiToEther(amountWei) + " Ether to account");

	// step 1: get the nonce (tx count for sending address)
	EthGetTransactionCount transactionCount = web3j
			.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.LATEST)
			.sendAsync()
			.get();

	BigInteger nonce = transactionCount.getTransactionCount();
	System.out.println("Nonce for sending address (coinbase): " + nonce);

	// step 2: create the transaction object
	Transaction transaction = Transaction
			.createEtherTransaction(
					fromAddress, 
					nonce, 
					Web3jConstants.GAS_PRICE, 
					Web3jConstants.GAS_LIMIT_ETHER_TX, 
					toAddress, 
					amountWei);

	// step 3: send the tx to the network
	EthSendTransaction response = web3j
			.ethSendTransaction(transaction)
			.sendAsync()
			.get();

	String txHash = response.getTransactionHash();		
	System.out.println("Tx hash: " + txHash);

	// step 4: wait for the confirmation of the network
	TransactionReceipt receipt = Web3jUtils.waitForReceipt(web3j, txHash);
	
	BigInteger gasUsed = receipt.getCumulativeGasUsed();
	System.out.println("Tx cost: " + gasUsed + " Gas (" + 
			Web3jUtils.weiToEther(gasUsed.multiply(Web3jConstants.GAS_PRICE)) +" Ether)\n");

	System.out.println("Balance after Tx: " + Web3jUtils.getBalanceEther(web3j, toAddress));
}
 
开发者ID:matthiaszimmermann,项目名称:web3j_demo,代码行数:55,代码来源:TransferDemo.java

示例4: testCreateAndSendTransaction

import org.web3j.protocol.core.methods.request.Transaction; //导入方法依赖的package包/类
/**
 * Ether transfer tests using methods {@link Transaction#createEtherTransaction()}, and {@link Web3j#ethSendTransaction()}.
 * Sending account needs to be unlocked for this to work.   
 */
@Test
public void testCreateAndSendTransaction() throws Exception {

	String from = getCoinbase();
	BigInteger nonce = getNonce(from);
	String to = Alice.ADDRESS;
	BigInteger amountWei = Convert.toWei("0.456", Convert.Unit.ETHER).toBigInteger();

	// this is the method to test here
	Transaction transaction = Transaction
			.createEtherTransaction(
					from, 
					nonce, 
					Web3jConstants.GAS_PRICE, 
					Web3jConstants.GAS_LIMIT_ETHER_TX, 
					to, 
					amountWei);
	

	// record account balances before the transfer
	BigInteger fromBalanceBefore = getBalanceWei(from);
	BigInteger toBalanceBefore = getBalanceWei(to);

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

	String txHash = ethSendTx.getTransactionHash();
	assertFalse(txHash.isEmpty());
	
	TransactionReceipt txReceipt = waitForReceipt(txHash);
	BigInteger txFee = txReceipt.getCumulativeGasUsed().multiply(Web3jConstants.GAS_PRICE);

	// coinbase might have gotten additional funds from mining
	BigInteger fromMinimumBalanceExpected = fromBalanceBefore.subtract(amountWei.add(txFee));
	BigInteger fromBalanceActual = getBalanceWei(from);
	BigInteger fromBalanceDelta = fromBalanceActual.subtract(fromMinimumBalanceExpected);
	
	System.out.println("testCreateAndSendTransaction balance difference=" + fromBalanceDelta + " likely cause block reward (5 Ethers) and tx fees (" + txFee + " Weis)");
	assertTrue("Unexected balance for 'from' address. difference=" + fromBalanceDelta, fromBalanceDelta.signum() >= 0);
	assertEquals("Unexected balance for 'to' address", toBalanceBefore.add(amountWei), getBalanceWei(to));		
}
 
开发者ID:matthiaszimmermann,项目名称:web3j_demo,代码行数:49,代码来源:TransferEtherTest.java

示例5: testTransferEther

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


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