@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.
201 lines (200 loc) • 8.45 kB
JavaScript
import { assertNonNegative, DivisionByZeroError, MathLib, } from "@morpho-org/morpho-ts";
import { PriceGreaterThanOneError, SettlementFeeExceedsPriceError, } from "../errors.js";
import { TickLib } from "./TickLib.js";
/**
* TypeScript port of Midnight `TakeAmountsLib`.
*
* @example
* ```ts
* import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
*
* console.log(typeof TakeAmountsLib.toUnits);
* ```
*/
export 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);
assertNonNegative("settlementFee", settlementFee);
const offerPrice = TickLib.tickToPrice(params.offer.tick);
if (params.offer.buy && offerPrice < settlementFee) {
throw new 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);
assertNonNegative("targetBuyerAssets", targetBuyerAssets);
const { buyerPrice } = prices({ offer: params.offer, settlementFee });
if (buyerPrice === 0n) {
throw new DivisionByZeroError("buyerPrice");
}
if (buyerPrice > MathLib.WAD) {
throw new PriceGreaterThanOneError(buyerPrice);
}
return MathLib.mulDiv(targetBuyerAssets, 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);
assertNonNegative("targetSellerAssets", targetSellerAssets);
const { sellerPrice } = prices({ offer: params.offer, settlementFee });
if (sellerPrice === 0n) {
throw new DivisionByZeroError("sellerPrice");
}
return MathLib.mulDiv(targetSellerAssets, 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);
assertNonNegative("assets", assets);
assertNonNegative("price", price);
if (price === 0n) {
throw new DivisionByZeroError("price");
}
return MathLib.mulDiv(assets, 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.tickToPrice(params.tick),
rounding: params.rounding,
});
}
TakeAmountsLib.toUnitsAtTick = toUnitsAtTick;
})(TakeAmountsLib || (TakeAmountsLib = {}));