UNPKG

@nervosnetwork/ckb-sdk-utils

Version:

Utils module of @nervosnetwork/ckb-sdk-core

44 lines 2.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getWithdrawEpoch = exports.parseEpoch = exports.serializeEpoch = void 0; const tslib_1 = require("tslib"); const jsbi_1 = (0, tslib_1.__importDefault)(require("jsbi")); const validators_js_1 = require("./validators.js"); const serializeEpoch = ({ length, index, number }) => { (0, validators_js_1.assertToBeHexString)(length); (0, validators_js_1.assertToBeHexString)(index); (0, validators_js_1.assertToBeHexString)(number); const epochSince = jsbi_1.default.add(jsbi_1.default.add(jsbi_1.default.add(jsbi_1.default.leftShift(jsbi_1.default.BigInt(0x20), jsbi_1.default.BigInt(56)), jsbi_1.default.leftShift(jsbi_1.default.BigInt(length), jsbi_1.default.BigInt(40))), jsbi_1.default.leftShift(jsbi_1.default.BigInt(index), jsbi_1.default.BigInt(24))), jsbi_1.default.BigInt(number)); return `0x${epochSince.toString(16)}`; }; exports.serializeEpoch = serializeEpoch; const parseEpoch = (epoch) => ({ length: `0x${jsbi_1.default.bitwiseAnd(jsbi_1.default.signedRightShift(jsbi_1.default.BigInt(epoch), jsbi_1.default.BigInt(40)), jsbi_1.default.BigInt(0xffff)).toString(16)}`, index: `0x${jsbi_1.default.bitwiseAnd(jsbi_1.default.signedRightShift(jsbi_1.default.BigInt(epoch), jsbi_1.default.BigInt(24)), jsbi_1.default.BigInt(0xffff)).toString(16)}`, number: `0x${jsbi_1.default.bitwiseAnd(jsbi_1.default.BigInt(epoch), jsbi_1.default.BigInt(0xffffff)).toString(16)}`, }); exports.parseEpoch = parseEpoch; const getWithdrawEpoch = (depositEpoch, withdrawingEpoch) => { const EPOCHS_PER_WITHDRAW_CYCLE = 180; const depositEpochInfo = (0, exports.parseEpoch)(depositEpoch); const withdrawingEpochInfo = (0, exports.parseEpoch)(withdrawingEpoch); let depositedEpochCount = +withdrawingEpochInfo.number - +depositEpochInfo.number; if (+withdrawingEpochInfo.index * +depositEpochInfo.length > +depositEpochInfo.index * +withdrawingEpochInfo.length) { depositedEpochCount += 1; } const minEpockCountToLock = depositedEpochCount <= EPOCHS_PER_WITHDRAW_CYCLE ? EPOCHS_PER_WITHDRAW_CYCLE : (Math.floor((depositedEpochCount - 1) / EPOCHS_PER_WITHDRAW_CYCLE) + 1) * EPOCHS_PER_WITHDRAW_CYCLE; return (0, exports.serializeEpoch)({ index: depositEpochInfo.index, length: depositEpochInfo.length, number: `0x${(+depositEpochInfo.number + minEpockCountToLock).toString(16)}`, }); }; exports.getWithdrawEpoch = getWithdrawEpoch; exports.default = { serializeEpoch: exports.serializeEpoch, parseEpoch: exports.parseEpoch, getWithdrawEpoch: exports.getWithdrawEpoch, }; //# sourceMappingURL=epochs.js.map