org.bcos.web3j.console.WalletSendFunds Maven / Gradle / Ivy
package org.bcos.web3j.console;
import java.io.File;
import java.math.BigDecimal;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.bcos.web3j.crypto.Credentials;
import org.bcos.web3j.crypto.WalletUtils;
import org.bcos.web3j.protocol.Web3j;
import org.bcos.web3j.protocol.core.methods.response.TransactionReceipt;
import org.bcos.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.bcos.web3j.protocol.exceptions.TransactionTimeoutException;
import org.bcos.web3j.protocol.http.HttpService;
import org.bcos.web3j.protocol.infura.InfuraHttpService;
import org.bcos.web3j.tx.Transfer;
import org.bcos.web3j.utils.Convert;
import static org.bcos.web3j.utils.Console.exitError;
/**
* Simple class for creating a wallet file.
*/
public class WalletSendFunds extends WalletManager {
private static final String USAGE = "send ";
public static void main(String[] args) {
if (args.length != 2) {
exitError(USAGE);
} else {
new WalletSendFunds().run(args[0], args[1]);
}
}
private void run(String walletFileLocation, String destinationAddress) {
File walletFile = new File(walletFileLocation);
Credentials credentials = getCredentials(walletFile);
console.printf("Wallet for address " + credentials.getAddress() + " loaded\n");
if (!WalletUtils.isValidAddress(destinationAddress)) {
exitError("Invalid destination address specified");
}
Web3j web3j = getEthereumClient();
BigDecimal amountToTransfer = getAmountToTransfer();
Convert.Unit transferUnit = getTransferUnit();
BigDecimal amountInWei = Convert.toWei(amountToTransfer, transferUnit);
confirmTransfer(amountToTransfer, transferUnit, amountInWei, destinationAddress);
TransactionReceipt transactionReceipt = performTransfer(
web3j, destinationAddress, credentials, amountInWei);
console.printf("Funds have been successfully transferred from %s to %s%n"
+ "Transaction hash: %s%nMined block number: %s%n",
credentials.getAddress(),
destinationAddress,
transactionReceipt.getTransactionHash(),
transactionReceipt.getBlockNumber());
}
private BigDecimal getAmountToTransfer() {
String amount = console.readLine("What amound would you like to transfer "
+ "(please enter a numeric value): ")
.trim();
try {
return new BigDecimal(amount);
} catch (NumberFormatException e) {
exitError("Invalid amount specified");
}
throw new RuntimeException("Application exit failure");
}
private Convert.Unit getTransferUnit() {
String unit = console.readLine("Please specify the unit (ether, wei, ...) [ether]: ")
.trim();
Convert.Unit transferUnit;
if (unit.equals("")) {
transferUnit = Convert.Unit.ETHER;
} else {
transferUnit = Convert.Unit.fromString(unit.toLowerCase());
}
return transferUnit;
}
private void confirmTransfer(
BigDecimal amountToTransfer, Convert.Unit transferUnit, BigDecimal amountInWei,
String destinationAddress) {
console.printf("Please confim that you wish to transfer %s %s (%s %s) to address %s%n",
amountToTransfer.stripTrailingZeros().toPlainString(), transferUnit,
amountInWei.stripTrailingZeros().toPlainString(),
Convert.Unit.WEI, destinationAddress);
String confirm = console.readLine("Please type 'yes' to proceed: ").trim();
if (!confirm.toLowerCase().equals("yes")) {
exitError("OK, some other time perhaps...");
}
}
private TransactionReceipt performTransfer(
Web3j web3j, String destinationAddress, Credentials credentials,
BigDecimal amountInWei) {
console.printf("Commencing transfer (this may take a few minutes) ");
try {
Future future = Transfer.sendFundsAsync(
web3j, credentials, destinationAddress, amountInWei, Convert.Unit.WEI);
while (!future.isDone()) {
console.printf(".");
Thread.sleep(500);
}
console.printf("$%n%n");
return future.get();
} catch (InterruptedException | ExecutionException | TransactionTimeoutException e) {
exitError("Problem encountered transferring funds: \n" + e.getMessage());
}
throw new RuntimeException("Application exit failure");
}
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");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy