{"version":3,"file":"flashLoans.mjs","names":["#config","poolMoveCalls.borrowFlashloanBase","poolMoveCalls.returnFlashloanBase","poolMoveCalls.borrowFlashloanQuote","poolMoveCalls.returnFlashloanQuote"],"sources":["../../src/transactions/flashLoans.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nimport type { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions';\n\nimport type { DeepBookConfig } from '../utils/config.js';\nimport { convertQuantity } from '../utils/conversion.js';\nimport * as poolMoveCalls from '../contracts/deepbook/pool.js';\n\n/**\n * FlashLoanContract class for managing flash loans.\n */\nexport class FlashLoanContract {\n\t#config: DeepBookConfig;\n\n\t/**\n\t * @param {DeepBookConfig} config Configuration object for DeepBook\n\t */\n\tconstructor(config: DeepBookConfig) {\n\t\tthis.#config = config;\n\t}\n\n\t/**\n\t * @description Borrow base asset from the pool\n\t * @param {string} poolKey The key to identify the pool\n\t * @param {number} borrowAmount The amount to borrow\n\t * @returns A function that takes a Transaction object\n\t */\n\tborrowBaseAsset = (poolKey: string, borrowAmount: number) => (tx: Transaction) => {\n\t\tconst pool = this.#config.getPool(poolKey);\n\t\tconst baseCoin = this.#config.getCoin(pool.baseCoin);\n\t\tconst quoteCoin = this.#config.getCoin(pool.quoteCoin);\n\t\tconst inputQuantity = convertQuantity(borrowAmount, baseCoin.scalar);\n\t\tconst [baseCoinResult, flashLoan] = tx.add(\n\t\t\tpoolMoveCalls.borrowFlashloanBase({\n\t\t\t\tpackage: this.#config.DEEPBOOK_PACKAGE_ID,\n\t\t\t\targuments: { self: pool.address, baseAmount: inputQuantity },\n\t\t\t\ttypeArguments: [baseCoin.type, quoteCoin.type],\n\t\t\t}),\n\t\t);\n\t\treturn [baseCoinResult, flashLoan] as const;\n\t};\n\n\t/**\n\t * @description Return base asset to the pool after a flash loan.\n\t * @param {string} poolKey The key to identify the pool\n\t * @param {number} borrowAmount The amount of the base asset to return\n\t * @param {TransactionObjectArgument} baseCoinInput Coin object representing the base asset to be returned\n\t * @param {TransactionObjectArgument} flashLoan FlashLoan object representing the loan to be settled\n\t * @returns A function that takes a Transaction object\n\t */\n\treturnBaseAsset =\n\t\t(\n\t\t\tpoolKey: string,\n\t\t\tborrowAmount: number,\n\t\t\tbaseCoinInput: TransactionObjectArgument,\n\t\t\tflashLoan: TransactionObjectArgument,\n\t\t) =>\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 borrowScalar = baseCoin.scalar;\n\n\t\t\tconst [baseCoinReturn] = tx.splitCoins(baseCoinInput, [\n\t\t\t\ttx.pure.u64(convertQuantity(borrowAmount, borrowScalar)),\n\t\t\t]);\n\t\t\ttx.add(\n\t\t\t\tpoolMoveCalls.returnFlashloanBase({\n\t\t\t\t\tpackage: this.#config.DEEPBOOK_PACKAGE_ID,\n\t\t\t\t\targuments: { self: pool.address, coin: baseCoinReturn, flashLoan },\n\t\t\t\t\ttypeArguments: [baseCoin.type, quoteCoin.type],\n\t\t\t\t}),\n\t\t\t);\n\n\t\t\treturn baseCoinInput;\n\t\t};\n\n\t/**\n\t * @description Borrow quote asset from the pool\n\t * @param {string} poolKey The key to identify the pool\n\t * @param {number} borrowAmount The amount to borrow\n\t * @returns A function that takes a Transaction object\n\t */\n\tborrowQuoteAsset = (poolKey: string, borrowAmount: number) => (tx: Transaction) => {\n\t\tconst pool = this.#config.getPool(poolKey);\n\t\tconst baseCoin = this.#config.getCoin(pool.baseCoin);\n\t\tconst quoteCoin = this.#config.getCoin(pool.quoteCoin);\n\t\tconst inputQuantity = convertQuantity(borrowAmount, quoteCoin.scalar);\n\t\tconst [quoteCoinResult, flashLoan] = tx.add(\n\t\t\tpoolMoveCalls.borrowFlashloanQuote({\n\t\t\t\tpackage: this.#config.DEEPBOOK_PACKAGE_ID,\n\t\t\t\targuments: { self: pool.address, quoteAmount: inputQuantity },\n\t\t\t\ttypeArguments: [baseCoin.type, quoteCoin.type],\n\t\t\t}),\n\t\t);\n\t\treturn [quoteCoinResult, flashLoan] as const;\n\t};\n\n\t/**\n\t * @description Return quote asset to the pool after a flash loan.\n\t * @param {string} poolKey The key to identify the pool\n\t * @param {number} borrowAmount The amount of the quote asset to return\n\t * @param {TransactionObjectArgument} quoteCoinInput Coin object representing the quote asset to be returned\n\t * @param {TransactionObjectArgument} flashLoan FlashLoan object representing the loan to be settled\n\t * @returns A function that takes a Transaction object\n\t */\n\treturnQuoteAsset =\n\t\t(\n\t\t\tpoolKey: string,\n\t\t\tborrowAmount: number,\n\t\t\tquoteCoinInput: TransactionObjectArgument,\n\t\t\tflashLoan: TransactionObjectArgument,\n\t\t) =>\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 borrowScalar = quoteCoin.scalar;\n\n\t\t\tconst [quoteCoinReturn] = tx.splitCoins(quoteCoinInput, [\n\t\t\t\ttx.pure.u64(convertQuantity(borrowAmount, borrowScalar)),\n\t\t\t]);\n\t\t\ttx.add(\n\t\t\t\tpoolMoveCalls.returnFlashloanQuote({\n\t\t\t\t\tpackage: this.#config.DEEPBOOK_PACKAGE_ID,\n\t\t\t\t\targuments: { self: pool.address, coin: quoteCoinReturn, flashLoan },\n\t\t\t\t\ttypeArguments: [baseCoin.type, quoteCoin.type],\n\t\t\t\t}),\n\t\t\t);\n\n\t\t\treturn quoteCoinInput;\n\t\t};\n}\n"],"mappings":";;;;;;;AAWA,IAAa,oBAAb,MAA+B;CAC9B;;;;CAKA,YAAY,QAAwB;0BAUjB,SAAiB,kBAA0B,OAAoB;GACjF,MAAM,OAAO,MAAKA,OAAQ,QAAQ,QAAQ;GAC1C,MAAM,WAAW,MAAKA,OAAQ,QAAQ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