{"version":3,"file":"marginLiquidations.mjs","names":["liquidationVaultMoveCalls.createLiquidationVault","#config","liquidationVaultMoveCalls.deposit","liquidationVaultMoveCalls.withdraw","#liquidateBaseCall","#liquidateQuoteCall","liquidationVaultMoveCalls.balance","liquidationVaultMoveCalls.liquidateBaseUpgraded","liquidationVaultMoveCalls.liquidateQuoteUpgraded"],"sources":["../../src/transactions/marginLiquidations.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nimport type { Transaction } from '@mysten/sui/transactions';\nimport { coinWithBalance } from '@mysten/sui/transactions';\n\nimport type { DeepBookConfig } from '../utils/config.js';\nimport { convertQuantity } from '../utils/conversion.js';\nimport * as liquidationVaultMoveCalls from '../contracts/margin_liquidation/liquidation_vault.js';\n\n/**\n * MarginLiquidationsContract class for managing LiquidationVault operations.\n */\nexport class MarginLiquidationsContract {\n\t#config: DeepBookConfig;\n\n\t/**\n\t * @param {DeepBookConfig} config Configuration for MarginLiquidationsContract\n\t */\n\tconstructor(config: DeepBookConfig) {\n\t\tthis.#config = config;\n\t}\n\n\t/**\n\t * The oracle-taking liquidation entrypoints. Unlike the margin package,\n\t * `margin_liquidation` puts the upgraded entries in the same module as parallel\n\t * functions rather than in a parallel module, so these name a function rather than a\n\t * namespace. The vault's own deposit/withdraw/balance take no oracle and are unchanged.\n\t */\n\tget #liquidateBaseCall() {\n\t\treturn liquidationVaultMoveCalls.liquidateBaseUpgraded;\n\t}\n\n\tget #liquidateQuoteCall() {\n\t\treturn liquidationVaultMoveCalls.liquidateQuoteUpgraded;\n\t}\n\n\t/**\n\t * @description Create a new liquidation vault\n\t * @param {string} liquidationAdminCap The liquidation admin cap object ID\n\t * @returns A function that takes a Transaction object\n\t */\n\tcreateLiquidationVault = (liquidationAdminCap: string) => (tx: Transaction) => {\n\t\ttx.add(\n\t\t\tliquidationVaultMoveCalls.createLiquidationVault({\n\t\t\t\tpackage: this.#config.LIQUIDATION_PACKAGE_ID,\n\t\t\t\targuments: { LiquidationCap: liquidationAdminCap },\n\t\t\t}),\n\t\t);\n\t};\n\n\t/**\n\t * @description Deposit coins into a liquidation vault\n\t * @param {string} vaultId The liquidation vault object ID\n\t * @param {string} liquidationAdminCap The liquidation admin cap object ID\n\t * @param {string} coinKey The key to identify the coin type\n\t * @param {number} amount The amount to deposit\n\t * @returns A function that takes a Transaction object\n\t */\n\tdeposit =\n\t\t(vaultId: string, liquidationAdminCap: string, coinKey: string, amount: number) =>\n\t\t(tx: Transaction) => {\n\t\t\tconst coin = this.#config.getCoin(coinKey);\n\t\t\tconst depositCoin = coinWithBalance({\n\t\t\t\ttype: coin.type,\n\t\t\t\tbalance: convertQuantity(amount, coin.scalar),\n\t\t\t});\n\t\t\ttx.add(\n\t\t\t\tliquidationVaultMoveCalls.deposit({\n\t\t\t\t\tpackage: this.#config.LIQUIDATION_PACKAGE_ID,\n\t\t\t\t\targuments: { self: vaultId, LiquidationCap: liquidationAdminCap, coin: depositCoin },\n\t\t\t\t\ttypeArguments: [coin.type],\n\t\t\t\t}),\n\t\t\t);\n\t\t};\n\n\t/**\n\t * @description Withdraw coins from a liquidation vault\n\t * @param {string} vaultId The liquidation vault object ID\n\t * @param {string} liquidationAdminCap The liquidation admin cap object ID\n\t * @param {string} coinKey The key to identify the coin type\n\t * @param {number} amount The amount to withdraw\n\t * @returns A function that takes a Transaction object and returns the withdrawn coin\n\t */\n\twithdraw =\n\t\t(vaultId: string, liquidationAdminCap: string, coinKey: string, amount: number) =>\n\t\t(tx: Transaction) => {\n\t\t\tconst coin = this.#config.getCoin(coinKey);\n\t\t\treturn tx.add(\n\t\t\t\tliquidationVaultMoveCalls.withdraw({\n\t\t\t\t\tpackage: this.#config.LIQUIDATION_PACKAGE_ID,\n\t\t\t\t\targuments: {\n\t\t\t\t\t\tself: vaultId,\n\t\t\t\t\t\tLiquidationCap: liquidationAdminCap,\n\t\t\t\t\t\tamount: convertQuantity(amount, coin.scalar),\n\t\t\t\t\t},\n\t\t\t\t\ttypeArguments: [coin.type],\n\t\t\t\t}),\n\t\t\t);\n\t\t};\n\n\t/**\n\t * @description Liquidate a margin manager by repaying base debt\n\t * @param {string} vaultId The liquidation vault object ID\n\t * @param {string} managerAddress The margin manager address to liquidate\n\t * @param {string} poolKey The key to identify the pool\n\t * @param {number} [repayAmount] The amount to repay (in base asset units), or undefined for full liquidation\n\t * @returns A function that takes a Transaction object\n\t */\n\tliquidateBase =\n\t\t(vaultId: string, managerAddress: string, poolKey: string, repayAmount?: number) =>\n\t\t(tx: Transaction) => {\n\t\t\tconst pool = this.#config.getPool(poolKey);\n\t\t\tconst baseCoin = this.#config.getCoin(pool.baseCoin);\n\t\t\tconst quoteCoin = this.#config.getCoin(pool.quoteCoin);\n\t\t\tconst baseMarginPool = this.#config.getMarginPool(pool.baseCoin);\n\t\t\tconst quoteMarginPool = this.#config.getMarginPool(pool.quoteCoin);\n\n\t\t\t// Build the Option arg first so its pure input registers ahead of the\n\t\t\t// object inputs (matches the original positional ordering — byte-identical).\n\t\t\tconst repayAmountArg =\n\t\t\t\trepayAmount !== undefined\n\t\t\t\t\t? tx.pure.option('u64', convertQuantity(repayAmount, baseCoin.scalar))\n\t\t\t\t\t: tx.pure.option('u64', null);\n\n\t\t\ttx.add(\n\t\t\t\tthis.#liquidateBaseCall({\n\t\t\t\t\tpackage: this.#config.LIQUIDATION_PACKAGE_ID,\n\t\t\t\t\targuments: {\n\t\t\t\t\t\tself: vaultId,\n\t\t\t\t\t\tmarginManager: managerAddress,\n\t\t\t\t\t\tregistry: this.#config.MARGIN_REGISTRY_ID,\n\t\t\t\t\t\tbaseOracle: this.#config.getPriceInfoObjectId(pool.baseCoin),\n\t\t\t\t\t\tquoteOracle: this.#config.getPriceInfoObjectId(pool.quoteCoin),\n\t\t\t\t\t\tbaseMarginPool: baseMarginPool.address,\n\t\t\t\t\t\tquoteMarginPool: quoteMarginPool.address,\n\t\t\t\t\t\tpool: pool.address,\n\t\t\t\t\t\trepayAmount: repayAmountArg,\n\t\t\t\t\t},\n\t\t\t\t\ttypeArguments: [baseCoin.type, quoteCoin.type],\n\t\t\t\t}),\n\t\t\t);\n\t\t};\n\n\t/**\n\t * @description Liquidate a margin manager by repaying quote debt\n\t * @param {string} vaultId The liquidation vault object ID\n\t * @param {string} managerAddress The margin manager address to liquidate\n\t * @param {string} poolKey The key to identify the pool\n\t * @param {number} [repayAmount] The amount to repay (in quote asset units), or undefined for full liquidation\n\t * @returns A function that takes a Transaction object\n\t */\n\tliquidateQuote =\n\t\t(vaultId: string, managerAddress: string, poolKey: string, repayAmount?: number) =>\n\t\t(tx: Transaction) => {\n\t\t\tconst pool = this.#config.getPool(poolKey);\n\t\t\tconst baseCoin = this.#config.getCoin(pool.baseCoin);\n\t\t\tconst quoteCoin = this.#config.getCoin(pool.quoteCoin);\n\t\t\tconst baseMarginPool = this.#config.getMarginPool(pool.baseCoin);\n\t\t\tconst quoteMarginPool = this.#config.getMarginPool(pool.quoteCoin);\n\n\t\t\t// Build the Option arg first so its pure input registers ahead of the\n\t\t\t// object inputs (matches the original positional ordering — byte-identical).\n\t\t\tconst repayAmountArg =\n\t\t\t\trepayAmount !== undefined\n\t\t\t\t\t? tx.pure.option('u64', convertQuantity(repayAmount, quoteCoin.scalar))\n\t\t\t\t\t: tx.pure.option('u64', null);\n\n\t\t\ttx.add(\n\t\t\t\tthis.#liquidateQuoteCall({\n\t\t\t\t\tpackage: this.#config.LIQUIDATION_PACKAGE_ID,\n\t\t\t\t\targuments: {\n\t\t\t\t\t\tself: vaultId,\n\t\t\t\t\t\tmarginManager: managerAddress,\n\t\t\t\t\t\tregistry: this.#config.MARGIN_REGISTRY_ID,\n\t\t\t\t\t\tbaseOracle: this.#config.getPriceInfoObjectId(pool.baseCoin),\n\t\t\t\t\t\tquoteOracle: this.#config.getPriceInfoObjectId(pool.quoteCoin),\n\t\t\t\t\t\tbaseMarginPool: baseMarginPool.address,\n\t\t\t\t\t\tquoteMarginPool: quoteMarginPool.address,\n\t\t\t\t\t\tpool: pool.address,\n\t\t\t\t\t\trepayAmount: repayAmountArg,\n\t\t\t\t\t},\n\t\t\t\t\ttypeArguments: [baseCoin.type, quoteCoin.type],\n\t\t\t\t}),\n\t\t\t);\n\t\t};\n\n\t// === Read-Only Functions ===\n\n\t/**\n\t * @description Get the balance of a specific coin type in the liquidation vault\n\t * @param {string} vaultId The liquidation vault object ID\n\t * @param {string} coinKey The key to identify the coin type\n\t * @returns A function that takes a Transaction object\n\t */\n\tbalance = (vaultId: string, coinKey: string) => (tx: Transaction) => {\n\t\tconst coin = this.#config.getCoin(coinKey);\n\t\treturn tx.add(\n\t\t\tliquidationVaultMoveCalls.balance({\n\t\t\t\tpackage: this.#config.LIQUIDATION_PACKAGE_ID,\n\t\t\t\targuments: { self: vaultId },\n\t\t\t\ttypeArguments: [coin.type],\n\t\t\t}),\n\t\t);\n\t};\n}\n"],"mappings":";;;;;;;;AAYA,IAAa,6BAAb,MAAwC;CACvC;;;;CAKA,YAAY,QAAwB;iCAuBV,yBAAiC,OAAoB;AAC9E,MAAG,IACFA,uBAAiD;IAChD,SAAS,MAAKC,OAAQ;IACtB,WAAW,EAAE,gBAAgB,qBAAqB;IAClD,CAAC,CACF;;kBAYA,SAAiB,qBAA6B,SAAiB,YAC/D,OAAoB;GACpB,MAAM,OAAO,MAAKA,OAAQ,QAAQ,QAAQ;GAC1C,MAAM,cAAc,gBAAgB;IACnC,MAAM,KAAK;IACX,SAAS,gBAAgB,QAAQ,KAAK,OAAO;IAC7C,CAAC;AACF,MAAG,IACFC,QAAkC;IACjC,SAAS,MAAKD,OAAQ;IACtB,WAAW;KAAE,MAAM;KAAS,gBAAgB;KAAqB,MAAM;KAAa;IACpF,eAAe,CAAC,KAAK,KAAK;IAC1B,CAAC,CACF;;mBAYD,SAAiB,qBAA6B,SAAiB,YAC/D,OAAoB;GACpB,MAAM,OAAO,MAAKA,OAAQ,QAAQ,QAAQ;AAC1C,UAAO,GAAG,IACTE,SAAmC;IAClC,SAAS,MA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