Java Code Examples for org.web3j.utils.Convert#toWei()

The following examples show how to use org.web3j.utils.Convert#toWei() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Transfer.java    From web3j with Apache License 2.0 6 votes vote down vote up
private TransactionReceipt send(
        String toAddress,
        BigDecimal value,
        Convert.Unit unit,
        BigInteger gasPrice,
        BigInteger gasLimit)
        throws IOException, InterruptedException, TransactionException {

    BigDecimal weiValue = Convert.toWei(value, unit);
    if (!Numeric.isIntegerValue(weiValue)) {
        throw new UnsupportedOperationException(
                "Non decimal Wei value provided: "
                        + value
                        + " "
                        + unit.toString()
                        + " = "
                        + weiValue
                        + " Wei");
    }

    String resolvedAddress = ensResolver.resolve(toAddress);
    return send(resolvedAddress, "", weiValue.toBigIntegerExact(), gasPrice, gasLimit);
}
 
Example 2
Source File: Transfer.java    From web3j with Apache License 2.0 6 votes vote down vote up
private TransactionReceipt sendEIP1559(
        String toAddress,
        BigDecimal value,
        Convert.Unit unit,
        BigInteger gasLimit,
        BigInteger gasPremium,
        BigInteger feeCap)
        throws IOException, InterruptedException, TransactionException {

    BigDecimal weiValue = Convert.toWei(value, unit);
    if (!Numeric.isIntegerValue(weiValue)) {
        throw new UnsupportedOperationException(
                "Non decimal Wei value provided: "
                        + value
                        + " "
                        + unit.toString()
                        + " = "
                        + weiValue
                        + " Wei");
    }

    String resolvedAddress = ensResolver.resolve(toAddress);
    return sendEIP1559(
            resolvedAddress, "", weiValue.toBigIntegerExact(), gasLimit, gasPremium, feeCap);
}
 
Example 3
Source File: WalletSendFunds.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
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)
            && !EnsResolver.isValidEnsName(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());
}
 
Example 4
Source File: Transfer.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private TransactionReceipt send(
        String toAddress, BigDecimal value, Convert.Unit unit, BigInteger gasPrice,
        BigInteger gasLimit) throws IOException, InterruptedException,
        TransactionException {

    BigDecimal weiValue = Convert.toWei(value, unit);
    if (!Numeric.isIntegerValue(weiValue)) {
        throw new UnsupportedOperationException(
                "Non decimal Wei value provided: " + value + " " + unit.toString()
                        + " = " + weiValue + " Wei");
    }

    String resolvedAddress = ensResolver.resolve(toAddress);
    return send(resolvedAddress, "", weiValue.toBigIntegerExact(), gasPrice, gasLimit);
}
 
Example 5
Source File: MyAddressActivity.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
@Override
public void amountChanged(String newAmount)
{
    if (token != null && newAmount != null && newAmount.length() > 0 && Character.isDigit(newAmount.charAt(0)))
    {
        //generate payment request link
        //EIP681 format
        BigDecimal weiAmount = Convert.toWei(newAmount.replace(",", "."), Convert.Unit.ETHER);
        System.out.println("AMT: " + weiAmount.toString());
        EIP681Request request;
        String eip681String;
        if (token.isEthereum())
        {
            request = new EIP681Request(displayAddress, networkInfo.chainId, weiAmount);
            eip681String = request.generateRequest();
        }
        else if (token.isERC20())
        {
            weiAmount = token.getCorrectedAmount(newAmount);
            request = new EIP681Request(displayAddress, token.getAddress(), networkInfo.chainId, weiAmount);
            eip681String = request.generateERC20Request();
        }
        else
        {
            return;
        }
        qrImageView.setImageBitmap(QRUtils.createQRImage(this, eip681String, screenWidth));
    }
}
 
Example 6
Source File: TransactionManager.java    From BitcoinWallet with MIT License 3 votes vote down vote up
/**
 * ETH 转账离线签名
 *
 * @param to         转入的钱包地址
 * @param nonce      以太坊nonce
 * @param gasPrice   gasPrice
 * @param gasLimit   gasLimit
 * @param amount     转账的eth数量
 * @param walletfile 钱包对象
 * @param password   密码
 * @return 签名data
 */
public String signedEthTransactionData(String to, BigInteger nonce, BigInteger gasPrice,
                                       BigInteger gasLimit, BigDecimal amount, WalletFile walletfile,
                                       String password) throws Exception {
    // 把十进制的转换成ETH的Wei, 1ETH = 10^18 Wei
    BigDecimal amountInWei = Convert.toWei(amount.toString(), Convert.Unit.ETHER);
    RawTransaction rawTransaction =
            RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, to,
                    amountInWei.toBigInteger());
    return signData(rawTransaction, walletfile, password);
}