mirror of
https://github.com/status-im/keycard-redeem.git
synced 2025-02-22 08:48:30 +00:00
19 lines
568 B
TypeScript
19 lines
568 B
TypeScript
import Web3Utils from "web3-utils";
|
|
|
|
const BN = Web3Utils.BN;
|
|
|
|
export const toBaseUnit = (fullAmount: string, decimalsSize: number, roundDecimals: number) => {
|
|
const amount = new BN(fullAmount);
|
|
const base = new BN(10).pow(new BN(decimalsSize));
|
|
const whole = amount.div(base).toString();
|
|
let decimals = amount.mod(base).toString();
|
|
for (let i = decimals.length; i < decimalsSize; i++) {
|
|
decimals = `0${decimals}`;
|
|
}
|
|
|
|
const full = `${whole}.${decimals}`;
|
|
const rounded = `${whole}.${decimals.slice(0, roundDecimals)}`;
|
|
|
|
return [full, rounded];
|
|
}
|