evm-blockchain-tools
Version:
This is a collection of resuseable tools to support development for EVM-powered blockchains
27 lines • 1.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseERC20Currency = void 0;
const bignumber_js_1 = require("bignumber.js");
const constants_1 = require("../common/constants");
function parseCurrencyWithDecimals(amount, decimals, floatDecimals) {
const BigTen = (0, bignumber_js_1.BigNumber)(10).pow(decimals);
const amountInEther = (0, bignumber_js_1.BigNumber)(amount).div(BigTen).toString();
const [a, b] = amountInEther.split(".");
if ((b === null || b === void 0 ? void 0 : b.length) > 0) {
return `${a}.${b.slice(0, floatDecimals)}`;
}
return amountInEther.toString();
}
const parseERC20Currency = (amount, currency, network = constants_1.APP_NETWORK.ETH) => {
const usdtDecimals = network === constants_1.APP_NETWORK.BINANCE ? 18 : 6;
switch (currency) {
case constants_1.ACCEPTED_CURRENCY.USC:
return parseCurrencyWithDecimals(amount, 18, 8);
case constants_1.ACCEPTED_CURRENCY.USDT:
return parseCurrencyWithDecimals(amount, usdtDecimals, 8);
default:
throw new Error("currency not supported");
}
};
exports.parseERC20Currency = parseERC20Currency;
//# sourceMappingURL=erc20-currency-parser.js.map