package leigh.chain.etherium;

import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Convert;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.concurrent.ExecutionException;

public class CheckBalanceApp {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(CheckBalanceApp.class);

    public static void main(String args[]) throws IOException, ExecutionException, InterruptedException {
        Web3j web3 = Web3j.build(new HttpService("http://localhost:8545"));
        // "0x21dc9adacb308530daa08c5258ae86a54e06aece"
        // "0x400a72097cdeeA17525D9FE9cC975D23B8997ea3"

        EthGetBalance balanceWei = web3.ethGetBalance("0x21dc9adacb308530daa08c5258ae86a54e06aece",
                DefaultBlockParameterName.LATEST).send();
        BigDecimal balanceInEther = Convert.fromWei(balanceWei.getBalance().toString(), Convert.Unit.ETHER);

        logger.info("Balance. [wei: {}] [coin: {}]: ", balanceWei.getBalance(), balanceInEther);
    }
}
