UNPKG

@ethersphere/bee-js

Version:
145 lines (144 loc) 4.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DAI = exports.BZZ = void 0; const cafe_utility_1 = require("cafe-utility"); class BZZ { constructor(state) { this.state = state; } static fromDecimalString(string) { return new BZZ(cafe_utility_1.FixedPointNumber.fromDecimalString(string, BZZ.DIGITS)); } static fromPLUR(plur) { return new BZZ(new cafe_utility_1.FixedPointNumber(plur, BZZ.DIGITS)); } toPLURString() { return this.state.toString(); } toPLURBigInt() { return this.state.value; } toDecimalString() { return this.state.toDecimalString(); } toSignificantDigits(digits) { return this.toDecimalString().slice(0, this.toDecimalString().indexOf('.') + digits + 1); } /** * Does not mutate the current BZZ instance. * * @param other BZZ instance, or amount in PLUR * @returns New BZZ instance */ plus(other) { return new BZZ(this.state.add(other instanceof BZZ ? other.state : new cafe_utility_1.FixedPointNumber(other, BZZ.DIGITS))); } /** * Does not mutate the current BZZ instance. * * @param other BZZ instance, or amount in PLUR * @returns New BZZ instance */ minus(other) { return new BZZ(this.state.subtract(other instanceof BZZ ? other.state : new cafe_utility_1.FixedPointNumber(other, BZZ.DIGITS))); } /** * Does not mutate the current BZZ instance. * * @param other Divisor * @returns New BZZ instance */ divide(other) { return new BZZ(this.state.divmod(other)[0]); } gt(other) { return this.state.compare(other.state) === 1; } gte(other) { return this.state.compare(other.state) !== -1; } lt(other) { return this.state.compare(other.state) === -1; } lte(other) { return this.state.compare(other.state) !== 1; } eq(other) { return this.state.compare(other.state) === 0; } exchangeToDAI(daiPerBzz) { return DAI.fromWei(this.state.exchange('*', new cafe_utility_1.FixedPointNumber(daiPerBzz.toWeiBigInt(), DAI.DIGITS), DAI.DIGITS).value); } } exports.BZZ = BZZ; BZZ.DIGITS = 16; class DAI { constructor(state) { this.state = state; } static fromDecimalString(string) { return new DAI(cafe_utility_1.FixedPointNumber.fromDecimalString(string, DAI.DIGITS)); } static fromWei(wei) { return new DAI(new cafe_utility_1.FixedPointNumber(wei, DAI.DIGITS)); } toWeiString() { return this.state.toString(); } toWeiBigInt() { return this.state.value; } toDecimalString() { return this.state.toDecimalString(); } toSignificantDigits(digits) { return this.toDecimalString().slice(0, this.toDecimalString().indexOf('.') + digits + 1); } /** * Does not mutate the current DAI instance. * * @param other DAI instance, or amount in PLUR * @returns New DAI instance */ plus(other) { return new DAI(this.state.add(other instanceof DAI ? other.state : new cafe_utility_1.FixedPointNumber(other, DAI.DIGITS))); } /** * Does not mutate the current DAI instance. * * @param other DAI instance, or amount in PLUR * @returns New DAI instance */ minus(other) { return new DAI(this.state.subtract(other instanceof DAI ? other.state : new cafe_utility_1.FixedPointNumber(other, DAI.DIGITS))); } /** * Does not mutate the current DAI instance. * * @param other Divisor * @returns New DAI instance */ divide(other) { return new DAI(this.state.divmod(other)[0]); } gt(other) { return this.state.compare(other.state) === 1; } gte(other) { return this.state.compare(other.state) !== -1; } lt(other) { return this.state.compare(other.state) === -1; } lte(other) { return this.state.compare(other.state) !== 1; } eq(other) { return this.state.compare(other.state) === 0; } exchangeToBZZ(daiPerBzz) { return BZZ.fromPLUR(this.state.exchange('/', new cafe_utility_1.FixedPointNumber(daiPerBzz.toWeiBigInt(), DAI.DIGITS), BZZ.DIGITS).value); } } exports.DAI = DAI; DAI.DIGITS = 18;