@morpho-org/midnight-sdk
Version:
Viem-based package for Morpho Midnight that exports protocol utilities, fetch helpers, and Midnight API utilities under a dedicated API subpath.
204 lines (203 loc) • 8.75 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TakeAmountsLib = void 0;
const morpho_ts_1 = require("@morpho-org/morpho-ts");
const errors_js_1 = require("../errors.js");
const TickLib_js_1 = require("./TickLib.js");
/**
* TypeScript port of Midnight `TakeAmountsLib`.
*
* @example
* ```ts
* import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
*
* console.log(typeof TakeAmountsLib.toUnits);
* ```
*/
var TakeAmountsLib;
(function (TakeAmountsLib) {
/**
* Computes buyer and seller prices for an offer and settlement fee.
*
* @param params.offer.buy - Whether the maker buys loan assets.
* @param params.offer.tick - Offer tick used to compute the base price.
* @param params.settlementFee - WAD-scaled settlement fee for the market and time to maturity.
* @returns Buyer and seller prices.
* @throws {NegativeValueError} when `settlementFee` or the offer tick is negative.
* @throws {TickOutOfRangeError} when the offer tick exceeds `MAX_TICK`.
* @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
* @example
* ```ts
* import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
*
* const offer = {
* buy: true,
* tick: 5_820n,
* };
*
* const prices = TakeAmountsLib.prices({ offer, settlementFee: 1_000000000000n });
* console.log(prices.buyerPrice);
* ```
*/
function prices(params) {
const settlementFee = BigInt(params.settlementFee);
(0, morpho_ts_1.assertNonNegative)("settlementFee", settlementFee);
const offerPrice = TickLib_js_1.TickLib.tickToPrice(params.offer.tick);
if (params.offer.buy && offerPrice < settlementFee) {
throw new errors_js_1.SettlementFeeExceedsPriceError(settlementFee, offerPrice);
}
const sellerPrice = params.offer.buy
? offerPrice - settlementFee
: offerPrice;
const buyerPrice = sellerPrice + settlementFee;
return { buyerPrice, sellerPrice };
}
TakeAmountsLib.prices = prices;
/**
* Converts a target buyer-asset amount into units for an offer.
*
* `settlementFee` must be the fee for the offer market's current time to maturity.
*
* @param params.offer.buy - Whether the maker buys loan assets.
* @param params.offer.tick - Offer tick used to compute the buyer price.
* @param params.targetBuyerAssets - Target buyer-side asset amount.
* @param params.settlementFee - WAD-scaled settlement fee for the market and time to maturity.
* @returns Units that round-trip to the target buyer assets where reachable.
* @throws {NegativeValueError} when `targetBuyerAssets`, `settlementFee`, or the offer tick is negative.
* @throws {DivisionByZeroError} when the computed buyer price is zero.
* @throws {PriceGreaterThanOneError} when buyer price is above WAD.
* @throws {TickOutOfRangeError} when the offer tick exceeds `MAX_TICK`.
* @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
* @example
* ```ts
* import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
*
* const offer = {
* buy: true,
* tick: 5_820n,
* };
*
* const units = TakeAmountsLib.buyerAssetsToUnits({
* offer,
* targetBuyerAssets: 100n,
* settlementFee: 0n,
* });
* console.log(units);
* ```
*/
function buyerAssetsToUnits(params) {
const settlementFee = BigInt(params.settlementFee);
const targetBuyerAssets = BigInt(params.targetBuyerAssets);
(0, morpho_ts_1.assertNonNegative)("targetBuyerAssets", targetBuyerAssets);
const { buyerPrice } = prices({ offer: params.offer, settlementFee });
if (buyerPrice === 0n) {
throw new morpho_ts_1.DivisionByZeroError("buyerPrice");
}
if (buyerPrice > morpho_ts_1.MathLib.WAD) {
throw new errors_js_1.PriceGreaterThanOneError(buyerPrice);
}
return morpho_ts_1.MathLib.mulDiv(targetBuyerAssets, morpho_ts_1.MathLib.WAD, buyerPrice, params.offer.buy ? "Up" : "Down");
}
TakeAmountsLib.buyerAssetsToUnits = buyerAssetsToUnits;
/**
* Converts a target seller-asset amount into units for an offer.
*
* `settlementFee` must be the fee for the offer market's current time to maturity.
*
* @param params.offer.buy - Whether the maker buys loan assets.
* @param params.offer.tick - Offer tick used to compute the seller price.
* @param params.targetSellerAssets - Target seller-side asset amount.
* @param params.settlementFee - WAD-scaled settlement fee for the market and time to maturity.
* @returns Units that round-trip to the target seller assets where reachable.
* @throws {NegativeValueError} when `targetSellerAssets`, `settlementFee`, or the offer tick is negative.
* @throws {DivisionByZeroError} when the computed seller price is zero.
* @throws {TickOutOfRangeError} when the offer tick exceeds `MAX_TICK`.
* @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
* @example
* ```ts
* import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
*
* const offer = {
* buy: false,
* tick: 5_820n,
* };
*
* const units = TakeAmountsLib.sellerAssetsToUnits({
* offer,
* targetSellerAssets: 100n,
* settlementFee: 0n,
* });
* console.log(units);
* ```
*/
function sellerAssetsToUnits(params) {
const settlementFee = BigInt(params.settlementFee);
const targetSellerAssets = BigInt(params.targetSellerAssets);
(0, morpho_ts_1.assertNonNegative)("targetSellerAssets", targetSellerAssets);
const { sellerPrice } = prices({ offer: params.offer, settlementFee });
if (sellerPrice === 0n) {
throw new morpho_ts_1.DivisionByZeroError("sellerPrice");
}
return morpho_ts_1.MathLib.mulDiv(targetSellerAssets, morpho_ts_1.MathLib.WAD, sellerPrice, params.offer.buy ? "Up" : "Down");
}
TakeAmountsLib.sellerAssetsToUnits = sellerAssetsToUnits;
/**
* Converts assets to units at a WAD price.
*
* This is an SDK-only generic conversion convenience.
*
* @param params.assets - Asset amount to convert.
* @param params.price - WAD-scaled price used for the conversion.
* @param params.rounding - Rounding direction applied by the fixed-point conversion.
* @returns Units.
* @throws {NegativeValueError} when `assets` or `price` is negative.
* @throws {DivisionByZeroError} when price is zero.
* @example
* ```ts
* import { MathLib } from "@morpho-org/morpho-ts";
* import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
*
* const units = TakeAmountsLib.toUnits({ assets: 100n, price: MathLib.WAD, rounding: "Down" });
* console.log(units);
* ```
*/
function toUnits(params) {
const assets = BigInt(params.assets);
const price = BigInt(params.price);
(0, morpho_ts_1.assertNonNegative)("assets", assets);
(0, morpho_ts_1.assertNonNegative)("price", price);
if (price === 0n) {
throw new morpho_ts_1.DivisionByZeroError("price");
}
return morpho_ts_1.MathLib.mulDiv(assets, morpho_ts_1.MathLib.WAD, price, params.rounding);
}
TakeAmountsLib.toUnits = toUnits;
/**
* Converts assets to units at a tick price.
*
* This is an SDK-only tick-priced conversion convenience.
*
* @param params.assets - Asset amount to convert.
* @param params.tick - Offer tick whose price is used for the conversion.
* @param params.rounding - Rounding direction applied by the fixed-point conversion.
* @returns Units.
* @throws {NegativeValueError} when `assets` or `tick` is negative.
* @throws {DivisionByZeroError} when the tick price is zero.
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
* @example
* ```ts
* import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
*
* const units = TakeAmountsLib.toUnitsAtTick({ assets: 100n, tick: 6744n, rounding: "Down" });
* console.log(units);
* ```
*/
function toUnitsAtTick(params) {
return toUnits({
assets: params.assets,
price: TickLib_js_1.TickLib.tickToPrice(params.tick),
rounding: params.rounding,
});
}
TakeAmountsLib.toUnitsAtTick = toUnitsAtTick;
})(TakeAmountsLib || (exports.TakeAmountsLib = TakeAmountsLib = {}));