All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fisco.bcos.web3j.utils.Convert Maven / Gradle / Ivy

package org.fisco.bcos.web3j.utils;

import java.math.BigDecimal;

/** Ethereum unit conversion functions. */
public final class Convert {
  private Convert() {}

  public static BigDecimal fromWei(String number, Unit unit) {
    return fromWei(new BigDecimal(number), unit);
  }

  public static BigDecimal fromWei(BigDecimal number, Unit unit) {
    return number.divide(unit.getWeiFactor());
  }

  public static BigDecimal toWei(String number, Unit unit) {
    return toWei(new BigDecimal(number), unit);
  }

  public static BigDecimal toWei(BigDecimal number, Unit unit) {
    return number.multiply(unit.getWeiFactor());
  }

  public enum Unit {
    WEI("wei", 0),
    KWEI("kwei", 3),
    MWEI("mwei", 6),
    GWEI("gwei", 9),
    SZABO("szabo", 12),
    FINNEY("finney", 15),
    ETHER("ether", 18),
    KETHER("kether", 21),
    METHER("mether", 24),
    GETHER("gether", 27);

    private String name;
    private BigDecimal weiFactor;

    Unit(String name, int factor) {
      this.name = name;
      this.weiFactor = BigDecimal.TEN.pow(factor);
    }

    public BigDecimal getWeiFactor() {
      return weiFactor;
    }

    @Override
    public String toString() {
      return name;
    }

    public static Unit fromString(String name) {
      if (name != null) {
        for (Unit unit : Unit.values()) {
          if (name.equalsIgnoreCase(unit.name)) {
            return unit;
          }
        }
      }
      return Unit.valueOf(name);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy