client-aftermath-ts-sdk
Version:
Client Aftermath TypeScript SDK
24 lines (23 loc) • 1.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FixedUtils = void 0;
class FixedUtils {
}
exports.FixedUtils = FixedUtils;
// 18 decimal places
FixedUtils.fixedOneN = 1000000000000000000;
FixedUtils.fixedOneB = BigInt("1000000000000000000");
// 9 decimal places
FixedUtils.fixedOneN9 = 1000000000;
FixedUtils.fixedOneB9 = BigInt(1000000000);
// These terms come from their on chain counterparts. They are backwards from the point of view of js.
// On chain direct cast means e.g. taking ((x: u64) as u256).
FixedUtils.convertFromInt = (n) => Number(n);
FixedUtils.convertToInt = (n) => BigInt(Math.floor(n));
FixedUtils.directCast = (n) => Number(n) / FixedUtils.fixedOneN;
FixedUtils.directUncast = (n) => BigInt(Math.floor(n * FixedUtils.fixedOneN));
FixedUtils.complement = (n) => Math.max(0, 1 - Math.max(0, n));
FixedUtils.normalizeAmount = (decimalsScalar, amount) => amount * decimalsScalar;
FixedUtils.unnormalizeAmount = (decimalsScalar, normalizedAmount) => normalizedAmount / decimalsScalar;
FixedUtils.castAndNormalize = (decimalsScalar, amount) => FixedUtils.directCast(FixedUtils.normalizeAmount(decimalsScalar, amount));
FixedUtils.uncastAndUnnormalize = (decimalsScalar, normalizedAmount) => FixedUtils.unnormalizeAmount(decimalsScalar, FixedUtils.directUncast(normalizedAmount));