fsl-js-sdk
Version:
sdk for web
51 lines (50 loc) • 2.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.U64Utils = exports.ONE = exports.ZERO = void 0;
var spl_token_v0_1 = require("@solana/spl-token-v0");
var __1 = require("../..");
var decimal_utils_1 = require("./decimal-utils");
exports.ZERO = new spl_token_v0_1.u64(0);
exports.ONE = new spl_token_v0_1.u64(1);
var U64Utils = /** @class */ (function () {
function U64Utils() {
}
U64Utils.toTokenU64 = function (input, token, varName) {
if (input instanceof __1.DooarU64) {
if (input.scale !== token.scale) {
throw new Error("".concat(varName, "'s scale of ").concat(input.scale, " does not match token's decimal of ").concat(token.scale));
}
return input.toU64();
}
return decimal_utils_1.DecimalUtil.toU64(input, token.scale);
};
U64Utils.toPoolU64 = function (input, pool, varName) {
if (input instanceof __1.DooarU64) {
if (input.scale !== pool.poolTokenDecimals) {
throw new Error("".concat(varName, "'s scale of ").concat(input.scale, " does not match pool's decimal of ").concat(pool.poolTokenDecimals));
}
return input.toU64();
}
return decimal_utils_1.DecimalUtil.toU64(input, pool.poolTokenDecimals);
};
// Note: divisor input variable modified in place
// https://github.com/solana-labs/solana-program-library/blob/master/libraries/math/src/checked_ceil_div.rs#L5-L22
U64Utils.ceilingDivision = function (dividend, divisor) {
var quotient = dividend.div(divisor);
if (quotient.eq(exports.ZERO)) {
return [exports.ZERO, divisor];
}
var remainder = dividend.mod(divisor);
if (remainder.gt(exports.ZERO)) {
quotient = quotient.add(exports.ONE);
divisor = dividend.div(quotient);
remainder = dividend.mod(quotient);
if (remainder.gt(exports.ZERO)) {
divisor = divisor.add(exports.ONE);
}
}
return [quotient, divisor];
};
return U64Utils;
}());
exports.U64Utils = U64Utils;