UNPKG

@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.

176 lines (175 loc) • 6.77 kB
import { type BigIntish, type RoundingDirection } from "@morpho-org/morpho-ts"; /** * TypeScript port of Midnight `TakeAmountsLib`. * * @example * ```ts * import { TakeAmountsLib } from "@morpho-org/midnight-sdk"; * * console.log(typeof TakeAmountsLib.toUnits); * ``` */ export declare namespace 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: { readonly offer: { readonly buy: boolean; readonly tick: BigIntish; }; readonly settlementFee: BigIntish; }): { readonly buyerPrice: bigint; readonly sellerPrice: bigint; }; /** * 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: { readonly offer: { readonly buy: boolean; readonly tick: BigIntish; }; readonly targetBuyerAssets: BigIntish; readonly settlementFee: BigIntish; }): bigint; /** * 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: { readonly offer: { readonly buy: boolean; readonly tick: BigIntish; }; readonly targetSellerAssets: BigIntish; readonly settlementFee: BigIntish; }): bigint; /** * 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: { readonly assets: BigIntish; readonly price: BigIntish; readonly rounding: RoundingDirection; }): bigint; /** * 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: { readonly assets: BigIntish; readonly tick: BigIntish; readonly rounding: RoundingDirection; }): bigint; }