From f39bf8c51140847c1e9f7277f7c0336a48c72f17 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Mon, 27 Feb 2023 17:08:32 +0300 Subject: [PATCH 01/39] feat(swap): initialize swap router --- package.json | 2 +- src/index.ts | 10 +- src/swap/types.ts | 150 ++++++++++++++++++- src/swap/utils.ts | 235 ++++++++++++++++++------------ src/swap/v1_1/index.ts | 24 ++- src/swap/v2/constants.ts | 4 + src/swap/v2/index.ts | 173 ++++++++++++++-------- src/swap/v2/router/index.ts | 2 + src/swap/v2/router/swap-router.ts | 189 ++++++++++++++++++++++++ src/swap/v2/router/util.ts | 18 +++ src/util/asset/assetUtils.ts | 7 + 11 files changed, 637 insertions(+), 177 deletions(-) create mode 100644 src/swap/v2/router/index.ts create mode 100644 src/swap/v2/router/swap-router.ts create mode 100644 src/swap/v2/router/util.ts diff --git a/package.json b/package.json index f4064896..573adcd4 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "@tinymanorg/tinyman-js-sdk", + "name": "qefqefeq", "version": "2.0.3", "description": "Tinyman JS SDK", "author": "Tinyman Core Team", diff --git a/src/index.ts b/src/index.ts index 1319f4df..b5f06a97 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,8 @@ export { MINIMUM_ADD_LIQUIDITY_AMOUNT } from "./util/constant"; +export * from "./swap/v2/router"; + export { applySlippageToAmount, ASSET_OPT_IN_PROCESS_TXN_COUNT, @@ -105,13 +107,17 @@ export {RemoveLiquidity} from "./remove-liquidity"; export type { SwapQuote, - SwapQuoteWithPool, V1SwapExecution, - V2SwapExecution + V2SwapExecution, + DirectSwapQuote, + SwapRoute, + GenerateSwapTxnsParams } from "./swap/types"; export {getSwapTotalFee} from "./swap/utils"; export {SwapType} from "./swap/constants"; export {Swap} from "./swap"; +// eslint-disable-next-line no-duplicate-imports +export {SwapQuoteType} from "./swap/types"; export { redeemExcessAsset, diff --git a/src/swap/types.ts b/src/swap/types.ts index c8847f5f..966bac1d 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -1,8 +1,11 @@ -import {AssetWithIdAndAmount} from "../util/asset/assetModels"; +import {Algodv2} from "algosdk"; + +import {AssetWithIdAndAmount, TinymanAnalyticsApiAsset} from "../util/asset/assetModels"; +import {SupportedNetwork} from "../util/commonTypes"; import {PoolReserves, V1PoolInfo, V2PoolInfo} from "../util/pool/poolTypes"; +import {SwapType} from "./constants"; -/** An object containing information about a swap quote. */ -export interface SwapQuote { +export interface DirectSwapQuote { /** The ID of the input asset in this quote. */ assetInID: number; /** The quantity of the input asset in this quote. */ @@ -19,6 +22,7 @@ export interface SwapQuote { priceImpact: number; /** The round that this quote is based on. */ round?: number; + price?: number; } /** An object containing information about a successfully executed swap. */ @@ -59,12 +63,146 @@ export interface V2SwapExecution { /** Can be `undefined` if the execution was successful, but there was an issue while * extracting the output asset data fron the transaction response */ assetOut: AssetWithIdAndAmount | undefined; - pool: V2PoolInfo; + quote: SwapQuote; txnID: string; round: number; } +export type GetSwapQuoteParams = { + assetIn: Pick; + assetOut: Pick; + pools: {info: V1PoolInfo | V2PoolInfo; reserves: PoolReserves}[]; + amount: number | bigint; + type: SwapType; + network: SupportedNetwork; + isSwapRouterEnabled?: boolean; +}; + export interface SwapQuoteWithPool { - quote: SwapQuote; - pool: {info: V1PoolInfo | V2PoolInfo; reserves: PoolReserves}; + quote: DirectSwapQuote; + pool: V1PoolInfo | V2PoolInfo; +} + +export type SwapQuote = + | { + quoteWithPool: SwapQuoteWithPool; + type: SwapQuoteType.Direct; + } + | { + route: SwapRoute; + type: SwapQuoteType.Router; + }; + +export type GetSwapQuoteBySwapTypeParams = Omit; + +export type GetSwapQuoteWithContractVersionParams = { + type: SwapType; + pool: V2PoolInfo; + asset: AssetWithIdAndAmount; + decimals: {assetIn: number; assetOut: number}; + isSwapRouterEnabled?: boolean; +}; + +export type GetFixedInputSwapQuoteByContractVersionParams = Omit< + GetSwapQuoteWithContractVersionParams, + "type" | "asset" +> & { + assetIn: AssetWithIdAndAmount; +}; + +export type GetFixedOutputSwapQuoteByContractVersionParams = Omit< + GetSwapQuoteWithContractVersionParams, + "type" | "asset" +> & { + assetOut: AssetWithIdAndAmount; +}; + +export interface FetchSwapRouteQuotesPayload { + asset_in_id: string; + asset_out_id: string; + amount: string; + swap_type: SwapType; +} + +export interface SwapRouteAsset { + id: string; + name: string; + unit_name: string; + decimals: number; +} + +export interface SwapRoutePool { + address: string; + asset_1: SwapRouteAsset; + asset_2: SwapRouteAsset; + version: "2.0"; } + +export type SwapRoute = { + quote: SwapRouterQuote; + pool: SwapRoutePool; +}[]; + +export enum SwapQuoteType { + Direct = "direct", + Router = "router" +} + +export interface GenerateSwapTxnsWithoutRouterParams { + client: Algodv2; + pool: V1PoolInfo | V2PoolInfo; + swapType: SwapType; + assetIn: AssetWithIdAndAmount; + assetOut: AssetWithIdAndAmount; + slippage: number; + initiatorAddr: string; +} + +export interface GenerateSwapRouterTxnsParams { + client: Algodv2; + routerAppID: number; + initiatorAddr: string; + assetIn: AssetWithIdAndAmount; + assetOut: AssetWithIdAndAmount; + intermediaryAssetID: number; + swapType: SwapType; + network: SupportedNetwork; +} + +export type GenerateSwapTxnsParams = + | (GenerateSwapTxnsWithoutRouterParams & { + isUsingSwapRouter: false; + }) + | (GenerateSwapRouterTxnsParams & { + isUsingSwapRouter: true; + }); + +export type GenerateV1_1SwapTxnsParams = Omit & { + quote: DirectSwapQuote; +}; + +export interface SwapRouterQuote { + swap_type: SwapType; + amount_in: { + asset: SwapRouteAsset; + amount: string; + }; + amount_out: { + asset: SwapRouteAsset; + amount: string; + }; + swap_fees: { + amount: string; + asset_id: string; + }; + price: number; + price_impact: number; +} + +export type FetchSwapRouteQuotesResponse = FetchSwapRouteQuotesPayload & { + route: SwapRoute; + status: { + round_number: string; + round_datetime: string; + }; +}; diff --git a/src/swap/utils.ts b/src/swap/utils.ts index f7d62303..1d08ea31 100644 --- a/src/swap/utils.ts +++ b/src/swap/utils.ts @@ -1,133 +1,163 @@ import {Algodv2} from "algosdk"; import {CONTRACT_VERSION} from "../contract/constants"; -import {AssetWithIdAndAmount, TinymanAnalyticsApiAsset} from "../util/asset/assetModels"; +import {AssetWithIdAndAmount} from "../util/asset/assetModels"; import {InitiatorSigner, SignerTransaction, SupportedNetwork} from "../util/commonTypes"; -import {PoolReserves, V1PoolInfo, V2PoolInfo} from "../util/pool/poolTypes"; -import {SwapQuoteWithPool, SwapQuote} from "./types"; +import {V1PoolInfo} from "../util/pool/poolTypes"; +import { + GetSwapQuoteBySwapTypeParams, + GenerateSwapTxnsParams, + GetSwapQuoteParams, + SwapQuote, + SwapQuoteType +} from "./types"; import {SwapType} from "./constants"; import {SwapV1_1} from "./v1_1"; import {SwapV2} from "./v2"; import {V1_1_SWAP_TOTAL_FEE} from "./v1_1/constants"; import {getV2SwapTotalFee} from "./v2/util"; import {isPoolEmpty} from "../util/pool/common"; +import {ContractVersionValue} from "../contract/types"; +import {getSwapRouteRate} from "./v2/router/util"; /** - * Gets quotes for swap from each pool passed as an argument, - * and returns the best quote (with the highest rate). + * Gets the best quote for swap from the pools and swap router and returns the best option. */ -export function getQuote(params: { - type: SwapType; - pools: {info: V1PoolInfo | V2PoolInfo; reserves: PoolReserves}[]; - assetIn: Pick; - assetOut: Pick; - amount: number | bigint; -}): SwapQuoteWithPool { - if (params.pools.every((pool) => isPoolEmpty(pool.reserves))) { +export function getQuote(params: GetSwapQuoteParams): Promise { + const {type, isSwapRouterEnabled, pools} = params; + + if (!isSwapRouterEnabled && pools.every((pool) => isPoolEmpty(pool.reserves))) { throw new Error("No pools available for swap"); } - if (params.type === SwapType.FixedInput) { + if (type === SwapType.FixedInput) { return getFixedInputSwapQuote(params); + } else if (type === SwapType.FixedOutput) { + return getFixedOutputSwapQuote(params); } - return getFixedOutputSwapQuote(params); + return Promise.reject(new Error("Invalid swap type")); } /** - * Gets quotes for fixed input swap from each pool passed as an argument, + * Gets quotes for fixed input swap the pools and swap router, * and returns the best quote (with the highest rate). */ -export function getFixedInputSwapQuote({ - pools, - assetIn, - assetOut, - amount -}: { - pools: {info: V1PoolInfo | V2PoolInfo; reserves: PoolReserves}[]; - assetIn: Pick; - assetOut: Pick; - amount: number | bigint; -}): SwapQuoteWithPool { - const quotes = pools.map((pool) => { - let quote: SwapQuote; - const quoteGetterArgs = { - pool: pool.info, - assetIn: {amount, id: Number(assetIn.id)}, - decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, - reserves: pool.reserves - }; - - if (pool.info.contractVersion === CONTRACT_VERSION.V1_1) { - quote = SwapV1_1.getFixedInputSwapQuote(quoteGetterArgs); - } else { - quote = SwapV2.getFixedInputSwapQuote(quoteGetterArgs); - } +export async function getFixedInputSwapQuote( + params: GetSwapQuoteBySwapTypeParams +): Promise { + const {amount, assetIn, assetOut, pools} = params; + + const quotes: SwapQuote[] = await Promise.all( + pools.map(async (pool) => { + let quote: SwapQuote; + const quoteGetterArgs = { + pool: pool.info, + assetIn: {amount, id: Number(assetIn.id)}, + decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, + reserves: pool.reserves, + isSwapRouterEnabled: params.isSwapRouterEnabled + }; + + if (pool.info.contractVersion === CONTRACT_VERSION.V1_1) { + quote = { + type: SwapQuoteType.Direct, + quoteWithPool: { + quote: SwapV1_1.getFixedInputSwapQuote(quoteGetterArgs), + pool: pool.info + } + }; + } else { + quote = await SwapV2.getFixedInputSwapQuote(quoteGetterArgs); + } - return {pool, quote}; - }); + return quote; + }) + ); + + console.log("quotes", quotes); return getBestQuote(quotes); } /** - * Gets quotes for fixed output swap from each pool passed as an argument, + * Gets quotes for fixed output swap from the pools and swap router, * and returns the best quote (with the highest rate). */ -export function getFixedOutputSwapQuote({ - pools, - assetIn, - assetOut, - amount -}: { - pools: {info: V1PoolInfo | V2PoolInfo; reserves: PoolReserves}[]; - assetIn: Pick; - assetOut: Pick; - amount: number | bigint; -}): SwapQuoteWithPool { - const quotes = pools.map((pool) => { - let quote: SwapQuote; - const quoteGetterArgs = { - pool: pool.info, - assetOut: {amount, id: Number(assetOut.id)}, - decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, - reserves: pool.reserves - }; - - if (pool.info.contractVersion === CONTRACT_VERSION.V1_1) { - quote = SwapV1_1.getFixedOutputSwapQuote(quoteGetterArgs); - } else { - quote = SwapV2.getFixedOutputSwapQuote(quoteGetterArgs); - } +export async function getFixedOutputSwapQuote( + params: GetSwapQuoteBySwapTypeParams +): Promise { + const {amount, assetIn, assetOut, pools} = params; + + const quotes: SwapQuote[] = await Promise.all( + pools.map(async (pool) => { + let quote: SwapQuote; + const quoteGetterArgs = { + pool: pool.info, + assetOut: {amount, id: Number(assetOut.id)}, + decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, + reserves: pool.reserves + }; + + if (pool.info.contractVersion === CONTRACT_VERSION.V1_1) { + quote = { + type: SwapQuoteType.Direct, + quoteWithPool: { + quote: SwapV1_1.getFixedOutputSwapQuote(quoteGetterArgs), + pool: pool.info + } + }; + } else { + quote = await SwapV2.getFixedOutputSwapQuote(quoteGetterArgs); + } - return {pool, quote}; - }); + return quote; + }) + ); return getBestQuote(quotes); } +/** + * Returns the rate of a swap quote. + * + * @param quote - The quote. + * @returns The rate. + */ +function getSwapQuoteRate(quote: SwapQuote): number { + if (quote.type === SwapQuoteType.Direct) { + return quote.quoteWithPool.quote.rate; + } + + return getSwapRouteRate(quote.route); +} + /** * Compares the given quotes and returns the best one (with the highest rate). */ -function getBestQuote(quotes: SwapQuoteWithPool[]): SwapQuoteWithPool { - const quotesByDescendingRate = quotes - .filter((quote) => !isPoolEmpty(quote.pool.reserves)) - .sort((a, b) => b.quote.rate - a.quote.rate); +function getBestQuote(quotes: SwapQuote[]): SwapQuote { + let bestQuote: SwapQuote = quotes[0]; + let bestQuoteRate = getSwapQuoteRate(bestQuote); + + for (const quote of quotes) { + const currentRate = getSwapQuoteRate(quote); - return quotesByDescendingRate[0]; + if (currentRate > bestQuoteRate) { + bestQuote = quote; + bestQuoteRate = currentRate; + } + } + + return bestQuote; } -export function generateTxns(params: { - client: Algodv2; - pool: V1PoolInfo | V2PoolInfo; - poolAddress: string; - swapType: SwapType; - assetIn: AssetWithIdAndAmount; - assetOut: AssetWithIdAndAmount; - slippage: number; - initiatorAddr: string; -}): Promise { - if (params.pool.contractVersion === CONTRACT_VERSION.V1_1) { +export function generateTxns( + params: GenerateSwapTxnsParams +): Promise { + if ( + !params.isUsingSwapRouter && + params.pool.contractVersion === CONTRACT_VERSION.V1_1 + ) { return SwapV1_1.generateTxns(params); } @@ -135,12 +165,20 @@ export function generateTxns(params: { } export function signTxns(params: { - pool: V1PoolInfo; + quote: SwapQuote; txGroup: SignerTransaction[]; initiatorSigner: InitiatorSigner; + network: SupportedNetwork; }): Promise { - if (params.pool.contractVersion === CONTRACT_VERSION.V1_1) { - return SwapV1_1.signTxns(params); + if ( + params.quote.type === SwapQuoteType.Direct && + getContractVersionFromSwapQuote(params.quote) === CONTRACT_VERSION.V1_1 + ) { + const { + quoteWithPool: {pool} + } = params.quote; + + return SwapV1_1.signTxns({pool, ...params}); } return SwapV2.signTxns(params); @@ -149,7 +187,6 @@ export function signTxns(params: { interface ExecuteCommonParams { swapType: SwapType; client: Algodv2; - pool: V2PoolInfo; network: SupportedNetwork; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; @@ -158,8 +195,12 @@ interface ExecuteCommonParams { export function execute( params: ( - | {contractVersion: typeof CONTRACT_VERSION.V1_1; initiatorAddr: string} - | {contractVersion: typeof CONTRACT_VERSION.V2} + | { + contractVersion: typeof CONTRACT_VERSION.V1_1; + initiatorAddr: string; + pool: V1PoolInfo; + } + | {contractVersion: typeof CONTRACT_VERSION.V2; quote: SwapQuote} ) & ExecuteCommonParams ) { @@ -195,3 +236,11 @@ export function getSwapTotalFee( throw new Error("Provided contract version was not valid."); } } + +export function getContractVersionFromSwapQuote(quote: SwapQuote): ContractVersionValue { + if (quote.type === SwapQuoteType.Direct) { + return quote.quoteWithPool.pool.contractVersion; + } + + return CONTRACT_VERSION.V2; +} diff --git a/src/swap/v1_1/index.ts b/src/swap/v1_1/index.ts index 8fdfacd5..9d4e88e0 100644 --- a/src/swap/v1_1/index.ts +++ b/src/swap/v1_1/index.ts @@ -15,7 +15,11 @@ import {DEFAULT_FEE_TXN_NOTE} from "../../util/constant"; import {ALGO_ASSET_ID} from "../../util/asset/assetConstants"; import {PoolReserves, PoolStatus, V1PoolInfo} from "../../util/pool/poolTypes"; import {getAccountExcessWithinPool} from "../../util/account/accountUtils"; -import {SwapQuote, V1SwapExecution} from "../types"; +import { + DirectSwapQuote, + GenerateSwapTxnsWithoutRouterParams, + V1SwapExecution +} from "../types"; import {SwapType} from "../constants"; import {calculatePriceImpact, calculateSwapRate} from "../common/utils"; import {AssetWithIdAndAmount} from "../../util/asset/assetModels"; @@ -67,15 +71,7 @@ async function generateTxns({ assetOut, slippage, initiatorAddr -}: { - client: Algodv2; - pool: V1PoolInfo; - swapType: SwapType; - assetIn: AssetWithIdAndAmount; - assetOut: AssetWithIdAndAmount; - slippage: number; - initiatorAddr: string; -}): Promise { +}: GenerateSwapTxnsWithoutRouterParams): Promise { const suggestedParams = await client.getTransactionParams().do(); const poolAddress = pool.account.address(); const validatorAppCallArgs = [ @@ -182,8 +178,8 @@ function getQuote( reserves: PoolReserves, asset: AssetWithIdAndAmount, decimals: {assetIn: number; assetOut: number} -): SwapQuote { - let quote; +): DirectSwapQuote { + let quote: DirectSwapQuote; if (pool.status !== PoolStatus.READY) { throw new TinymanError({pool, asset}, "Trying to swap on a non-existent pool"); @@ -217,7 +213,7 @@ function getFixedInputSwapQuote({ reserves: PoolReserves; assetIn: AssetWithIdAndAmount; decimals: {assetIn: number; assetOut: number}; -}): SwapQuote { +}): DirectSwapQuote { const assetInAmount = BigInt(assetIn.amount); let assetOutID: number; @@ -359,7 +355,7 @@ function getFixedOutputSwapQuote({ reserves: PoolReserves; assetOut: AssetWithIdAndAmount; decimals: {assetIn: number; assetOut: number}; -}): SwapQuote { +}): DirectSwapQuote { const assetOutAmount = BigInt(assetOut.amount); let assetInID: number; diff --git a/src/swap/v2/constants.ts b/src/swap/v2/constants.ts index 011b6083..22f39e20 100644 --- a/src/swap/v2/constants.ts +++ b/src/swap/v2/constants.ts @@ -25,3 +25,7 @@ export const V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED = { [SwapType.FixedInput]: encodeString("fixed-input"), [SwapType.FixedOutput]: encodeString("fixed-output") } as const; + +export const V2_SWAP_ROUTER_APP_ARGS_ENCODED = { + ASSET_OPT_IN: encodeString("asset_opt_in") +}; diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index f19b4576..e6cdd42b 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -5,14 +5,19 @@ import { convertFromBaseUnits, sendAndWaitRawTransaction } from "../../util/util"; -import { - InitiatorSigner, - SignerTransaction, - SupportedNetwork -} from "../../util/commonTypes"; +import {InitiatorSigner, SignerTransaction} from "../../util/commonTypes"; import TinymanError from "../../util/error/TinymanError"; -import {PoolStatus, V2PoolInfo} from "../../util/pool/poolTypes"; -import {SwapQuote, V2SwapExecution} from "../types"; +import {PoolStatus} from "../../util/pool/poolTypes"; +import { + DirectSwapQuote, + GenerateSwapTxnsParams, + GetFixedInputSwapQuoteByContractVersionParams, + GetFixedOutputSwapQuoteByContractVersionParams, + GetSwapQuoteWithContractVersionParams, + SwapQuote, + SwapQuoteType, + V2SwapExecution +} from "../types"; import {SwapType} from "../constants"; import { V2_SWAP_APP_CALL_ARG_ENCODED, @@ -20,31 +25,23 @@ import { V2SwapTxnGroupIndices, V2_SWAP_APP_CALL_INNER_TXN_COUNT } from "./constants"; -import {poolUtils} from "../../util/pool"; import {isAlgo} from "../../util/asset/assetUtils"; import {calculatePriceImpact} from "../common/utils"; import {getAppCallInnerTxns} from "../../util/transaction/transactionUtils"; import {AssetWithIdAndAmount} from "../../util/asset/assetModels"; import {tinymanJSSDKConfig} from "../../config"; import {CONTRACT_VERSION} from "../../contract/constants"; +import {generateSwapRouterTxns, getSwapRoute} from "./router/swap-router"; + +async function generateTxns( + params: GenerateSwapTxnsParams +): Promise { + if (params.isUsingSwapRouter) { + return generateSwapRouterTxns(params); + } + + const {assetIn, assetOut, client, initiatorAddr, pool, slippage, swapType} = params; -async function generateTxns({ - client, - pool, - swapType, - assetIn, - assetOut, - initiatorAddr, - slippage -}: { - client: Algodv2; - pool: V2PoolInfo; - swapType: SwapType; - assetIn: AssetWithIdAndAmount; - assetOut: AssetWithIdAndAmount; - initiatorAddr: string; - slippage: number; -}): Promise { const suggestedParams = await client.getTransactionParams().do(); const poolAddress = pool.account.address(); const isAssetInAlgo = isAlgo(assetIn.id); @@ -77,7 +74,7 @@ async function generateTxns({ const appCallTxn = algosdk.makeApplicationNoOpTxnFromObject({ from: initiatorAddr, - appIndex: pool.validatorAppID!, + appIndex: pool.validatorAppID, appArgs: [ V2_SWAP_APP_CALL_ARG_ENCODED, V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED[swapType], @@ -132,22 +129,23 @@ function getSwapAppCallFeeAmount(swapType: SwapType) { */ async function execute({ client, - pool, + quote, txGroup, signedTxns, - network, assetIn }: { client: Algodv2; - pool: V2PoolInfo; - network: SupportedNetwork; + quote: SwapQuote; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; assetIn: AssetWithIdAndAmount; }): Promise { let [{confirmedRound, txnID}] = await sendAndWaitRawTransaction(client, [signedTxns]); const innerTxns = await getAppCallInnerTxns(client, txGroup); - const assetOutId = [pool.asset1ID, pool.asset2ID].filter((id) => id !== assetIn.id)[0]; + const assetOutId = + quote.type === SwapQuoteType.Direct + ? quote.quoteWithPool.quote.assetOutID + : Number(quote.route[quote.route.length - 1].quote.amount_out.asset.id); /** * If the swap type if Fixed Output, usually there will be a difference between * input amount and the actual used input amount. The change will be returned to the user @@ -172,12 +170,7 @@ async function execute({ amount: assetOutInnerTxn.aamt, id: assetOutId }, - pool: await poolUtils.v2.getPoolInfo({ - client, - network, - asset1ID: pool.asset1ID, - asset2ID: pool.asset2ID - }), + quote, txnID }; } @@ -191,12 +184,10 @@ async function execute({ * @param decimals.assetOut - Decimals quantity for the output asset * @returns A promise for the Swap quote */ -function getQuote( - type: SwapType, - pool: V2PoolInfo, - asset: AssetWithIdAndAmount, - decimals: {assetIn: number; assetOut: number} -): SwapQuote { +async function getQuote( + params: GetSwapQuoteWithContractVersionParams +): Promise { + const {asset, decimals, pool, type, isSwapRouterEnabled} = params; let quote: SwapQuote; if (pool.status !== PoolStatus.READY) { @@ -204,9 +195,19 @@ function getQuote( } if (type === SwapType.FixedInput) { - quote = getFixedInputSwapQuote({pool, assetIn: asset, decimals}); + quote = await getFixedInputSwapQuote({ + pool, + assetIn: asset, + decimals, + isSwapRouterEnabled + }); } else { - quote = getFixedOutputSwapQuote({pool, assetOut: asset, decimals}); + quote = await getFixedOutputSwapQuote({ + pool, + assetOut: asset, + decimals, + isSwapRouterEnabled + }); } return quote; @@ -215,15 +216,12 @@ function getQuote( /** * @returns A quote for a fixed input swap. Does NOT execute any transactions. */ -function getFixedInputSwapQuote({ +async function getFixedInputSwapQuote({ pool, assetIn, - decimals -}: { - pool: V2PoolInfo; - assetIn: AssetWithIdAndAmount; - decimals: {assetIn: number; assetOut: number}; -}): SwapQuote { + decimals, + isSwapRouterEnabled +}: GetFixedInputSwapQuoteByContractVersionParams): Promise { if (pool.status !== PoolStatus.READY) { throw new TinymanError({pool, assetIn}, "Trying to swap on a non-existent pool"); } @@ -253,7 +251,7 @@ function getFixedInputSwapQuote({ decimals }); - return { + const directSwapQuote: DirectSwapQuote = { assetInID: assetIn.id, assetInAmount, assetOutID, @@ -264,20 +262,45 @@ function getFixedInputSwapQuote({ convertFromBaseUnits(decimals.assetIn, Number(assetInAmount)), priceImpact }; + + if (isSwapRouterEnabled) { + const swapRoute = await getSwapRoute({ + amount: assetIn.amount, + assetInID: Number(assetIn.id), + assetOutID, + swapType: SwapType.FixedInput + }); + + if ( + swapRoute.length > 1 && + BigInt(swapRoute[swapRoute.length - 1].quote.amount_out.amount) > + directSwapQuote.assetOutAmount + ) { + return { + route: swapRoute, + type: SwapQuoteType.Router + }; + } + } + + return { + quoteWithPool: { + pool, + quote: directSwapQuote + }, + type: SwapQuoteType.Direct + }; } /** * @returns A quote for a fixed output swap. Does NOT execute any transactions. */ -function getFixedOutputSwapQuote({ +async function getFixedOutputSwapQuote({ pool, assetOut, - decimals -}: { - pool: V2PoolInfo; - assetOut: AssetWithIdAndAmount; - decimals: {assetIn: number; assetOut: number}; -}): SwapQuote { + decimals, + isSwapRouterEnabled +}: GetFixedOutputSwapQuoteByContractVersionParams): Promise { const assetOutAmount = BigInt(assetOut.amount); const totalFeeShare = pool.totalFeeShare!; let assetInID: number; @@ -302,7 +325,7 @@ function getFixedOutputSwapQuote({ decimals }); - return { + const directSwapQuote = { assetInID, assetInAmount: swapInputAmount, assetOutID: assetOut.id, @@ -313,6 +336,34 @@ function getFixedOutputSwapQuote({ convertFromBaseUnits(decimals.assetIn, Number(swapInputAmount)), priceImpact }; + + if (isSwapRouterEnabled) { + const swapRoute = await getSwapRoute({ + amount: assetOut.amount, + assetInID, + assetOutID: assetOut.id, + swapType: SwapType.FixedInput + }); + + if ( + swapRoute.length > 1 && + BigInt(swapRoute[swapRoute.length - 1].quote.amount_in.amount) < + directSwapQuote.assetInAmount + ) { + return { + route: swapRoute, + type: SwapQuoteType.Router + }; + } + } + + return { + quoteWithPool: { + pool, + quote: directSwapQuote + }, + type: SwapQuoteType.Direct + }; } function calculateFixedInputSwap({ diff --git a/src/swap/v2/router/index.ts b/src/swap/v2/router/index.ts new file mode 100644 index 00000000..05b32ead --- /dev/null +++ b/src/swap/v2/router/index.ts @@ -0,0 +1,2 @@ +export * from "./swap-router"; +export * from "./util"; diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts new file mode 100644 index 00000000..d13fa7c3 --- /dev/null +++ b/src/swap/v2/router/swap-router.ts @@ -0,0 +1,189 @@ +import algosdk, {ALGORAND_MIN_TX_FEE, getApplicationAddress} from "algosdk"; +import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; + +import {CONTRACT_VERSION} from "../../../contract/constants"; +import {tinymanContract_v2} from "../../../contract/v2/contract"; +import {isAlgo} from "../../../util/asset/assetUtils"; +import {SignerTransaction} from "../../../util/commonTypes"; +import {getValidatorAppID} from "../../../validator"; +import {SwapType} from "../../constants"; +import { + FetchSwapRouteQuotesPayload, + FetchSwapRouteQuotesResponse, + GenerateSwapRouterTxnsParams, + SwapRoute +} from "../../types"; +import { + V2SwapTxnGroupIndices, + V2_SWAP_APP_CALL_ARG_ENCODED, + V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED, + V2_SWAP_ROUTER_APP_ARGS_ENCODED +} from "../constants"; + +export async function generateSwapRouterAssetOptInTransaction({ + client, + routerAppID, + assetIDs, + accountAddress +}: { + client: AlgodClient; + routerAppID: number; + assetIDs: number[]; + accountAddress: string; +}): Promise { + const suggestedParams = await client.getTransactionParams().do(); + // We need to create a NoOp transaction for each asset we want to opt-in to + const assetOptInTxn = algosdk.makeApplicationNoOpTxnFromObject({ + from: accountAddress, + appIndex: routerAppID, + appArgs: [V2_SWAP_ROUTER_APP_ARGS_ENCODED.ASSET_OPT_IN], + foreignAssets: assetIDs, + suggestedParams + }); + // The number of inner transactions is the number of assets we're opting in to + const innerTransactionCount = assetIDs.length; + + // The fee for the transaction is the fee for the outer transaction (which is a NoOp) + // multiplied by the number of inner transactions + assetOptInTxn.fee = ALGORAND_MIN_TX_FEE * (innerTransactionCount + 1); + + console.log(assetOptInTxn.fee, "router app opt-in fee"); + + const txGroup = algosdk.assignGroupID([assetOptInTxn]); + + return [{txn: txGroup[0], signers: [accountAddress]}]; +} + +export async function generateSwapRouterTxns({ + initiatorAddr, + assetIn, + assetOut, + client, + intermediaryAssetID, + routerAppID, + network, + swapType +}: GenerateSwapRouterTxnsParams) { + const suggestedParams = await client.getTransactionParams().do(); + + const pool1LogicSig = tinymanContract_v2.generateLogicSigAccountForPool({ + asset1ID: assetIn.id, + asset2ID: intermediaryAssetID, + network + }); + const pool1Address = pool1LogicSig.address(); + const pool2LogicSig = tinymanContract_v2.generateLogicSigAccountForPool({ + asset1ID: intermediaryAssetID, + asset2ID: assetOut.id, + network + }); + const pool2Address = pool2LogicSig.address(); + + const isAssetInAlgo = isAlgo(assetIn.id); + + const inputTxn = isAssetInAlgo + ? algosdk.makePaymentTxnWithSuggestedParamsFromObject({ + from: initiatorAddr, + to: getApplicationAddress(routerAppID), + amount: assetIn.amount, + suggestedParams + }) + : algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ + from: initiatorAddr, + to: getApplicationAddress(routerAppID), + amount: assetIn.amount, + assetIndex: assetIn.id, + suggestedParams + }); + + const routerAppCallTxn = algosdk.makeApplicationNoOpTxnFromObject({ + from: initiatorAddr, + appIndex: routerAppID, + appArgs: [ + V2_SWAP_APP_CALL_ARG_ENCODED, + V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED[swapType], + algosdk.encodeUint64(assetOut.amount) + ], + foreignApps: [getValidatorAppID(network, CONTRACT_VERSION.V2)], + foreignAssets: [assetIn.id, intermediaryAssetID, assetOut.id], + accounts: [pool1Address, pool2Address], + suggestedParams + }); + + const innerTransactionCount = swapType === SwapType.FixedInput ? 7 : 8; + + routerAppCallTxn.fee = ALGORAND_MIN_TX_FEE * (innerTransactionCount + 1); + + const txGroup = algosdk.assignGroupID([inputTxn, routerAppCallTxn]); + + return [ + { + txn: txGroup[V2SwapTxnGroupIndices.INPUT_TXN], + signers: [initiatorAddr] + }, + { + txn: txGroup[V2SwapTxnGroupIndices.APP_CALL_TXN], + signers: [initiatorAddr] + } + ]; +} + +export async function getSwapRouterAppOptInRequiredAssetIDs({ + client, + routerAppID, + assetIDs +}: { + client: AlgodClient; + routerAppID: number; + assetIDs: number[]; +}) { + const swapRouterAppAddress = getApplicationAddress(routerAppID); + const accountInfo = await client.accountInformation(swapRouterAppAddress).do(); + const appOptedInAssetIDs = accountInfo.assets.map( + (asset: {"asset-id": number}) => asset["asset-id"] + ); + const requiredAssetIDs = assetIDs.filter( + (assetID: number) => !appOptedInAssetIDs.includes(assetID) + ); + + return requiredAssetIDs; +} + +export async function getSwapRoute({ + amount, + assetInID, + assetOutID, + swapType +}: { + assetInID: number; + assetOutID: number; + swapType: SwapType; + amount: number | bigint; +}): Promise { + const payload: FetchSwapRouteQuotesPayload = { + asset_in_id: String(assetInID), + asset_out_id: String(assetOutID), + swap_type: swapType, + amount: String(amount) + }; + + const response = await fetch( + "https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/", + { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify(payload) + } + ); + + // TODO: Handle all errors properly + if (response.status !== 200) { + throw new Error("Failed to fetch swap route quotes"); + } + + const {route}: FetchSwapRouteQuotesResponse = await response.json(); + + return route; +} diff --git a/src/swap/v2/router/util.ts b/src/swap/v2/router/util.ts new file mode 100644 index 00000000..5588e876 --- /dev/null +++ b/src/swap/v2/router/util.ts @@ -0,0 +1,18 @@ +import {convertFromBaseUnits} from "../../../util/util"; +import {SwapRoute, SwapRouterQuote} from "../../types"; + +export function getSwapFeesFromSwapRoute(route: SwapRouterQuote[]): number { + return route.reduce((acc, quote) => acc + Number(quote.swap_fees.amount), 0); +} + +export function getSwapRouteRate(route: SwapRoute) { + const [assetIn, assetOut] = [ + route[0].quote.amount_in, + route[route.length - 1].quote.amount_out + ]; + + return ( + convertFromBaseUnits(assetOut.asset.decimals, Number(assetOut.amount)) / + convertFromBaseUnits(assetIn.asset.decimals, Number(assetIn.amount)) + ); +} diff --git a/src/util/asset/assetUtils.ts b/src/util/asset/assetUtils.ts index 00c18283..82847e81 100644 --- a/src/util/asset/assetUtils.ts +++ b/src/util/asset/assetUtils.ts @@ -73,3 +73,10 @@ export function sortAssetIds(asset1ID: number, asset2ID: number): number[] { export function isAlgo(id: number | string) { return Number(id) === ALGO_ASSET_ID; } + +/** + * Returns asset.asset_id + */ +export function getAssetId(asset: {id: string | number}): number { + return Number(asset.id); +} From 62921e2a5305ebdca35fbd6468d31b536461f8d9 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Mon, 27 Feb 2023 17:08:59 +0300 Subject: [PATCH 02/39] chore: get build --- dist/contract/index.d.ts | 2 +- dist/index.d.ts | 4 +- dist/index.js | 4 +- dist/swap/index.d.ts | 54 +++--------- dist/swap/types.d.ts | 127 +++++++++++++++++++++++++-- dist/swap/utils.d.ts | 63 ++++--------- dist/swap/v1_1/index.d.ts | 18 ++-- dist/swap/v2/constants.d.ts | 3 + dist/swap/v2/index.d.ts | 44 ++-------- dist/swap/v2/router/index.d.ts | 2 + dist/swap/v2/router/swap-router.d.ts | 26 ++++++ dist/swap/v2/router/types.d.ts | 14 +++ dist/swap/v2/router/util.d.ts | 3 + dist/util/asset/assetUtils.d.ts | 6 ++ 14 files changed, 219 insertions(+), 151 deletions(-) create mode 100644 dist/swap/v2/router/index.d.ts create mode 100644 dist/swap/v2/router/swap-router.d.ts create mode 100644 dist/swap/v2/router/types.d.ts create mode 100644 dist/swap/v2/router/util.d.ts diff --git a/dist/contract/index.d.ts b/dist/contract/index.d.ts index 75a11a79..775db32c 100644 --- a/dist/contract/index.d.ts +++ b/dist/contract/index.d.ts @@ -1,3 +1,3 @@ import { ContractVersionValue } from "./types"; export declare function getIsV2ContractVersion(contractVersion: ContractVersionValue): boolean; -export declare function getContract(contractVersion: ContractVersionValue): import("./v1_1/contract").TinymanContractV1_1 | import("./v2/contract").TinymanContractV2; +export declare function getContract(contractVersion: ContractVersionValue): import("./v2/contract").TinymanContractV2 | import("./v1_1/contract").TinymanContractV1_1; diff --git a/dist/index.d.ts b/dist/index.d.ts index 156a96b2..ca7dc8d8 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,5 +1,6 @@ export type { InitiatorSigner, SignerTransaction, SupportedNetwork } from "./util/commonTypes"; export { BASE_MINIMUM_BALANCE, MINIMUM_BALANCE_REQUIRED_PER_ASSET, MINIMUM_BALANCE_REQUIRED_PER_APP, MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA, MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE, MINIMUM_ADD_LIQUIDITY_AMOUNT } from "./util/constant"; +export * from "./swap/v2/router"; export { applySlippageToAmount, ASSET_OPT_IN_PROCESS_TXN_COUNT, convertFromBaseUnits, convertToBaseUnits, sendAndWaitRawTransaction, getTxnGroupID, sumUpTxnFees } from "./util/util"; export { generateOptIntoAssetTxns } from "./util/asset/assetUtils"; export type { AccountAsset, TinymanAnalyticsApiAsset, IndexerAssetInformation } from "./util/asset/assetModels"; @@ -26,10 +27,11 @@ export type { V2RemoveLiquidityQuote, V2SingleAssetRemoveLiquidityQuote, V2Remov export { V1_1_REMOVE_LIQUIDITY_TXN_COUNT } from "./remove-liquidity/v1_1/constants"; export { V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT } from "./remove-liquidity/v2/constants"; export { RemoveLiquidity } from "./remove-liquidity"; -export type { SwapQuote, SwapQuoteWithPool, V1SwapExecution, V2SwapExecution } from "./swap/types"; +export type { SwapQuote, V1SwapExecution, V2SwapExecution, DirectSwapQuote, SwapRoute, GenerateSwapTxnsParams } from "./swap/types"; export { getSwapTotalFee } from "./swap/utils"; export { SwapType } from "./swap/constants"; export { Swap } from "./swap"; +export { SwapQuoteType } from "./swap/types"; export { redeemExcessAsset, redeemAllExcessAsset, generateRedeemTxns, REDEEM_PROCESS_TXN_COUNT } from "./redeem"; export { prepareCommitTransactions, getStakingAppID } from "./stake"; export { tinymanJSSDKConfig } from "./config"; diff --git a/dist/index.js b/dist/index.js index 74f9ce71..13bd8233 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),s=Uint8Array.from([1]);function a(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--s){var a=this.tryEntries[s],o=a.completion;if("root"===a.tryLoc)return r("end");if(a.tryLoc<=this.prev){var i=n.call(a,"catchLoc"),u=n.call(a,"finallyLoc");if(i&&u){if(this.prev=0;--r){var s=this.tryEntries[r];if(s.tryLoc<=this.prev&&n.call(s,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var s=r.arg;_(n)}return s}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function u(t,e,n,r,s,a,o){try{var i=t[a](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,s)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,s){var a=t.apply(e,n);function o(t){u(a,r,s,o,i,"next",t)}function i(t){u(a,r,s,o,i,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:s}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,a=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw a}}}}var E="- would result negative",S="logic eval error:",N="exceeds schema integer count",O=/transaction \w+:/,P=function(t){m(n,T(Error));var e=y(n);function n(t,r){var s;p(this,n);for(var a=arguments.length,o=new Array(a>2?a-2:0),i=2;i1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var s="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*s))}catch(t){throw new Error(t.message)}return r}function G(t,e){var n=Number(t);return W({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function q(t,e){return W({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function W(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var s=v(J(e),2),a=s[0],o=s[1],i=v(J(Math.round(Number(z(a,o+r)))),2),u=i[0],c=i[1];return Number(z(u,c-r))}return Math.round(e)}function z(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function J(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(i().mark((function t(e,n){var r,s,a,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=b(n),t.prev=3,s.s();case 5:if((a=s.n()).done){t.next=18;break}return o=a.value,t.next=9,e.sendRawTransaction(o).do();case 9:return u=t.sent,c=u.txId,t.next=13,j(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new P(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function H(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function Z(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function $(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function tt(t){return(new TextEncoder).encode(t)}var et={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},nt={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"};function rt(){return(rt=c(i().mark((function t(e){var n,s,a,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.assetID,a=e.initiatorAddr,t.prev=1,t.next=4,n.getTransactionParams().do();case 4:return o=t.sent,u=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:a,assetIndex:s,amount:0,suggestedParams:o}),t.abrupt("return",[{txn:u,signers:[a]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new P(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function st(t,e){var n=Number(t.id),r=Number(e.id);return n>r?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function at(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function ot(t){return 0===Number(t)}var it,ut={V1_1:"v1_1",V2:"v2"},ct={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},pt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},lt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return tt("tinyman/".concat(t===ut.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),dt=(f(it={},ut.V1_1,{testnet:62368684,mainnet:552635992}),f(it,ut.V2,{testnet:148607e3,mainnet:1002541853}),it);function ft(t,e){var n=dt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function mt(){return(mt=c(i().mark((function t(e){var n,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:ft(s,a),note:lt.getAppCallTxnNoteWithClientName(a),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function gt(){return(gt=c(i().mark((function t(e){var n,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:ft(s,a),note:lt.getAppCallTxnNoteWithClientName(a),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var It=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),At=new(function(n){m(s,It);var r=y(s);function s(t,e){var n;return p(this,s),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(s,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=ft(n.network,ut.V1_1),s=v(at(n.asset1ID,n.asset2ID),2),a=s[0],o=s[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:a,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(i().mark((function t(s,a){var u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:u=t.sent,s(o(o({},u),{},{minimum_required_balance:wt(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),a(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function _t(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?k({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function wt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Dt,bt,Et=tt("e");function St(t){return Nt.apply(this,arguments)}function Nt(){return(Nt=c(i().mark((function n(s){var a,o,u,c,p,l,d,f,m,g,I,A,x,T,h,y,v,_,w,D,E;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.pool,u=s.accountAddr,n.next=3,a.accountInformation(u).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=b(p),n.prev=10,g.s();case 12:if((I=g.n()).done){n.next=31;break}if((A=I.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:T=k({stateArray:x}),h=e.fromByteArray(L([r.default.decodeAddress(m).publicKey,Et,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(L([r.default.decodeAddress(m).publicKey,Et,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(L([r.default.decodeAddress(m).publicKey,Et,r.default.encodeUint64(o.poolTokenID)])),_=T[h],w=T[y],D=T[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof D&&(f=D);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((E={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ot(){return(Ot=c(i().mark((function n(s){var a,o,u,c,p,l,d,f,m,g,I,A,x,T,h;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.accountAddr,u=s.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==u})),d=[],l&&l["key-value"])for(f=k({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mLt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Lt,"]): ").concat(B.issuedLiquidity));case 48:return n.abrupt("return",B);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d=arguments;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(a=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",a[r]);case 4:return t.next=6,vt(n,r);case 6:return o=t.sent,u=_t(o,ft(s,ut.V1_1)),c=null,u&&(l=o["created-assets"][0],p=l.index,c={asset1ID:u[Pt[ut.V1_1].asset1],asset2ID:u[Pt[ut.V1_1].asset2],poolTokenID:p},a[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Mt.apply(this,arguments)}var Ct=Object.freeze({__proto__:null,getPoolInfo:Ft,getPoolReserves:function(t,e){return Rt.apply(this,arguments)},getPoolAssets:function(t){return Mt.apply(this,arguments)}});function Vt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,a=e.asset2ID,o=yt(ut.V2),u=o.generateLogicSigAccountForPool(e),c=ft(r,ut.V2),p=u.address(),l=at(s,a),t.next=8,vt(n,p);case 8:return d=t.sent,f=_t(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:ut.V2},f&&(m.asset1ProtocolFees=BigInt(f[Pt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Pt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Pt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Pt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Pt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Pt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Pt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Pt.v2.totalFeeShare]),m.poolTokenID=Number(f[Pt.v2.poolTokenID]),m.asset1ID=Number(f[Pt.v2.asset1]),m.asset2ID=Number(f[Pt.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function jt(){return(jt=c(i().mark((function t(e,n){var r,s,a;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,vt(e,n.account.address());case 2:return r=t.sent,s=_t(r,n.validatorAppID),a={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(a.asset1=BigInt(s[Pt.v2.asset1Reserves]),a.asset2=BigInt(s[Pt.v2.asset2Reserves]),a.issuedLiquidity=BigInt(s[Pt.v2.issuedPoolTokens])),t.abrupt("return",a);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ut(){return(Ut=c(i().mark((function t(e){var n,r,s,a,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,vt(n,r);case 3:return a=t.sent,o=_t(a,ft(s,ut.V2)),u=null,o&&(u={asset1ID:o[Pt[ut.V2].asset1],asset2ID:o[Pt[ut.V2].asset2],poolTokenID:o[Pt[ut.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolInfo:Vt,getPoolReserves:function(t,e){return jt.apply(this,arguments)},getPoolAssets:function(t){return Ut.apply(this,arguments)}});function Gt(t){return Boolean(t&&!(t.asset1+t.asset2))}var qt,Wt,zt=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Gt(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=G(t.asset1,e.asset1)/G(t.asset2,e.asset2)),r},isPoolEmpty:Gt,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Ft(t),Vt(t)])}}),Jt=o((f(qt={},ut.V1_1,o(o({},Ct),zt)),f(qt,ut.V2,o(o({},Qt),zt)),qt),zt);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Wt||(Wt={}));var Yt=96e4,Kt=859e3;function Ht(){return(Ht=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f,m,g,I,A,x,T,h,y,_,w,D,b,E,S,N;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.asset_1,o=e.asset_2,u=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=st(a,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],I=g.id,A=g.unit_name,x=ot(I),T=ft(s,ut.V1_1),h=At.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:I}),y=h.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:T,note:lt.getAppCallTxnNoteWithClientName(ut.V1_1),appArgs:[tt("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(I)],foreignAssets:x?[f]:[I],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:nt.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(A),assetURL:"https://tinyman.org",suggestedParams:c}),D=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),b=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Zt(x),suggestedParams:c}),(E=[])[Wt.FUNDING_TXN]=b,E[Wt.VALIDATOR_APP_CALL]=_,E[Wt.POOL_TOKEN_CREATE]=w,E[Wt.ASSET1_OPT_IN]=D,x||(E[Wt.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:I,amount:0,suggestedParams:c})),S=r.default.assignGroupID(E),N=[{txn:S[Wt.FUNDING_TXN],signers:[u]},{txn:S[Wt.VALIDATOR_APP_CALL],signers:[y]},{txn:S[Wt.POOL_TOKEN_CREATE],signers:[y]},{txn:S[Wt.ASSET1_OPT_IN],signers:[y]}],S[Wt.ASSET2_OPT_IN]&&N.push({txn:S[Wt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",N);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zt(t){return t?Yt:Kt}function $t(){return($t=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f,m,g,I,A,x;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=at(o,u),f=v(d,2),m=f[0],g=f[1],I=At.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),A=[],x=n.map((function(t,e){if(e===Wt.FUNDING_TXN)return A.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,I),s=n.txID,a=n.blob;return A.push(s),a})),t.abrupt("return",{signedTxns:x,txnIDs:A});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function te(t){return ee.apply(this,arguments)}function ee(){return(ee=c(i().mark((function t(e){var n,r,s,a,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,j(n,s[Wt.POOL_TOKEN_CREATE]);case 6:if(a=t.sent,"number"==typeof(o=a["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new P(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function ne(){return(ne=c(i().mark((function t(e){var n,r,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,te({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var re,se={generateTxns:function(t){return Ht.apply(this,arguments)},signTxns:function(t){return $t.apply(this,arguments)},execute:function(t){return ne.apply(this,arguments)},getBootstrapFundingTxnAmount:Zt};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(re||(re={}));var ae=5,oe=6;function ie(t,e){return ue.apply(this,arguments)}function ue(){return(ue=c(i().mark((function e(n,r){var s,a,o;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(a=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,a,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ce(t,e){return pe.apply(this,arguments)}function pe(){return(pe=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ie(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function le(){return(le=c(i().mark((function e(n){var s,a,o,u,c,p,l,d,f,m,g,I,A,x,T,h,y,_,w,D;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.network,o=n.asset_1,u=n.asset_2,c=n.initiatorAddr,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=ft(a,ut.V2),d=t.getApplicationAddress(l),f=st(o,u),m=v(f,2),g=m[0].id,I=m[1].id,e.next=9,Jt.v2.getPoolInfo({client:s,network:a,asset1ID:g,asset2ID:I});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(u.unit_name," already exists"));case 12:return A=ht.generateLogicSigAccountForPool({network:a,asset1ID:g,asset2ID:I}),x=A.address(),T=ot(I),(h=r.default.makeApplicationOptInTxnFromObject({from:x,appIndex:l,appArgs:[tt("bootstrap")],note:lt.getAppCallTxnNoteWithClientName(ut.V2),foreignAssets:[g,I],rekeyTo:d,suggestedParams:p})).fee=fe(T),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:x,amount:de(T),suggestedParams:p}),(_=[])[re.FUNDING_TXN]=y,_[re.VALIDATOR_APP_CALL]=h,w=r.default.assignGroupID(_),(D=[])[re.FUNDING_TXN]={txn:w[re.FUNDING_TXN],signers:[c]},D[re.VALIDATOR_APP_CALL]={txn:w[re.VALIDATOR_APP_CALL],signers:[x]},e.abrupt("return",D);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function de(t){return function(t,e){var n=yt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(ut.V2,t)+fe(t)+1e5}function fe(e){return((e?ae:oe)+1)*t.ALGORAND_MIN_TX_FEE}function me(){return(me=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f,m,g,I,A,x;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=at(o,u),f=v(d,2),m=f[0],g=f[1],I=ht.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),A=[],x=n.map((function(t,e){if(e===re.FUNDING_TXN)return A.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,I),s=n.txID,a=n.blob;return A.push(s),a})),t.abrupt("return",{signedTxns:x,txnIDs:A});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ge(){return(ge=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,ie(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Pt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new P(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Ie,Ae={generateTxns:function(t){return le.apply(this,arguments)},signTxns:function(t){return me.apply(this,arguments)},execute:function(t){return ge.apply(this,arguments)},getBootstrapFundingTxnAmount:de,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+de(e)}};var xe,Te=(f(Ie={},ut.V1_1,se),f(Ie,ut.V2,Ae),f(Ie,"generateTxns",(function(t){return t.contractVersion===ut.V1_1?se.generateTxns(t):Ae.generateTxns(t)})),f(Ie,"signTxns",(function(t){return t.contractVersion===ut.V1_1?se.signTxns(t):Ae.signTxns(t)})),f(Ie,"execute",(function(t){return t.contractVersion===ut.V1_1?se.execute(t):Ae.execute(t)})),f(Ie,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===ut.V1_1?se.getBootstrapFundingTxnAmount(n):Ae.getBootstrapFundingTxnAmount(n)})),Ie);exports.V1_1AddLiquidityTxnIndices=void 0,(xe=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[xe.FEE_TXN=0]="FEE_TXN",xe[xe.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",xe[xe.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",xe[xe.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",xe[xe.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var he,ye,ve,_e,we=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(_e=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",_e.FLEXIBLE="flexible",_e.INITIAL="initial";var De,be=(f(he={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(he,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(he,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),he),Ee=(f(ye={},exports.V2AddLiquidityType.INITIAL,1),f(ye,exports.V2AddLiquidityType.SINGLE,2),f(ye,exports.V2AddLiquidityType.FLEXIBLE,2),ye),Se=(f(ve={},exports.V2AddLiquidityType.INITIAL,3),f(ve,exports.V2AddLiquidityType.FLEXIBLE,3),f(ve,exports.V2AddLiquidityType.SINGLE,2),ve);function Ne(t){var e=t.assetIn,n=t.assetOut;return G(n.decimals,Number(n.amount))/G(e.decimals,Number(e.amount))}function Oe(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,s=t.assetOut,a=Ne({assetIn:r,assetOut:s}),o=G(s.decimals,Number(n))/G(r.decimals,Number(e));return W({decimalPlaces:5},Math.abs(a/o-1))}function Pe(t){var e,n,r,s,a=t.reserves,o=t.totalFeeShare,i=t.asset1Amount,u=t.asset2Amount,c=t.decimals,p=a.asset1*a.asset2,l=a.asset1+BigInt(i),d=a.asset2+BigInt(u),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*a.issuedLiquidity*a.issuedLiquidity/p))))),g=m-a.issuedLiquidity,I=g*l/m,A=g*d/m,x=BigInt(i)-I,T=BigInt(u)-A;if(x>T){var h=x;r=BigInt(Math.abs(Math.min(Number(T),0))),e=!0,n=h+(s=ke(h,o)),g-=s*m/(l*BigInt(2))}else{var y=T;r=BigInt(Math.abs(Math.min(Number(x),0))),e=!1,n=y+(s=ke(y,o)),g-=s*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:s,swapPriceImpact:Oe({inputSupply:e?a.asset1:a.asset2,outputSupply:e?a.asset2:a.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function ke(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Le(e){return(Ee[e]+1)*t.ALGORAND_MIN_TX_FEE}var Fe=tt("add_liquidity"),Be=(f(De={},ut.V1_1,[tt("mint")]),f(De,ut.V2,{INITIAL_LIQUIDITY:[tt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Fe,tt("single")],FLEXIBLE_MODE:[Fe,tt("flexible")]}),De);function Re(){return(Re=c(i().mark((function t(e){var n,a,o,u,c,p,l,d,f,m,g,I,A,x,T,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,o=e.poolAddress,u=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=Q("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:ft(a,ut.V1_1),appArgs:Be.v1_1,accounts:[d],note:lt.getAppCallTxnNoteWithClientName(ut.V1_1),foreignAssets:0==c.id?[u.id,p.id]:[u.id,c.id,p.id],suggestedParams:m}),I=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:m}),A=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),T=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+x.fee,note:s,suggestedParams:m}),h=r.default.assignGroupID([T,g,I,A,x]),t.abrupt("return",[{txn:h[0],signers:[d]},{txn:h[1],signers:[o]},{txn:h[2],signers:[d]},{txn:h[3],signers:[d]},{txn:h[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Me(){return(Me=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,o=n.account,t.next=4,a([s]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ce(){return(Ce=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,I,A,x,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.signedTxns,o=e.initiatorAddr,t.prev=1,u=BigInt(s[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,St({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,Y(n,[a]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=H(s),I=Z(s),t.next=17,St({client:n,pool:r,accountAddr:o});case 17:return A=t.sent,(x=A.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:A.excessPoolTokens},txnID:m,groupID:I});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(T=new P(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&T.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),T;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Ve=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,s=t.asset2In;if(0n===n.issuedLiquidity){var a=BigInt(Math.floor(Math.sqrt(Number(r)*Number(s))));if(a<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(a,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(s),poolTokenID:e.poolTokenID,poolTokenOut:a-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(s)*n.issuedLiquidity/n.asset2,u=os+BigInt(1e3)?(n=s*e.asset1/a,r=s*e.asset2/a):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function vn(){return(vn=c(i().mark((function e(n){var s,a,o,u,c,p,l,d,f,m,g,I,A,x;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.poolTokenIn,u=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.account.address(),m=a.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:m,amount:o,suggestedParams:d}),(I=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:a.validatorAppID,note:lt.getAppCallTxnNoteWithClientName(ut.V2),appArgs:[en,r.default.encodeUint64(Q("negative",l,c)),r.default.encodeUint64(Q("negative",l,p))],accounts:[f],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(A=[])[$e.ASSET_TRANSFER_TXN]=g,A[$e.APP_CALL_TXN]=I,x=r.default.assignGroupID(A),e.abrupt("return",[{txn:x[$e.ASSET_TRANSFER_TXN],signers:[u]},{txn:x[$e.APP_CALL_TXN],signers:[u]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function _n(){return(_n=c(i().mark((function e(n){var s,a,o,u,c,p,l,d,f,m,g,I,A,x,T,h,y,v,_;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.initiatorAddr,u=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.asset1ID,m=a.asset2ID,g=a.account.address(),I=a.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,x=0,T=Q("negative",l,p),c!==f){e.next=17;break}A=T,x=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}A=0,x=T,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return h=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:I,amount:u,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:lt.getAppCallTxnNoteWithClientName(ut.V2),appArgs:[en,r.default.encodeUint64(A),r.default.encodeUint64(x)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[$e.ASSET_TRANSFER_TXN]=h,v[$e.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[$e.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[$e.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function wn(){return(wn=c(i().mark((function t(e){var n,r,s,a,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,s=e.txGroup,a=e.signedTxns,t.next=3,Y(r,[a]);case 3:return o=t.sent,u=v(o,1),c=u[0].txnID,t.next=8,ce(r,s);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Dn,bn,En={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,s=BigInt(r),a=yn(s,n),o=a.asset1OutputAmount,i=a.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:s}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,s=t.poolTokenIn,a=t.assetOutID,o=t.decimals,i=BigInt(s),u=yn(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(a===n.asset1ID){var d=hn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(a!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var I=hn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),A=I.swapOutputAmount,x=I.totalFeeAmount,T=I.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:p+A},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:A},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:T}}}return e},generateTxns:function(t){return vn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return _n.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return wn.apply(this,arguments)}},Sn=(f(Dn={},ut.V1_1,pn),f(Dn,ut.V2,En),Dn),Nn=3n,On=1000n;function Pn(){return(Pn=c(i().mark((function t(e){var n,s,a,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,t.next=3,a([s]);case 3:return o=t.sent,u=v(o,2),c=u[0],p=u[1],l=s.map((function(t,e){return e===bn.FEE_TXN_INDEX?c:e===bn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function kn(){return(kn=c(i().mark((function t(e){var n,a,o,u,c,p,l,d,f,m,g,I,A,x,T,h,y;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.pool,o=e.swapType,u=e.assetIn,c=e.assetOut,p=e.slippage,l=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return d=t.sent,f=a.account.address(),m=[tt("swap"),o===exports.SwapType.FixedInput?tt("fi"):tt("fo")],g=r.default.makeApplicationNoOpTxnFromObject({from:f,appIndex:a.validatorAppID,appArgs:m,accounts:[l],note:lt.getAppCallTxnNoteWithClientName(ut.V1_1),foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:d}),I=o===exports.SwapType.FixedOutput?Q("positive",p,u.amount):u.amount,A=0===u.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:I,suggestedParams:d}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:u.id,amount:I,suggestedParams:d}),x=o===exports.SwapType.FixedInput?Q("negative",p,c.amount):c.amount,T=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:l,amount:x,suggestedParams:d}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:l,assetIndex:c.id,amount:x,suggestedParams:d}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:g.fee+T.fee,note:s,suggestedParams:d}),y=r.default.assignGroupID([h,g,A,T]),t.abrupt("return",[{txn:y[0],signers:[l]},{txn:y[1],signers:[f]},{txn:y[2],signers:[l]},{txn:y[3],signers:[f]}]);case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ln(t){var e,n,r,s=t.pool,a=t.reserves,i=t.assetIn,u=t.decimals,c=BigInt(i.amount);i.id===s.asset1ID?(e=s.asset2ID,n=a.asset1,r=a.asset2):(e=s.asset1ID,n=a.asset2,r=a.asset1);var p=c*Nn/On,l=r-n*r/(n+(c-p));if(l>r)throw new Error("Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:u.assetIn},assetOut:{amount:l,decimals:u.assetOut}};return{round:a.round,assetInID:i.id,assetInAmount:c,assetOutID:e,assetOutAmount:l,swapFee:Number(p),rate:Ne(d),priceImpact:Oe(o({inputSupply:n,outputSupply:r},d))}}function Fn(t){return Bn.apply(this,arguments)}function Bn(){return(Bn=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,I,A,x;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,St({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Y(n,[s]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,St({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(I=c.excessAsset1,A=g.excessAsset1):(I=c.excessAsset2,A=g.excessAsset2),(x=A-I)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+x,excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:A},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rn(t){var e,n,r,s=t.pool,a=t.reserves,o=t.assetOut,i=t.decimals,u=BigInt(o.amount);if(o.id===s.asset1ID?(e=s.asset2ID,n=a.asset2,r=a.asset1):(e=s.asset1ID,n=a.asset1,r=a.asset2),u>r)throw new Error("Output amount exceeds available liquidity.");var c=n*r/(r-u)-n,p=c*On/(On-Nn),l=p-c,d=G(i.assetOut,Number(u))/G(i.assetIn,Number(p)),f=G(i.assetOut,Number(r))/G(i.assetIn,Number(n)),m=W({decimalPlaces:5},Math.abs(d/f-1));return{round:a.round,assetInID:e,assetInAmount:p,assetOutID:o.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m}}function Mn(t){return Cn.apply(this,arguments)}function Cn(){return(Cn=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,I,A,x;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,St({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Y(n,[s]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,St({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,a.id===r.asset1ID?(I=c.excessAsset1,A=g.excessAsset1):(I=c.excessAsset2,A=g.excessAsset2),(x=A-I)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount)-x,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:a.id,excessAmountForSwap:x,totalExcessAmount:A},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Vn(){return(Vn=c(i().mark((function t(e){var n,r,s,a,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,s=e.swapType,a=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new P({pool:r,swapType:s,txGroup:a},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:a[bn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:a[bn.ASSET_IN_TXN_INDEX].txn.amount},l={id:a[bn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:a[bn.ASSET_OUT_TXN_INDEX].txn.amount},s!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,Fn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Mn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:Z(a),fees:H(a)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new P(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(bn||(bn={}));var Xn,jn={getQuote:function(t,e,n,r,s){if(e.status!==exports.PoolStatus.READY)throw new P({pool:e,asset:r},"Trying to swap on a non-existent pool");return t===exports.SwapType.FixedInput?Ln({pool:e,reserves:n,assetIn:r,decimals:s}):Rn({pool:e,reserves:n,assetOut:r,decimals:s})},getFixedInputSwapQuote:Ln,getFixedOutputSwapQuote:Rn,generateTxns:function(t){return kn.apply(this,arguments)},signTxns:function(t){return Pn.apply(this,arguments)},execute:function(t){return Vn.apply(this,arguments)},executeFixedOutputSwap:Mn},Un=4*t.ALGORAND_MIN_TX_FEE;function Qn(t){return t.filter((function(t){return!Gt(t.pool.reserves)})).sort((function(t,e){return e.quote.rate-t.quote.rate}))[0]}var Gn=(f(Xn={},ut.V1_1,jn),f(Xn,ut.V2,hn),f(Xn,"getQuote",(function(t){if(t.pools.every((function(t){return Gt(t.reserves)})))throw new Error("No pools available for swap");return t.type===exports.SwapType.FixedInput?(n=(e=t).pools,r=e.assetIn,s=e.assetOut,a=e.amount,Qn(n.map((function(t){var e={pool:t.info,assetIn:{amount:a,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves};return{pool:t,quote:t.info.contractVersion===ut.V1_1?jn.getFixedInputSwapQuote(e):hn.getFixedInputSwapQuote(e)}})))):function(t){var e=t.pools,n=t.assetIn,r=t.assetOut,s=t.amount;return Qn(e.map((function(t){var e={pool:t.info,assetOut:{amount:s,id:Number(r.id)},decimals:{assetIn:n.decimals,assetOut:r.decimals},reserves:t.reserves};return{pool:t,quote:t.info.contractVersion===ut.V1_1?jn.getFixedOutputSwapQuote(e):hn.getFixedOutputSwapQuote(e)}})))}(t);var e,n,r,s,a})),f(Xn,"generateTxns",(function(t){return t.pool.contractVersion===ut.V1_1?jn.generateTxns(t):hn.generateTxns(t)})),f(Xn,"signTxns",(function(t){return t.pool.contractVersion===ut.V1_1?jn.signTxns(t):hn.signTxns(t)})),f(Xn,"execute",(function(t){return t.contractVersion===ut.V1_1?jn.execute(t):hn.execute(t)})),Xn);function qn(){return(qn=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.initiatorSigner,t.prev=1,t.next=4,Wn({txGroup:s,pool:r,initiatorSigner:a});case 4:return o=t.sent,t.next=7,Y(n,[o]);case 7:return u=t.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:H(s),confirmedRound:d,txnID:l,groupID:Z(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new P(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function Wn(t){return zn.apply(this,arguments)}function zn(){return(zn=c(i().mark((function t(e){var n,s,a,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.pool,a=e.initiatorSigner,t.next=3,a([n]);case 3:return o=t.sent,u=v(o,1),c=u[0],p=s.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jn(){return Jn=c(i().mark((function t(e){var n,s,a,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.data,a=e.initiatorSigner,t.prev=1,o=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:H(e),groupID:Z(e),lsig:n.account.lsig}})),t.next=5,a(o.map((function(t){return t.txns})));case 5:return u=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var s=c(i().mark((function s(a,o){var c,p,l,d,f,m;return i().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:return s.prev=0,c=t.txns.map((function(n,s){return 0===s?u[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),s.next=4,Y(n,[c]);case 4:p=s.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,a({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(0),o(s.t0);case 15:case"end":return s.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new P(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jn.apply(this,arguments)}function Yn(){return(Yn=c(i().mark((function t(e){var n,a,o,u,c,p,l,d,f,m,g;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.pool,o=e.assetID,u=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=a.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:a.validatorAppID,appArgs:[tt("redeem")],note:lt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[c],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(u),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(u),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:s,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Kn(e){var n=e.suggestedParams,r=e.stakingAppID,s=e.initiatorAddr,a=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:s,suggestedParams:n,foreignAssets:[a],accounts:[o.accountAddress],appArgs:[tt("commit"),u],note:L([tt("tinymanStaking/v1:b"),c,t.encodeUint64(a),u])})}function Hn(){return(Hn=c(i().mark((function e(n){var r,s,a,o,u,c,p,l,d,f,m;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,a=n.program,o=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Kn({suggestedParams:l,stakingAppID:s,program:a,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[o],accounts:[a.accountAddress],appArgs:[tt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=et,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Ze,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Te,exports.CONTRACT_VERSION=ut,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=nt,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Sn,exports.Swap=Gn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=tn,exports.V2AddLiquidityTxnIndices=be,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=Q,exports.calculateAccountMinimumRequiredBalance=wt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=Jt,exports.prepareCommitTransactions=function(t){return Hn.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Jn.apply(this,arguments)},exports.redeemExcessAsset=function(t){return qn.apply(this,arguments)},exports.sendAndWaitRawTransaction=Y,exports.sumUpTxnFees=H,exports.tinymanContract_v1_1=At,exports.tinymanContract_v2=ht,exports.tinymanJSSDKConfig=lt; +i=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",s=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function u(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,s=Object.create(a.prototype),o=new w(r||[]);return s._invoke=function(t,e,n){var r="suspendedStart";return function(a,s){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw s;return D()}for(n.method=a,n.arg=s;;){var o=n.delegate;if(o){var i=y(o,n);if(i){if(i===l)continue;return i}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var u=p(t,e,n);if("normal"===u.type){if(r=n.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:n.done}}"throw"===u.type&&(r="completed",n.method="throw",n.arg=u.arg)}}}(t,n,o),s}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};u(g,a,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,a)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,s,o,i){var u=p(t[a],t,s);if("throw"!==u.type){var c=u.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,i)}),(function(t){r("throw",t,o,i)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,i)}))}i(u.arg)}var a;this._invoke=function(t,n){function s(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(s,s):s()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function w(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,s=function e(){for(;++r=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function u(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){u(s,r,a,o,i,"next",t)}function i(t){u(s,r,a,o,i,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={type:"logicsig",logic:{bytecode:"BoAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgQBbNQA0ADEYEkQxGYEBEkSBAUM="},name:"pool_logicsig"},N={type:"app",global_state_schema:{num_uints:0,num_byte_slices:3},local_state_schema:{num_uints:12,num_byte_slices:2},name:"validator_app"},O="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function z(t,e){var n=Number(t);return Y({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function J(t,e){return Y({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function Y(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(H(e),2),s=a[0],o=a[1],i=v(H(Math.round(Number(K(s,o+r)))),2),u=i[0],c=i[1];return Number(K(u,c-r))}return Math.round(e)}function K(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function H(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(i().mark((function t(e,n){var r,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return u=t.sent,c=u.txId,t.next=13,q(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function tt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function et(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function nt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function rt(t){return(new TextEncoder).encode(t)}var at,st=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return rt("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),ot=(f(at={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(at,S.V2,{testnet:148607e3,mainnet:1002541853}),at);function it(t,e){var n=ot[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ut(){return(ut=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:it(a,s),note:st.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ct(){return(ct=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:it(a,s),note:st.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),lt={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},dt={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"};function ft(){return(ft=c(i().mark((function t(e){var n,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.assetID,s=e.initiatorAddr,t.prev=1,t.next=4,n.getTransactionParams().do();case 4:return o=t.sent,u=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:s,assetIndex:a,amount:0,suggestedParams:o}),t.abrupt("return",[{txn:u,signers:[s]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function mt(t,e){var n=Number(t.id),r=Number(e.id);return n>r?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function gt(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function xt(t){return 0===Number(t)}var It,At,ht,Tt,yt=new(function(e){m(r,pt);var n=y(r);function r(t,e){var a;return p(this,r),(a=n.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,a}return d(r,[{key:"generateLogicSigAccountForPool",value:function(e){if(e.asset1ID===e.asset2ID)throw new Error("Assets are the same");var n=it(e.network,S.V2),r=v(gt(e.asset1ID,e.asset2ID),2),a=r[0],s=r[1],o={bytes:Array.from(Buffer.from(this.poolLogicSigContractTemplate,"base64")),validatorAppId:Array.from(t.encodeUint64(n)),asset1ID:Array.from(t.encodeUint64(a)),asset2ID:Array.from(t.encodeUint64(s))},i=[].concat(_(o.bytes.slice(0,3)),_(o.validatorAppId.slice(0,8)),_(o.asset1ID.slice(0,8)),_(o.asset2ID.slice(0,8)),_(o.bytes.slice(27)));return new t.LogicSigAccount(new Uint8Array(i))}}]),r}())(N,E);exports.SwapType=void 0,(It=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",It.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(Tt||(Tt={}));var vt=(f(At={},exports.SwapType.FixedInput,1),f(At,exports.SwapType.FixedOutput,2),At),_t=rt("swap"),wt=(f(ht={},exports.SwapType.FixedInput,rt("fixed-input")),f(ht,exports.SwapType.FixedOutput,rt("fixed-output")),ht),bt={ASSET_OPT_IN:rt("asset_opt_in")};function Dt(){return(Dt=c(i().mark((function e(n){var a,s,o,u,c,p,l,d;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,u=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s,appArgs:[bt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),console.log(p.fee,"router app opt-in fee"),d=r.default.assignGroupID([p]),e.abrupt("return",[{txn:d[0],signers:[u]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function St(t){return Et.apply(this,arguments)}function Et(){return(Et=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.assetIn,o=n.assetOut,u=n.client,c=n.intermediaryAssetID,p=n.routerAppID,l=n.network,d=n.swapType,e.next=3,u.getTransactionParams().do();case 3:return f=e.sent,m=yt.generateLogicSigAccountForPool({asset1ID:s.id,asset2ID:c,network:l}),g=m.address(),x=yt.generateLogicSigAccountForPool({asset1ID:c,asset2ID:o.id,network:l}),I=x.address(),A=xt(s.id),h=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(p),amount:s.amount,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(p),amount:s.amount,assetIndex:s.id,suggestedParams:f}),T=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:p,appArgs:[_t,wt[d],r.default.encodeUint64(o.amount)],foreignApps:[it(l,S.V2)],foreignAssets:[s.id,c,o.id],accounts:[g,I],suggestedParams:f}),y=d===exports.SwapType.FixedInput?7:8,T.fee=t.ALGORAND_MIN_TX_FEE*(y+1),v=r.default.assignGroupID([h,T]),e.abrupt("return",[{txn:v[Tt.INPUT_TXN],signers:[a]},{txn:v[Tt.APP_CALL_TXN],signers:[a]}]);case 15:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nt(){return(Nt=c(i().mark((function e(n){var r,a,s,o,u,c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ot(t){return Pt.apply(this,arguments)}function Pt(){return(Pt=c(i().mark((function t(e){var n,r,a,s,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.next=4,fetch("https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});case 4:if(200===(u=t.sent).status){t.next=7;break}throw new Error("Failed to fetch swap route quotes");case 7:return t.next=9,u.json();case 9:return c=t.sent,p=c.route,t.abrupt("return",p);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function kt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return z(r.asset.decimals,Number(r.amount))/z(n.asset.decimals,Number(n.amount))}var Lt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Ft={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Rt=new(function(n){m(a,pt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=it(n.network,S.V1_1),a=v(gt(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(i().mark((function t(a,s){var u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:u=t.sent,a(o(o({},u),{},{minimum_required_balance:Vt(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Ct(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?R({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Vt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Xt,jt,Qt=rt("e");function Ut(t){return qt.apply(this,arguments)}function qt(){return(qt=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,u=a.accountAddr,n.next=3,s.accountInformation(u).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=R({stateArray:A}),T=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Gt(){return(Gt=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,u=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==u})),d=[],l&&l["key-value"])for(f=R({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mJt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Jt,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Zt(){return Zt=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d=arguments;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,Mt(n,r);case 6:return o=t.sent,u=Ct(o,it(a,S.V1_1)),c=null,u&&(l=o["created-assets"][0],p=l.index,c={asset1ID:u[Wt[S.V1_1].asset1],asset2ID:u[Wt[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Zt.apply(this,arguments)}var $t=Object.freeze({__proto__:null,getPoolInfo:Yt,getPoolReserves:function(t,e){return Ht.apply(this,arguments)},getPoolAssets:function(t){return Zt.apply(this,arguments)}});function te(t){return ee.apply(this,arguments)}function ee(){return(ee=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Bt(S.V2),u=o.generateLogicSigAccountForPool(e),c=it(r,S.V2),p=u.address(),l=gt(a,s),t.next=8,Mt(n,p);case 8:return d=t.sent,f=Ct(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[Wt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Wt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Wt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Wt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Wt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Wt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Wt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Wt.v2.totalFeeShare]),m.poolTokenID=Number(f[Wt.v2.poolTokenID]),m.asset1ID=Number(f[Wt.v2.asset1]),m.asset2ID=Number(f[Wt.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ne(){return(ne=c(i().mark((function t(e,n){var r,a,s;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Mt(e,n.account.address());case 2:return r=t.sent,a=Ct(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[Wt.v2.asset1Reserves]),s.asset2=BigInt(a[Wt.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[Wt.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function re(){return(re=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,Mt(n,r);case 3:return s=t.sent,o=Ct(s,it(a,S.V2)),u=null,o&&(u={asset1ID:o[Wt[S.V2].asset1],asset2ID:o[Wt[S.V2].asset2],poolTokenID:o[Wt[S.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ae=Object.freeze({__proto__:null,getPoolInfo:te,getPoolReserves:function(t,e){return ne.apply(this,arguments)},getPoolAssets:function(t){return re.apply(this,arguments)}});function se(t){return Boolean(t&&!(t.asset1+t.asset2))}var oe,ie,ue=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=se(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=z(t.asset1,e.asset1)/z(t.asset2,e.asset2)),r},isPoolEmpty:se,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Yt(t),te(t)])}}),ce=o((f(oe={},S.V1_1,o(o({},$t),ue)),f(oe,S.V2,o(o({},ae),ue)),oe),ue);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(ie||(ie={}));var pe=96e4,le=859e3;function de(){return(de=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,O;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,u=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=mt(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=xt(x),h=it(a,S.V1_1),T=Rt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:st.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[rt("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:dt.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:fe(A),suggestedParams:c}),(E=[])[ie.FUNDING_TXN]=D,E[ie.VALIDATOR_APP_CALL]=_,E[ie.POOL_TOKEN_CREATE]=w,E[ie.ASSET1_OPT_IN]=b,A||(E[ie.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),O=[{txn:N[ie.FUNDING_TXN],signers:[u]},{txn:N[ie.VALIDATOR_APP_CALL],signers:[y]},{txn:N[ie.POOL_TOKEN_CREATE],signers:[y]},{txn:N[ie.ASSET1_OPT_IN],signers:[y]}],N[ie.ASSET2_OPT_IN]&&O.push({txn:N[ie.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(t){return t?pe:le}function me(){return(me=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=gt(o,u),f=v(d,2),m=f[0],g=f[1],x=Rt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===ie.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ge(t){return xe.apply(this,arguments)}function xe(){return(xe=c(i().mark((function t(e){var n,r,a,s,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,q(n,a[ie.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Ie(){return(Ie=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,ge({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",ce.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ae,he={generateTxns:function(t){return de.apply(this,arguments)},signTxns:function(t){return me.apply(this,arguments)},execute:function(t){return Ie.apply(this,arguments)},getBootstrapFundingTxnAmount:fe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ae||(Ae={}));var Te=5,ye=6;function ve(t,e){return _e.apply(this,arguments)}function _e(){return(_e=c(i().mark((function e(n,r){var a,s,o;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function we(t,e){return be.apply(this,arguments)}function be(){return(be=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function De(){return(De=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,u=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=it(s,S.V2),d=t.getApplicationAddress(l),f=mt(o,u),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,ce.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(u.unit_name," already exists"));case 12:return I=yt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=xt(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[rt("bootstrap")],note:st.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ee(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Se(h),suggestedParams:p}),(_=[])[Ae.FUNDING_TXN]=y,_[Ae.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Ae.FUNDING_TXN]={txn:w[Ae.FUNDING_TXN],signers:[c]},b[Ae.VALIDATOR_APP_CALL]={txn:w[Ae.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Se(t){return function(t,e){var n=Bt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ee(t)+1e5}function Ee(e){return((e?Te:ye)+1)*t.ALGORAND_MIN_TX_FEE}function Ne(){return(Ne=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=gt(o,u),f=v(d,2),m=f[0],g=f[1],x=yt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ae.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Oe(){return(Oe=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,ve(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Wt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",ce.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Pe,ke={generateTxns:function(t){return De.apply(this,arguments)},signTxns:function(t){return Ne.apply(this,arguments)},execute:function(t){return Oe.apply(this,arguments)},getBootstrapFundingTxnAmount:Se,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Se(e)}};var Le,Fe=(f(Pe={},S.V1_1,he),f(Pe,S.V2,ke),f(Pe,"generateTxns",(function(t){return t.contractVersion===S.V1_1?he.generateTxns(t):ke.generateTxns(t)})),f(Pe,"signTxns",(function(t){return t.contractVersion===S.V1_1?he.signTxns(t):ke.signTxns(t)})),f(Pe,"execute",(function(t){return t.contractVersion===S.V1_1?he.execute(t):ke.execute(t)})),f(Pe,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?he.getBootstrapFundingTxnAmount(n):ke.getBootstrapFundingTxnAmount(n)})),Pe);exports.V1_1AddLiquidityTxnIndices=void 0,(Le=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Le.FEE_TXN=0]="FEE_TXN",Le[Le.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Le[Le.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Le[Le.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Le[Le.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Re,Be,Me,Ce,Ve=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Ce=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Ce.FLEXIBLE="flexible",Ce.INITIAL="initial";var Xe,je=(f(Re={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Re,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Re,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Re),Qe=(f(Be={},exports.V2AddLiquidityType.INITIAL,1),f(Be,exports.V2AddLiquidityType.SINGLE,2),f(Be,exports.V2AddLiquidityType.FLEXIBLE,2),Be),Ue=(f(Me={},exports.V2AddLiquidityType.INITIAL,3),f(Me,exports.V2AddLiquidityType.FLEXIBLE,3),f(Me,exports.V2AddLiquidityType.SINGLE,2),Me);function qe(t){var e=t.assetIn,n=t.assetOut;return z(n.decimals,Number(n.amount))/z(e.decimals,Number(e.amount))}function Ge(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=qe({assetIn:r,assetOut:a}),o=z(a.decimals,Number(n))/z(r.decimals,Number(e));return Y({decimalPlaces:5},Math.abs(s/o-1))}function We(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,i=t.asset1Amount,u=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(i),d=s.asset2+BigInt(u),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(i)-x,h=BigInt(u)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=ze(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=ze(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:Ge({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function ze(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Je(e){return(Qe[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ye=rt("add_liquidity"),Ke=(f(Xe={},S.V1_1,[rt("mint")]),f(Xe,S.V2,{INITIAL_LIQUIDITY:[rt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ye,rt("single")],FLEXIBLE_MODE:[Ye,rt("flexible")]}),Xe);function He(){return(He=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,u=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=W("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:it(s,S.V1_1),appArgs:Ke.v1_1,accounts:[d],note:st.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[u.id,p.id]:[u.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ze(){return(Ze=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $e(){return($e=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,u=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Ut({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,Z(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=tt(a),x=et(a),t.next=17,Ut({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var tn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=o1&&BigInt(I[I.length-1].quote.amount_out.amount)>x.assetOutAmount)){t.next=14;break}return t.abrupt("return",{route:I,type:exports.SwapQuoteType.Router});case 14:return t.abrupt("return",{quoteWithPool:{pool:n,quote:x},type:exports.SwapQuoteType.Direct});case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nn(t){return On.apply(this,arguments)}function On(){return(On=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetOut,a=e.decimals,s=e.isSwapRouterEnabled,o=BigInt(r.amount),u=n.totalFeeShare,r.id===n.asset1ID?(c=n.asset2ID,p=n.asset2Reserves,l=n.asset1Reserves):(c=n.asset1ID,p=n.asset1Reserves,l=n.asset2Reserves),d=kn({inputSupply:p,outputSupply:l,swapOutputAmount:o,totalFeeShare:u,decimals:a}),f=d.swapInputAmount,m=d.totalFeeAmount,g=d.priceImpact,x={assetInID:c,assetInAmount:f,assetOutID:r.id,assetOutAmount:o,swapFee:Number(m),rate:z(a.assetOut,Number(o))/z(a.assetIn,Number(f)),priceImpact:g},!s){t.next=12;break}return t.next=9,Ot({amount:r.amount,assetInID:c,assetOutID:r.id,swapType:exports.SwapType.FixedInput});case 9:if(!((I=t.sent).length>1&&BigInt(I[I.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Rn(){return(Rn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,u=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s.validatorAppID,note:st.getAppCallTxnNoteWithClientName(S.V2),appArgs:[In,r.default.encodeUint64(W("negative",l,c)),r.default.encodeUint64(W("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[gn.ASSET_TRANSFER_TXN]=g,I[gn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[gn.ASSET_TRANSFER_TXN],signers:[u]},{txn:A[gn.APP_CALL_TXN],signers:[u]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,u=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=W("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:u,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:st.getAppCallTxnNoteWithClientName(S.V2),appArgs:[In,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[gn.ASSET_TRANSFER_TXN]=T,v[gn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[gn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[gn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,Z(r,[s]);case 3:return o=t.sent,u=v(o,1),c=u[0].txnID,t.next=8,we(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Cn,Vn,Xn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Fn(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=Fn(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ln.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Ln.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Rn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Bn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Mn.apply(this,arguments)}},jn=(f(Cn={},S.V1_1,vn),f(Cn,S.V2,Xn),Cn),Qn=3n,Un=1000n;function qn(){return(qn=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,u=v(o,2),c=u[0],p=u[1],l=a.map((function(t,e){return e===Vn.FEE_TXN_INDEX?c:e===Vn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Gn(){return(Gn=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.swapType,u=e.assetIn,c=e.assetOut,p=e.slippage,l=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return d=t.sent,f=s.account.address(),m=[rt("swap"),o===exports.SwapType.FixedInput?rt("fi"):rt("fo")],g=r.default.makeApplicationNoOpTxnFromObject({from:f,appIndex:s.validatorAppID,appArgs:m,accounts:[l],note:st.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:d}),x=o===exports.SwapType.FixedOutput?W("positive",p,u.amount):u.amount,I=0===u.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:x,suggestedParams:d}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:u.id,amount:x,suggestedParams:d}),A=o===exports.SwapType.FixedInput?W("negative",p,c.amount):c.amount,h=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:l,amount:A,suggestedParams:d}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:l,assetIndex:c.id,amount:A,suggestedParams:d}),T=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:g.fee+h.fee,note:a,suggestedParams:d}),y=r.default.assignGroupID([T,g,I,h]),t.abrupt("return",[{txn:y[0],signers:[l]},{txn:y[1],signers:[f]},{txn:y[2],signers:[l]},{txn:y[3],signers:[f]}]);case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Wn(t){var e,n,r,a=t.pool,s=t.reserves,i=t.assetIn,u=t.decimals,c=BigInt(i.amount);i.id===a.asset1ID?(e=a.asset2ID,n=s.asset1,r=s.asset2):(e=a.asset1ID,n=s.asset2,r=s.asset1);var p=c*Qn/Un,l=r-n*r/(n+(c-p));if(l>r)throw new Error("Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:u.assetIn},assetOut:{amount:l,decimals:u.assetOut}};return{round:s.round,assetInID:i.id,assetInAmount:c,assetOutID:e,assetOutAmount:l,swapFee:Number(p),rate:qe(d),priceImpact:Ge(o({inputSupply:n,outputSupply:r},d))}}function zn(t){return Jn.apply(this,arguments)}function Jn(){return(Jn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,Ut({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Z(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ut({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yn(t){var e,n,r,a=t.pool,s=t.reserves,o=t.assetOut,i=t.decimals,u=BigInt(o.amount);if(o.id===a.asset1ID?(e=a.asset2ID,n=s.asset2,r=s.asset1):(e=a.asset1ID,n=s.asset1,r=s.asset2),u>r)throw new Error("Output amount exceeds available liquidity.");var c=n*r/(r-u)-n,p=c*Un/(Un-Qn),l=p-c,d=z(i.assetOut,Number(u))/z(i.assetIn,Number(p)),f=z(i.assetOut,Number(r))/z(i.assetIn,Number(n)),m=Y({decimalPlaces:5},Math.abs(d/f-1));return{round:s.round,assetInID:e,assetInAmount:p,assetOutID:o.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m}}function Kn(t){return Hn.apply(this,arguments)}function Hn(){return(Hn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,Ut({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Z(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ut({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Vn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Vn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Vn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Vn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,zn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Kn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:et(s),fees:tt(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Vn||(Vn={}));var $n,tr={getQuote:function(t,e,n,r,a){if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,asset:r},"Trying to swap on a non-existent pool");return t===exports.SwapType.FixedInput?Wn({pool:e,reserves:n,assetIn:r,decimals:a}):Yn({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Wn,getFixedOutputSwapQuote:Yn,generateTxns:function(t){return Gn.apply(this,arguments)},signTxns:function(t){return qn.apply(this,arguments)},execute:function(t){return Zn.apply(this,arguments)},executeFixedOutputSwap:Kn},er=4*t.ALGORAND_MIN_TX_FEE;function nr(){return nr=c(i().mark((function t(e){var n,r,a,s,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,t.next=3,Promise.all(s.map(function(){var t=c(i().mark((function t(s){var o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(u={pool:s.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:s.reserves,isSwapRouterEnabled:e.isSwapRouterEnabled},s.info.contractVersion!==S.V1_1){t.next=5;break}o={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:tr.getFixedInputSwapQuote(u),pool:s.info}},t.next=8;break;case 5:return t.next=7,Ln.getFixedInputSwapQuote(u);case 7:o=t.sent;case 8:return t.abrupt("return",o);case 9:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}()));case 3:return o=t.sent,console.log("quotes",o),t.abrupt("return",sr(o));case 6:case"end":return t.stop()}}),t)}))),nr.apply(this,arguments)}function rr(){return rr=c(i().mark((function t(e){var n,r,a,s,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,t.next=3,Promise.all(s.map(function(){var t=c(i().mark((function t(e){var s,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(o={pool:e.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:e.reserves},e.info.contractVersion!==S.V1_1){t.next=5;break}s={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:tr.getFixedOutputSwapQuote(o),pool:e.info}},t.next=8;break;case 5:return t.next=7,Ln.getFixedOutputSwapQuote(o);case 7:s=t.sent;case 8:return t.abrupt("return",s);case 9:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}()));case 3:return o=t.sent,t.abrupt("return",sr(o));case 5:case"end":return t.stop()}}),t)}))),rr.apply(this,arguments)}function ar(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:kt(t.route)}function sr(t){var e,n=t[0],r=ar(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=ar(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}var or=(f($n={},S.V1_1,tr),f($n,S.V2,Ln),f($n,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return se(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return nr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return rr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f($n,"generateTxns",(function(t){return t.isUsingSwapRouter||t.pool.contractVersion!==S.V1_1?Ln.generateTxns(t):tr.generateTxns(t)})),f($n,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&function(t){if(t.type===exports.SwapQuoteType.Direct)return t.quoteWithPool.pool.contractVersion;return S.V2}(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return tr.signTxns(o({pool:e},t))}return Ln.signTxns(t)})),f($n,"execute",(function(t){return t.contractVersion===S.V1_1?tr.execute(t):Ln.execute(t)})),$n);function ir(){return(ir=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,ur({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,Z(n,[o]);case 7:return u=t.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:tt(a),confirmedRound:d,txnID:l,groupID:et(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function ur(t){return cr.apply(this,arguments)}function cr(){return(cr=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,u=v(o,1),c=u[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pr(){return pr=c(i().mark((function t(e){var n,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:tt(e),groupID:et(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return u=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(i().mark((function a(s,o){var c,p,l,d,f,m;return i().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?u[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,Z(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),pr.apply(this,arguments)}function lr(){return(lr=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,u=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[rt("redeem")],note:st.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(u),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(u),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[rt("commit"),u],note:B([rt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function fr(){return(fr=c(i().mark((function e(n){var r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=dr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[rt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=lt,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=mn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Fe,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=dt,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=jn,exports.Swap=or,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=xn,exports.V2AddLiquidityTxnIndices=je,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=W,exports.calculateAccountMinimumRequiredBalance=Vt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=ce,exports.prepareCommitTransactions=function(t){return fr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return pr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return ir.apply(this,arguments)},exports.sendAndWaitRawTransaction=Z,exports.sumUpTxnFees=tt,exports.tinymanContract_v1_1=Rt,exports.tinymanContract_v2=yt,exports.tinymanJSSDKConfig=st; diff --git a/dist/swap/index.d.ts b/dist/swap/index.d.ts index 08373cd0..ec49c902 100644 --- a/dist/swap/index.d.ts +++ b/dist/swap/index.d.ts @@ -4,7 +4,7 @@ export declare const Swap: { getQuote: (type: import("./constants").SwapType, pool: import("..").V1PoolInfo, reserves: import("..").PoolReserves, asset: import("../util/asset/assetModels").AssetWithIdAndAmount, decimals: { assetIn: number; assetOut: number; - }) => import("./types").SwapQuote; + }) => import("./types").DirectSwapQuote; getFixedInputSwapQuote: ({ pool, reserves, assetIn, decimals }: { pool: import("..").V1PoolInfo; reserves: import("..").PoolReserves; @@ -13,7 +13,7 @@ export declare const Swap: { assetIn: number; assetOut: number; }; - }) => import("./types").SwapQuote; + }) => import("./types").DirectSwapQuote; getFixedOutputSwapQuote: ({ pool, reserves, assetOut, decimals }: { pool: import("..").V1PoolInfo; reserves: import("..").PoolReserves; @@ -22,16 +22,8 @@ export declare const Swap: { assetIn: number; assetOut: number; }; - }) => import("./types").SwapQuote; - generateTxns: ({ client, pool, swapType, assetIn, assetOut, slippage, initiatorAddr }: { - client: import("algosdk").Algodv2; - pool: import("..").V1PoolInfo; - swapType: import("./constants").SwapType; - assetIn: import("../util/asset/assetModels").AssetWithIdAndAmount; - assetOut: import("../util/asset/assetModels").AssetWithIdAndAmount; - slippage: number; - initiatorAddr: string; - }) => Promise; + }) => import("./types").DirectSwapQuote; + generateTxns: ({ client, pool, swapType, assetIn, assetOut, slippage, initiatorAddr }: import("./types").GenerateSwapTxnsWithoutRouterParams) => Promise; signTxns: ({ pool, txGroup, initiatorSigner }: { pool: import("..").V1PoolInfo; txGroup: import("..").SignerTransaction[]; @@ -55,43 +47,17 @@ export declare const Swap: { }) => Promise>; }; v2: { - getQuote: (type: import("./constants").SwapType, pool: import("..").V2PoolInfo, asset: import("../util/asset/assetModels").AssetWithIdAndAmount, decimals: { - assetIn: number; - assetOut: number; - }) => import("./types").SwapQuote; - getFixedInputSwapQuote: ({ pool, assetIn, decimals }: { - pool: import("..").V2PoolInfo; - assetIn: import("../util/asset/assetModels").AssetWithIdAndAmount; - decimals: { - assetIn: number; - assetOut: number; - }; - }) => import("./types").SwapQuote; - getFixedOutputSwapQuote: ({ pool, assetOut, decimals }: { - pool: import("..").V2PoolInfo; - assetOut: import("../util/asset/assetModels").AssetWithIdAndAmount; - decimals: { - assetIn: number; - assetOut: number; - }; - }) => import("./types").SwapQuote; - generateTxns: ({ client, pool, swapType, assetIn, assetOut, initiatorAddr, slippage }: { - client: import("algosdk").Algodv2; - pool: import("..").V2PoolInfo; - swapType: import("./constants").SwapType; - assetIn: import("../util/asset/assetModels").AssetWithIdAndAmount; - assetOut: import("../util/asset/assetModels").AssetWithIdAndAmount; - initiatorAddr: string; - slippage: number; - }) => Promise; + getQuote: (params: import("./types").GetSwapQuoteWithContractVersionParams) => Promise; + getFixedInputSwapQuote: ({ pool, assetIn, decimals, isSwapRouterEnabled }: import("./types").GetFixedInputSwapQuoteByContractVersionParams) => Promise; + getFixedOutputSwapQuote: ({ pool, assetOut, decimals, isSwapRouterEnabled }: import("./types").GetFixedOutputSwapQuoteByContractVersionParams) => Promise; + generateTxns: (params: import("./types").GenerateSwapTxnsParams) => Promise; signTxns: ({ txGroup, initiatorSigner }: { txGroup: import("..").SignerTransaction[]; initiatorSigner: import("..").InitiatorSigner; }) => Promise; - execute: ({ client, pool, txGroup, signedTxns, network, assetIn }: { + execute: ({ client, quote, txGroup, signedTxns, assetIn }: { client: import("algosdk").Algodv2; - pool: import("..").V2PoolInfo; - network: import("..").SupportedNetwork; + quote: import("./types").SwapQuote; txGroup: import("..").SignerTransaction[]; signedTxns: Uint8Array[]; assetIn: import("../util/asset/assetModels").AssetWithIdAndAmount; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index e215f49e..532ee85f 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -1,7 +1,9 @@ -import { AssetWithIdAndAmount } from "../util/asset/assetModels"; +import { Algodv2 } from "algosdk"; +import { AssetWithIdAndAmount, TinymanAnalyticsApiAsset } from "../util/asset/assetModels"; +import { SupportedNetwork } from "../util/commonTypes"; import { PoolReserves, V1PoolInfo, V2PoolInfo } from "../util/pool/poolTypes"; -/** An object containing information about a swap quote. */ -export interface SwapQuote { +import { SwapType } from "./constants"; +export interface DirectSwapQuote { /** The ID of the input asset in this quote. */ assetInID: number; /** The quantity of the input asset in this quote. */ @@ -18,6 +20,7 @@ export interface SwapQuote { priceImpact: number; /** The round that this quote is based on. */ round?: number; + price?: number; } /** An object containing information about a successfully executed swap. */ export interface V1SwapExecution { @@ -56,14 +59,124 @@ export interface V2SwapExecution { /** Can be `undefined` if the execution was successful, but there was an issue while * extracting the output asset data fron the transaction response */ assetOut: AssetWithIdAndAmount | undefined; - pool: V2PoolInfo; + quote: SwapQuote; txnID: string; round: number; } -export interface SwapQuoteWithPool { - quote: SwapQuote; - pool: { +export declare type GetSwapQuoteParams = { + assetIn: Pick; + assetOut: Pick; + pools: { info: V1PoolInfo | V2PoolInfo; reserves: PoolReserves; + }[]; + amount: number | bigint; + type: SwapType; + network: SupportedNetwork; + isSwapRouterEnabled?: boolean; +}; +export interface SwapQuoteWithPool { + quote: DirectSwapQuote; + pool: V1PoolInfo | V2PoolInfo; +} +export declare type SwapQuote = { + quoteWithPool: SwapQuoteWithPool; + type: SwapQuoteType.Direct; +} | { + route: SwapRoute; + type: SwapQuoteType.Router; +}; +export declare type GetSwapQuoteBySwapTypeParams = Omit; +export declare type GetSwapQuoteWithContractVersionParams = { + type: SwapType; + pool: V2PoolInfo; + asset: AssetWithIdAndAmount; + decimals: { + assetIn: number; + assetOut: number; }; + isSwapRouterEnabled?: boolean; +}; +export declare type GetFixedInputSwapQuoteByContractVersionParams = Omit & { + assetIn: AssetWithIdAndAmount; +}; +export declare type GetFixedOutputSwapQuoteByContractVersionParams = Omit & { + assetOut: AssetWithIdAndAmount; +}; +export interface FetchSwapRouteQuotesPayload { + asset_in_id: string; + asset_out_id: string; + amount: string; + swap_type: SwapType; +} +export interface SwapRouteAsset { + id: string; + name: string; + unit_name: string; + decimals: number; +} +export interface SwapRoutePool { + address: string; + asset_1: SwapRouteAsset; + asset_2: SwapRouteAsset; + version: "2.0"; } +export declare type SwapRoute = { + quote: SwapRouterQuote; + pool: SwapRoutePool; +}[]; +export declare enum SwapQuoteType { + Direct = "direct", + Router = "router" +} +export interface GenerateSwapTxnsWithoutRouterParams { + client: Algodv2; + pool: V1PoolInfo | V2PoolInfo; + swapType: SwapType; + assetIn: AssetWithIdAndAmount; + assetOut: AssetWithIdAndAmount; + slippage: number; + initiatorAddr: string; +} +export interface GenerateSwapRouterTxnsParams { + client: Algodv2; + routerAppID: number; + initiatorAddr: string; + assetIn: AssetWithIdAndAmount; + assetOut: AssetWithIdAndAmount; + intermediaryAssetID: number; + swapType: SwapType; + network: SupportedNetwork; +} +export declare type GenerateSwapTxnsParams = (GenerateSwapTxnsWithoutRouterParams & { + isUsingSwapRouter: false; +}) | (GenerateSwapRouterTxnsParams & { + isUsingSwapRouter: true; +}); +export declare type GenerateV1_1SwapTxnsParams = Omit & { + quote: DirectSwapQuote; +}; +export interface SwapRouterQuote { + swap_type: SwapType; + amount_in: { + asset: SwapRouteAsset; + amount: string; + }; + amount_out: { + asset: SwapRouteAsset; + amount: string; + }; + swap_fees: { + amount: string; + asset_id: string; + }; + price: number; + price_impact: number; +} +export declare type FetchSwapRouteQuotesResponse = FetchSwapRouteQuotesPayload & { + route: SwapRoute; + status: { + round_number: string; + round_datetime: string; + }; +}; diff --git a/dist/swap/utils.d.ts b/dist/swap/utils.d.ts index 839b2c91..dbf9fb23 100644 --- a/dist/swap/utils.d.ts +++ b/dist/swap/utils.d.ts @@ -1,69 +1,35 @@ import { Algodv2 } from "algosdk"; import { CONTRACT_VERSION } from "../contract/constants"; -import { AssetWithIdAndAmount, TinymanAnalyticsApiAsset } from "../util/asset/assetModels"; +import { AssetWithIdAndAmount } from "../util/asset/assetModels"; import { InitiatorSigner, SignerTransaction, SupportedNetwork } from "../util/commonTypes"; -import { PoolReserves, V1PoolInfo, V2PoolInfo } from "../util/pool/poolTypes"; -import { SwapQuoteWithPool } from "./types"; +import { V1PoolInfo } from "../util/pool/poolTypes"; +import { GetSwapQuoteBySwapTypeParams, GenerateSwapTxnsParams, GetSwapQuoteParams, SwapQuote } from "./types"; import { SwapType } from "./constants"; +import { ContractVersionValue } from "../contract/types"; /** - * Gets quotes for swap from each pool passed as an argument, - * and returns the best quote (with the highest rate). + * Gets the best quote for swap from the pools and swap router and returns the best option. */ -export declare function getQuote(params: { - type: SwapType; - pools: { - info: V1PoolInfo | V2PoolInfo; - reserves: PoolReserves; - }[]; - assetIn: Pick; - assetOut: Pick; - amount: number | bigint; -}): SwapQuoteWithPool; +export declare function getQuote(params: GetSwapQuoteParams): Promise; /** - * Gets quotes for fixed input swap from each pool passed as an argument, + * Gets quotes for fixed input swap the pools and swap router, * and returns the best quote (with the highest rate). */ -export declare function getFixedInputSwapQuote({ pools, assetIn, assetOut, amount }: { - pools: { - info: V1PoolInfo | V2PoolInfo; - reserves: PoolReserves; - }[]; - assetIn: Pick; - assetOut: Pick; - amount: number | bigint; -}): SwapQuoteWithPool; +export declare function getFixedInputSwapQuote(params: GetSwapQuoteBySwapTypeParams): Promise; /** - * Gets quotes for fixed output swap from each pool passed as an argument, + * Gets quotes for fixed output swap from the pools and swap router, * and returns the best quote (with the highest rate). */ -export declare function getFixedOutputSwapQuote({ pools, assetIn, assetOut, amount }: { - pools: { - info: V1PoolInfo | V2PoolInfo; - reserves: PoolReserves; - }[]; - assetIn: Pick; - assetOut: Pick; - amount: number | bigint; -}): SwapQuoteWithPool; -export declare function generateTxns(params: { - client: Algodv2; - pool: V1PoolInfo | V2PoolInfo; - poolAddress: string; - swapType: SwapType; - assetIn: AssetWithIdAndAmount; - assetOut: AssetWithIdAndAmount; - slippage: number; - initiatorAddr: string; -}): Promise; +export declare function getFixedOutputSwapQuote(params: GetSwapQuoteBySwapTypeParams): Promise; +export declare function generateTxns(params: GenerateSwapTxnsParams): Promise; export declare function signTxns(params: { - pool: V1PoolInfo; + quote: SwapQuote; txGroup: SignerTransaction[]; initiatorSigner: InitiatorSigner; + network: SupportedNetwork; }): Promise; interface ExecuteCommonParams { swapType: SwapType; client: Algodv2; - pool: V2PoolInfo; network: SupportedNetwork; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; @@ -72,8 +38,10 @@ interface ExecuteCommonParams { export declare function execute(params: ({ contractVersion: typeof CONTRACT_VERSION.V1_1; initiatorAddr: string; + pool: V1PoolInfo; } | { contractVersion: typeof CONTRACT_VERSION.V2; + quote: SwapQuote; }) & ExecuteCommonParams): Promise | Promise; /** * @returns the total fee that will be paid by the user @@ -85,4 +53,5 @@ export declare function getSwapTotalFee(params: { version: typeof CONTRACT_VERSION.V2; type: SwapType; }): number; +export declare function getContractVersionFromSwapQuote(quote: SwapQuote): ContractVersionValue; export {}; diff --git a/dist/swap/v1_1/index.d.ts b/dist/swap/v1_1/index.d.ts index 8da014a9..e1d26409 100644 --- a/dist/swap/v1_1/index.d.ts +++ b/dist/swap/v1_1/index.d.ts @@ -1,7 +1,7 @@ import { Algodv2 } from "algosdk"; import { InitiatorSigner, SignerTransaction } from "../../util/commonTypes"; import { PoolReserves, V1PoolInfo } from "../../util/pool/poolTypes"; -import { SwapQuote, V1SwapExecution } from "../types"; +import { DirectSwapQuote, GenerateSwapTxnsWithoutRouterParams, V1SwapExecution } from "../types"; import { SwapType } from "../constants"; import { AssetWithIdAndAmount } from "../../util/asset/assetModels"; declare function signTxns({ pool, txGroup, initiatorSigner }: { @@ -9,15 +9,7 @@ declare function signTxns({ pool, txGroup, initiatorSigner }: { txGroup: SignerTransaction[]; initiatorSigner: InitiatorSigner; }): Promise; -declare function generateTxns({ client, pool, swapType, assetIn, assetOut, slippage, initiatorAddr }: { - client: Algodv2; - pool: V1PoolInfo; - swapType: SwapType; - assetIn: AssetWithIdAndAmount; - assetOut: AssetWithIdAndAmount; - slippage: number; - initiatorAddr: string; -}): Promise; +declare function generateTxns({ client, pool, swapType, assetIn, assetOut, slippage, initiatorAddr }: GenerateSwapTxnsWithoutRouterParams): Promise; /** * * @param type - Type of the swap @@ -32,7 +24,7 @@ declare function generateTxns({ client, pool, swapType, assetIn, assetOut, slipp declare function getQuote(type: SwapType, pool: V1PoolInfo, reserves: PoolReserves, asset: AssetWithIdAndAmount, decimals: { assetIn: number; assetOut: number; -}): SwapQuote; +}): DirectSwapQuote; /** * Get a quote for a fixed input swap This does not execute any transactions. * @@ -50,7 +42,7 @@ declare function getFixedInputSwapQuote({ pool, reserves, assetIn, decimals }: { assetIn: number; assetOut: number; }; -}): SwapQuote; +}): DirectSwapQuote; /** * Get a quote for a fixed output swap This does not execute any transactions. * @@ -68,7 +60,7 @@ declare function getFixedOutputSwapQuote({ pool, reserves, assetOut, decimals }: assetIn: number; assetOut: number; }; -}): SwapQuote; +}): DirectSwapQuote; /** * Execute a fixed output swap with the desired quantities. * diff --git a/dist/swap/v2/constants.d.ts b/dist/swap/v2/constants.d.ts index 1adbd66a..5acf62b1 100644 --- a/dist/swap/v2/constants.d.ts +++ b/dist/swap/v2/constants.d.ts @@ -18,3 +18,6 @@ export declare const V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED: { readonly "fixed-input": Uint8Array; readonly "fixed-output": Uint8Array; }; +export declare const V2_SWAP_ROUTER_APP_ARGS_ENCODED: { + ASSET_OPT_IN: Uint8Array; +}; diff --git a/dist/swap/v2/index.d.ts b/dist/swap/v2/index.d.ts index e21b3207..62e5c352 100644 --- a/dist/swap/v2/index.d.ts +++ b/dist/swap/v2/index.d.ts @@ -1,18 +1,8 @@ import { Algodv2 } from "algosdk"; -import { InitiatorSigner, SignerTransaction, SupportedNetwork } from "../../util/commonTypes"; -import { V2PoolInfo } from "../../util/pool/poolTypes"; -import { SwapQuote, V2SwapExecution } from "../types"; -import { SwapType } from "../constants"; +import { InitiatorSigner, SignerTransaction } from "../../util/commonTypes"; +import { GenerateSwapTxnsParams, GetFixedInputSwapQuoteByContractVersionParams, GetFixedOutputSwapQuoteByContractVersionParams, GetSwapQuoteWithContractVersionParams, SwapQuote, V2SwapExecution } from "../types"; import { AssetWithIdAndAmount } from "../../util/asset/assetModels"; -declare function generateTxns({ client, pool, swapType, assetIn, assetOut, initiatorAddr, slippage }: { - client: Algodv2; - pool: V2PoolInfo; - swapType: SwapType; - assetIn: AssetWithIdAndAmount; - assetOut: AssetWithIdAndAmount; - initiatorAddr: string; - slippage: number; -}): Promise; +declare function generateTxns(params: GenerateSwapTxnsParams): Promise; declare function signTxns({ txGroup, initiatorSigner }: { txGroup: SignerTransaction[]; initiatorSigner: InitiatorSigner; @@ -20,10 +10,9 @@ declare function signTxns({ txGroup, initiatorSigner }: { /** * Executes a swap with the desired quantities. */ -declare function execute({ client, pool, txGroup, signedTxns, network, assetIn }: { +declare function execute({ client, quote, txGroup, signedTxns, assetIn }: { client: Algodv2; - pool: V2PoolInfo; - network: SupportedNetwork; + quote: SwapQuote; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; assetIn: AssetWithIdAndAmount; @@ -37,32 +26,15 @@ declare function execute({ client, pool, txGroup, signedTxns, network, assetIn } * @param decimals.assetOut - Decimals quantity for the output asset * @returns A promise for the Swap quote */ -declare function getQuote(type: SwapType, pool: V2PoolInfo, asset: AssetWithIdAndAmount, decimals: { - assetIn: number; - assetOut: number; -}): SwapQuote; +declare function getQuote(params: GetSwapQuoteWithContractVersionParams): Promise; /** * @returns A quote for a fixed input swap. Does NOT execute any transactions. */ -declare function getFixedInputSwapQuote({ pool, assetIn, decimals }: { - pool: V2PoolInfo; - assetIn: AssetWithIdAndAmount; - decimals: { - assetIn: number; - assetOut: number; - }; -}): SwapQuote; +declare function getFixedInputSwapQuote({ pool, assetIn, decimals, isSwapRouterEnabled }: GetFixedInputSwapQuoteByContractVersionParams): Promise; /** * @returns A quote for a fixed output swap. Does NOT execute any transactions. */ -declare function getFixedOutputSwapQuote({ pool, assetOut, decimals }: { - pool: V2PoolInfo; - assetOut: AssetWithIdAndAmount; - decimals: { - assetIn: number; - assetOut: number; - }; -}): SwapQuote; +declare function getFixedOutputSwapQuote({ pool, assetOut, decimals, isSwapRouterEnabled }: GetFixedOutputSwapQuoteByContractVersionParams): Promise; declare function calculateFixedInputSwap({ inputSupply, outputSupply, swapInputAmount, totalFeeShare, decimals }: { inputSupply: bigint; outputSupply: bigint; diff --git a/dist/swap/v2/router/index.d.ts b/dist/swap/v2/router/index.d.ts new file mode 100644 index 00000000..05b32ead --- /dev/null +++ b/dist/swap/v2/router/index.d.ts @@ -0,0 +1,2 @@ +export * from "./swap-router"; +export * from "./util"; diff --git a/dist/swap/v2/router/swap-router.d.ts b/dist/swap/v2/router/swap-router.d.ts new file mode 100644 index 00000000..dbde43b8 --- /dev/null +++ b/dist/swap/v2/router/swap-router.d.ts @@ -0,0 +1,26 @@ +import algosdk from "algosdk"; +import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; +import { SignerTransaction } from "../../../util/commonTypes"; +import { SwapType } from "../../constants"; +import { GenerateSwapRouterTxnsParams, SwapRoute } from "../../types"; +export declare function generateSwapRouterAssetOptInTransaction({ client, routerAppID, assetIDs, accountAddress }: { + client: AlgodClient; + routerAppID: number; + assetIDs: number[]; + accountAddress: string; +}): Promise; +export declare function generateSwapRouterTxns({ initiatorAddr, assetIn, assetOut, client, intermediaryAssetID, routerAppID, network, swapType }: GenerateSwapRouterTxnsParams): Promise<{ + txn: algosdk.Transaction; + signers: string[]; +}[]>; +export declare function getSwapRouterAppOptInRequiredAssetIDs({ client, routerAppID, assetIDs }: { + client: AlgodClient; + routerAppID: number; + assetIDs: number[]; +}): Promise; +export declare function getSwapRoute({ amount, assetInID, assetOutID, swapType }: { + assetInID: number; + assetOutID: number; + swapType: SwapType; + amount: number | bigint; +}): Promise; diff --git a/dist/swap/v2/router/types.d.ts b/dist/swap/v2/router/types.d.ts new file mode 100644 index 00000000..de649885 --- /dev/null +++ b/dist/swap/v2/router/types.d.ts @@ -0,0 +1,14 @@ +import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; +import { AssetWithIdAndAmount } from "../../../util/asset/assetModels"; +import { SupportedNetwork } from "../../../util/commonTypes"; +import { SwapType } from "../../constants"; +export interface GenerateSwapRouterTxnsParams { + client: AlgodClient; + routerAppID: number; + initiatorAddr: string; + assetIn: AssetWithIdAndAmount; + assetOut: AssetWithIdAndAmount; + intermediaryAssetID: number; + swapType: SwapType; + network: SupportedNetwork; +} diff --git a/dist/swap/v2/router/util.d.ts b/dist/swap/v2/router/util.d.ts new file mode 100644 index 00000000..4e996e84 --- /dev/null +++ b/dist/swap/v2/router/util.d.ts @@ -0,0 +1,3 @@ +import { SwapRoute, SwapRouterQuote } from "../../types"; +export declare function getSwapFeesFromSwapRoute(route: SwapRouterQuote[]): number; +export declare function getSwapRouteRate(route: SwapRoute): number; diff --git a/dist/util/asset/assetUtils.d.ts b/dist/util/asset/assetUtils.d.ts index 4ba8e74b..ebb78ce4 100644 --- a/dist/util/asset/assetUtils.d.ts +++ b/dist/util/asset/assetUtils.d.ts @@ -25,3 +25,9 @@ export declare function sortAssetIds(asset1ID: number, asset2ID: number): number * @returns `true` if the given asset id is the ALGO asset id */ export declare function isAlgo(id: number | string): boolean; +/** + * Returns asset.asset_id + */ +export declare function getAssetId(asset: { + id: string | number; +}): number; From a7d7f6ade5174a5b1487f78be3a5149a47202aad Mon Sep 17 00:00:00 2001 From: anlerkan Date: Tue, 28 Feb 2023 03:31:15 +0300 Subject: [PATCH 03/39] fix(swap/util): use isSwapRouterEnabled parameter --- dist/index.js | 2 +- src/swap/utils.ts | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/dist/index.js b/dist/index.js index a9e8c074..0bc4e2c2 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),a=Uint8Array.from([1]);function s(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function u(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){u(s,r,a,o,i,"next",t)}function i(t){u(s,r,a,o,i,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={type:"logicsig",logic:{bytecode:"BoAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgQBbNQA0ADEYEkQxGYEBEkSBAUM="},name:"pool_logicsig"},O={type:"app",global_state_schema:{num_uints:0,num_byte_slices:3},local_state_schema:{num_uints:12,num_byte_slices:2},name:"validator_app"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function z(t,e){var n=Number(t);return Y({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function J(t,e){return Y({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function Y(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(H(e),2),s=a[0],o=a[1],i=v(H(Math.round(Number(K(s,o+r)))),2),u=i[0],c=i[1];return Number(K(u,c-r))}return Math.round(e)}function K(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function H(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(i().mark((function t(e,n){var r,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return u=t.sent,c=u.txId,t.next=13,q(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function tt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function et(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function nt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function rt(t){return(new TextEncoder).encode(t)}var at,st=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return rt("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),ot=(f(at={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(at,S.V2,{testnet:148607e3,mainnet:1002541853}),at);function it(t,e){var n=ot[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ut(){return(ut=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:it(a,s),note:st.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ct(){return(ct=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:it(a,s),note:st.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),lt={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},dt={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"};function ft(){return(ft=c(i().mark((function t(e){var n,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.assetID,s=e.initiatorAddr,t.prev=1,t.next=4,n.getTransactionParams().do();case 4:return o=t.sent,u=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:s,assetIndex:a,amount:0,suggestedParams:o}),t.abrupt("return",[{txn:u,signers:[s]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function mt(t,e){var n=Number(t.id),r=Number(e.id);return n>r?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function gt(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function xt(t){return 0===Number(t)}var It,At,ht,Tt,yt=new(function(e){m(r,pt);var n=y(r);function r(t,e){var a;return p(this,r),(a=n.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,a}return d(r,[{key:"generateLogicSigAccountForPool",value:function(e){if(e.asset1ID===e.asset2ID)throw new Error("Assets are the same");var n=it(e.network,S.V2),r=v(gt(e.asset1ID,e.asset2ID),2),a=r[0],s=r[1],o={bytes:Array.from(Buffer.from(this.poolLogicSigContractTemplate,"base64")),validatorAppId:Array.from(t.encodeUint64(n)),asset1ID:Array.from(t.encodeUint64(a)),asset2ID:Array.from(t.encodeUint64(s))},i=[].concat(_(o.bytes.slice(0,3)),_(o.validatorAppId.slice(0,8)),_(o.asset1ID.slice(0,8)),_(o.asset2ID.slice(0,8)),_(o.bytes.slice(27)));return new t.LogicSigAccount(new Uint8Array(i))}}]),r}())(O,E);exports.SwapType=void 0,(It=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",It.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(Tt||(Tt={}));var vt=(f(At={},exports.SwapType.FixedInput,1),f(At,exports.SwapType.FixedOutput,2),At),_t=rt("swap"),wt=(f(ht={},exports.SwapType.FixedInput,rt("fixed-input")),f(ht,exports.SwapType.FixedOutput,rt("fixed-output")),ht),bt={ASSET_OPT_IN:rt("asset_opt_in")};function Dt(){return(Dt=c(i().mark((function e(n){var a,s,o,u,c,p,l,d;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,u=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s,appArgs:[bt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),console.log(p.fee,"router app opt-in fee"),d=r.default.assignGroupID([p]),e.abrupt("return",[{txn:d[0],signers:[u]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function St(t){return Et.apply(this,arguments)}function Et(){return(Et=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.assetIn,o=n.assetOut,u=n.client,c=n.intermediaryAssetID,p=n.routerAppID,l=n.network,d=n.swapType,e.next=3,u.getTransactionParams().do();case 3:return f=e.sent,m=yt.generateLogicSigAccountForPool({asset1ID:s.id,asset2ID:c,network:l}),g=m.address(),x=yt.generateLogicSigAccountForPool({asset1ID:c,asset2ID:o.id,network:l}),I=x.address(),A=xt(s.id),h=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(p),amount:s.amount,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(p),amount:s.amount,assetIndex:s.id,suggestedParams:f}),T=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:p,appArgs:[_t,wt[d],r.default.encodeUint64(o.amount)],foreignApps:[it(l,S.V2)],foreignAssets:[s.id,c,o.id],accounts:[g,I],suggestedParams:f}),y=d===exports.SwapType.FixedInput?7:8,T.fee=t.ALGORAND_MIN_TX_FEE*(y+1),v=r.default.assignGroupID([h,T]),e.abrupt("return",[{txn:v[Tt.INPUT_TXN],signers:[a]},{txn:v[Tt.APP_CALL_TXN],signers:[a]}]);case 15:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ot(){return(Ot=c(i().mark((function e(n){var r,a,s,o,u,c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nt(t){return Pt.apply(this,arguments)}function Pt(){return(Pt=c(i().mark((function t(e){var n,r,a,s,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.next=4,fetch("https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});case 4:if(200===(u=t.sent).status){t.next=7;break}throw new Error("Failed to fetch swap route quotes");case 7:return t.next=9,u.json();case 9:return c=t.sent,p=c.route,t.abrupt("return",p);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function kt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return z(r.asset.decimals,Number(r.amount))/z(n.asset.decimals,Number(n.amount))}var Lt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Ft={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Rt=new(function(n){m(a,pt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=it(n.network,S.V1_1),a=v(gt(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(i().mark((function t(a,s){var u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:u=t.sent,a(o(o({},u),{},{minimum_required_balance:Vt(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Ct(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?R({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Vt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Xt,jt,Qt=rt("e");function Ut(t){return qt.apply(this,arguments)}function qt(){return(qt=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,u=a.accountAddr,n.next=3,s.accountInformation(u).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=R({stateArray:A}),T=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Gt(){return(Gt=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,u=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==u})),d=[],l&&l["key-value"])for(f=R({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mJt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Jt,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Zt(){return Zt=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d=arguments;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,Mt(n,r);case 6:return o=t.sent,u=Ct(o,it(a,S.V1_1)),c=null,u&&(l=o["created-assets"][0],p=l.index,c={asset1ID:u[Wt[S.V1_1].asset1],asset2ID:u[Wt[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Zt.apply(this,arguments)}var $t=Object.freeze({__proto__:null,getPoolInfo:Yt,getPoolReserves:function(t,e){return Ht.apply(this,arguments)},getPoolAssets:function(t){return Zt.apply(this,arguments)}});function te(t){return ee.apply(this,arguments)}function ee(){return(ee=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Bt(S.V2),u=o.generateLogicSigAccountForPool(e),c=it(r,S.V2),p=u.address(),l=gt(a,s),t.next=8,Mt(n,p);case 8:return d=t.sent,f=Ct(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[Wt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Wt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Wt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Wt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Wt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Wt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Wt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Wt.v2.totalFeeShare]),m.poolTokenID=Number(f[Wt.v2.poolTokenID]),m.asset1ID=Number(f[Wt.v2.asset1]),m.asset2ID=Number(f[Wt.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ne(){return(ne=c(i().mark((function t(e,n){var r,a,s;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Mt(e,n.account.address());case 2:return r=t.sent,a=Ct(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[Wt.v2.asset1Reserves]),s.asset2=BigInt(a[Wt.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[Wt.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function re(){return(re=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,Mt(n,r);case 3:return s=t.sent,o=Ct(s,it(a,S.V2)),u=null,o&&(u={asset1ID:o[Wt[S.V2].asset1],asset2ID:o[Wt[S.V2].asset2],poolTokenID:o[Wt[S.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ae=Object.freeze({__proto__:null,getPoolInfo:te,getPoolReserves:function(t,e){return ne.apply(this,arguments)},getPoolAssets:function(t){return re.apply(this,arguments)}});function se(t){return Boolean(t&&!(t.asset1+t.asset2))}var oe,ie,ue=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=se(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=z(t.asset1,e.asset1)/z(t.asset2,e.asset2)),r},isPoolEmpty:se,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Yt(t),te(t)])}}),ce=o((f(oe={},S.V1_1,o(o({},$t),ue)),f(oe,S.V2,o(o({},ae),ue)),oe),ue);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(ie||(ie={}));var pe=96e4,le=859e3;function de(){return(de=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,O,N;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,u=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=mt(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=xt(x),h=it(a,S.V1_1),T=Rt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:st.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[rt("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:dt.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:fe(A),suggestedParams:c}),(E=[])[ie.FUNDING_TXN]=D,E[ie.VALIDATOR_APP_CALL]=_,E[ie.POOL_TOKEN_CREATE]=w,E[ie.ASSET1_OPT_IN]=b,A||(E[ie.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),O=r.default.assignGroupID(E),N=[{txn:O[ie.FUNDING_TXN],signers:[u]},{txn:O[ie.VALIDATOR_APP_CALL],signers:[y]},{txn:O[ie.POOL_TOKEN_CREATE],signers:[y]},{txn:O[ie.ASSET1_OPT_IN],signers:[y]}],O[ie.ASSET2_OPT_IN]&&N.push({txn:O[ie.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",N);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(t){return t?pe:le}function me(){return(me=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=gt(o,u),f=v(d,2),m=f[0],g=f[1],x=Rt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===ie.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ge(t){return xe.apply(this,arguments)}function xe(){return(xe=c(i().mark((function t(e){var n,r,a,s,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,q(n,a[ie.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Ie(){return(Ie=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,ge({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",ce.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ae,he={generateTxns:function(t){return de.apply(this,arguments)},signTxns:function(t){return me.apply(this,arguments)},execute:function(t){return Ie.apply(this,arguments)},getBootstrapFundingTxnAmount:fe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ae||(Ae={}));var Te=5,ye=6;function ve(t,e){return _e.apply(this,arguments)}function _e(){return(_e=c(i().mark((function e(n,r){var a,s,o;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function we(t,e){return be.apply(this,arguments)}function be(){return(be=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function De(){return(De=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,u=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=it(s,S.V2),d=t.getApplicationAddress(l),f=mt(o,u),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,ce.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(u.unit_name," already exists"));case 12:return I=yt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=xt(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[rt("bootstrap")],note:st.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ee(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Se(h),suggestedParams:p}),(_=[])[Ae.FUNDING_TXN]=y,_[Ae.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Ae.FUNDING_TXN]={txn:w[Ae.FUNDING_TXN],signers:[c]},b[Ae.VALIDATOR_APP_CALL]={txn:w[Ae.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Se(t){return function(t,e){var n=Bt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ee(t)+1e5}function Ee(e){return((e?Te:ye)+1)*t.ALGORAND_MIN_TX_FEE}function Oe(){return(Oe=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=gt(o,u),f=v(d,2),m=f[0],g=f[1],x=yt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ae.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ne(){return(Ne=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,ve(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Wt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",ce.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Pe,ke={generateTxns:function(t){return De.apply(this,arguments)},signTxns:function(t){return Oe.apply(this,arguments)},execute:function(t){return Ne.apply(this,arguments)},getBootstrapFundingTxnAmount:Se,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Se(e)}};var Le,Fe=(f(Pe={},S.V1_1,he),f(Pe,S.V2,ke),f(Pe,"generateTxns",(function(t){return t.contractVersion===S.V1_1?he.generateTxns(t):ke.generateTxns(t)})),f(Pe,"signTxns",(function(t){return t.contractVersion===S.V1_1?he.signTxns(t):ke.signTxns(t)})),f(Pe,"execute",(function(t){return t.contractVersion===S.V1_1?he.execute(t):ke.execute(t)})),f(Pe,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?he.getBootstrapFundingTxnAmount(n):ke.getBootstrapFundingTxnAmount(n)})),Pe);exports.V1_1AddLiquidityTxnIndices=void 0,(Le=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Le.FEE_TXN=0]="FEE_TXN",Le[Le.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Le[Le.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Le[Le.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Le[Le.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Re,Be,Me,Ce,Ve=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Ce=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Ce.FLEXIBLE="flexible",Ce.INITIAL="initial";var Xe,je=(f(Re={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Re,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Re,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Re),Qe=(f(Be={},exports.V2AddLiquidityType.INITIAL,1),f(Be,exports.V2AddLiquidityType.SINGLE,2),f(Be,exports.V2AddLiquidityType.FLEXIBLE,2),Be),Ue=(f(Me={},exports.V2AddLiquidityType.INITIAL,3),f(Me,exports.V2AddLiquidityType.FLEXIBLE,3),f(Me,exports.V2AddLiquidityType.SINGLE,2),Me);function qe(t){var e=t.assetIn,n=t.assetOut;return z(n.decimals,Number(n.amount))/z(e.decimals,Number(e.amount))}function Ge(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=qe({assetIn:r,assetOut:a}),o=z(a.decimals,Number(n))/z(r.decimals,Number(e));return Y({decimalPlaces:5},Math.abs(s/o-1))}function We(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,i=t.asset1Amount,u=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(i),d=s.asset2+BigInt(u),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(i)-x,h=BigInt(u)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=ze(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=ze(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:Ge({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function ze(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Je(e){return(Qe[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ye=rt("add_liquidity"),Ke=(f(Xe={},S.V1_1,[rt("mint")]),f(Xe,S.V2,{INITIAL_LIQUIDITY:[rt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ye,rt("single")],FLEXIBLE_MODE:[Ye,rt("flexible")]}),Xe);function He(){return(He=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,u=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=W("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:it(s,S.V1_1),appArgs:Ke.v1_1,accounts:[d],note:st.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[u.id,p.id]:[u.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ze(){return(Ze=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $e(){return($e=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,u=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Ut({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,Z(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=tt(a),x=et(a),t.next=17,Ut({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var tn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function wn(){return(wn=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!e.isUsingSwapRouter){t.next=2;break}return t.abrupt("return",St(e));case 2:if(n=e.assetIn,a=e.assetOut,s=e.client,o=e.initiatorAddr,u=e.pool,c=e.slippage,p=e.swapType,l=u.account.address(),(d=[u.asset1ID,u.asset2ID]).includes(n.id)&&d.includes(a.id)&&n.id!==a.id){t.next=7;break}throw new F({pool:u,assetIn:n,assetOut:a},"Input asset (#".concat(n.id,") and output asset (#").concat(a.id,") provided to generate transactions do not belong to the pool ").concat(l,"."));case 7:return t.next=9,s.getTransactionParams().do();case 9:return f=t.sent,m=xt(n.id),g=p===exports.SwapType.FixedInput?n.amount:W("positive",c,n.amount),x=p===exports.SwapType.FixedOutput?a.amount:W("negative",c,a.amount),I=m?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,assetIndex:n.id,suggestedParams:f}),(A=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:u.validatorAppID,appArgs:[_t,wt[p],r.default.encodeUint64(x)],note:st.getAppCallTxnNoteWithClientName(S.V2),accounts:[l],foreignAssets:[u.asset1ID,u.asset2ID],suggestedParams:f})).fee=bn(p),(h=[])[Tt.INPUT_TXN]=I,h[Tt.APP_CALL_TXN]=A,T=r.default.assignGroupID(h),t.abrupt("return",[{txn:T[Tt.INPUT_TXN],signers:[o]},{txn:T[Tt.APP_CALL_TXN],signers:[o]}]);case 21:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function bn(e){return(vt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Dn(){return(Dn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,u=e.signedTxns,c=e.assetIn,t.next=3,Z(a,[u]);case 3:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=10,we(a,o);case 10:return g=t.sent,x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.route[s.route.length-1].quote.amount_out.asset.id),I=null==g||null===(n=g.find((function(t){return t.txn.txn.xaid===c.id})))||void 0===n?void 0:n.txn.txn,A=null==g||null===(r=g.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:f,assetIn:I&&{amount:BigInt(c.amount)-BigInt(I.aamt||0),id:c.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:m});case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Sn(){return(Sn=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.asset,r=e.decimals,a=e.pool,s=e.type,o=e.isSwapRouterEnabled,s!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,En({pool:a,assetIn:n,decimals:r,isSwapRouterEnabled:o});case 4:u=t.sent,t.next=10;break;case 7:return t.next=9,Nn({pool:a,assetOut:n,decimals:r,isSwapRouterEnabled:o});case 9:u=t.sent;case 10:return t.abrupt("return",u);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(t){return On.apply(this,arguments)}function On(){return(On=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetIn,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:n,assetIn:r},"Trying to swap on a non-existent pool");case 3:if(o=BigInt(r.amount),u=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}c=n.asset2ID,p=n.asset1Reserves,l=n.asset2Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}c=n.asset1ID,p=n.asset2Reserves,l=n.asset1Reserves,t.next=18;break;case 17:throw new F({pool:n,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(d=kn({inputSupply:p,outputSupply:l,swapInputAmount:o,totalFeeShare:u,decimals:a}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact,!(f>l)){t.next=21;break}throw new _n;case 21:if(x={assetInID:r.id,assetInAmount:o,assetOutID:c,assetOutAmount:f,swapFee:Number(m),rate:z(a.assetOut,Number(f))/z(a.assetIn,Number(o)),priceImpact:g},!s){t.next=28;break}return t.next=25,Nt({amount:r.amount,assetInID:Number(r.id),assetOutID:c,swapType:exports.SwapType.FixedInput});case 25:if(!((I=t.sent).length>1&&BigInt(I[I.length-1].quote.amount_out.amount)>x.assetOutAmount)){t.next=28;break}return t.abrupt("return",{route:I,type:exports.SwapQuoteType.Router});case 28:return t.abrupt("return",{quoteWithPool:{pool:n,quote:x},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nn(t){return Pn.apply(this,arguments)}function Pn(){return(Pn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetOut,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:n,assetOut:r},"Trying to swap on a non-existent pool");case 3:if(o=BigInt(r.amount),u=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}c=n.asset2ID,p=n.asset2Reserves,l=n.asset1Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}c=n.asset1ID,p=n.asset1Reserves,l=n.asset2Reserves,t.next=18;break;case 17:throw new F({pool:n,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(d=Ln({inputSupply:p,outputSupply:l,swapOutputAmount:o,totalFeeShare:u,decimals:a}),f=d.swapInputAmount,m=d.totalFeeAmount,g=d.priceImpact,!(o>l)){t.next=21;break}throw new _n;case 21:if(x={assetInID:c,assetInAmount:f,assetOutID:r.id,assetOutAmount:o,swapFee:Number(m),rate:z(a.assetOut,Number(o))/z(a.assetIn,Number(f)),priceImpact:g},!s){t.next=28;break}return t.next=25,Nt({amount:r.amount,assetInID:c,assetOutID:r.id,swapType:exports.SwapType.FixedInput});case 25:if(!((I=t.sent).length>1&&BigInt(I[I.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Bn(){return(Bn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,u=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s.validatorAppID,note:st.getAppCallTxnNoteWithClientName(S.V2),appArgs:[In,r.default.encodeUint64(W("negative",l,c)),r.default.encodeUint64(W("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[gn.ASSET_TRANSFER_TXN]=g,I[gn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[gn.ASSET_TRANSFER_TXN],signers:[u]},{txn:A[gn.APP_CALL_TXN],signers:[u]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,u=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=W("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:u,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:st.getAppCallTxnNoteWithClientName(S.V2),appArgs:[In,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[gn.ASSET_TRANSFER_TXN]=T,v[gn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[gn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[gn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Cn(){return(Cn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,Z(r,[s]);case 3:return o=t.sent,u=v(o,1),c=u[0].txnID,t.next=8,we(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Vn,Xn,jn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Rn(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=Rn(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Fn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Fn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Bn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Mn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Cn.apply(this,arguments)}},Qn=(f(Vn={},S.V1_1,vn),f(Vn,S.V2,jn),Vn),Un=3n,qn=1000n;function Gn(){return(Gn=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,u=v(o,2),c=u[0],p=u[1],l=a.map((function(t,e){return e===Xn.FEE_TXN_INDEX?c:e===Xn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Wn(){return(Wn=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,u=e.assetIn,c=e.assetOut,p=e.slippage,l=e.initiatorAddr,d=s.account.address(),(f=[s.asset1ID,s.asset2ID]).includes(u.id)&&f.includes(c.id)&&u.id!==c.id){t.next=5;break}throw new F({pool:s,assetIn:u,assetOut:c},"Input asset (#".concat(u.id,") and output asset (#").concat(c.id,") provided to generate transactions do not belong to the pool ").concat(d,"."));case 5:return t.next=7,n.getTransactionParams().do();case 7:return m=t.sent,g=[rt("swap"),o===exports.SwapType.FixedInput?rt("fi"):rt("fo")],x=r.default.makeApplicationNoOpTxnFromObject({from:d,appIndex:s.validatorAppID,appArgs:g,accounts:[l],note:st.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:m}),I=o===exports.SwapType.FixedOutput?W("positive",p,u.amount):u.amount,A=0===u.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:u.id,amount:I,suggestedParams:m}),h=o===exports.SwapType.FixedInput?W("negative",p,c.amount):c.amount,T=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:l,amount:h,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:l,assetIndex:c.id,amount:h,suggestedParams:m}),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.fee+T.fee,note:a,suggestedParams:m}),v=r.default.assignGroupID([y,x,A,T]),t.abrupt("return",[{txn:v[0],signers:[l]},{txn:v[1],signers:[d]},{txn:v[2],signers:[l]},{txn:v[3],signers:[d]}]);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function zn(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,i,u,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*Un/qn,l=u-i*u/(i+(c-p));if(l>u)throw new _n;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:qe(d),priceImpact:Ge(o({inputSupply:i,outputSupply:u},d))}}function Jn(t){return Yn.apply(this,arguments)}function Yn(){return(Yn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,Ut({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Z(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ut({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Kn(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new _n;var c=o*i/(i-u)-o,p=c*qn/(qn-Un),l=p-c,d=z(a.assetOut,Number(u))/z(a.assetIn,Number(p)),f=z(a.assetOut,Number(i))/z(a.assetIn,Number(o)),m=Y({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m}}function Hn(t){return Zn.apply(this,arguments)}function Zn(){return(Zn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,Ut({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Z(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ut({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(){return($n=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Xn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Xn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Xn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Xn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,Jn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Hn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:et(s),fees:tt(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Xn||(Xn={}));var tr,er={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?zn({pool:e,reserves:n,assetIn:r,decimals:a}):Kn({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:zn,getFixedOutputSwapQuote:Kn,generateTxns:function(t){return Wn.apply(this,arguments)},signTxns:function(t){return Gn.apply(this,arguments)},execute:function(t){return $n.apply(this,arguments)},executeFixedOutputSwap:Hn},nr=4*t.ALGORAND_MIN_TX_FEE;function rr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof _n})))throw new _n;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function ar(){return ar=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=s.map((function(t){return new Promise(function(){var e=c(i().mark((function e(s,o){var u,c;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(c={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}u={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:er.getFixedInputSwapQuote(c),pool:t.info}},e.next=9;break;case 6:return e.next=8,Fn.getFixedInputSwapQuote(c);case 8:u=e.sent;case 9:s(u),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),o(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,rr(o);case 4:return u=t.sent,t.abrupt("return",ir(u));case 6:case"end":return t.stop()}}),t)}))),ar.apply(this,arguments)}function sr(){return sr=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=s.map((function(t){return new Promise(function(){var e=c(i().mark((function e(s,o){var u,c;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(c={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}u={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:er.getFixedOutputSwapQuote(c),pool:t.info}},e.next=9;break;case 6:return e.next=8,Fn.getFixedOutputSwapQuote(c);case 8:u=e.sent;case 9:s(u),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),o(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,rr(o);case 4:return u=t.sent,t.abrupt("return",ir(u));case 6:case"end":return t.stop()}}),t)}))),sr.apply(this,arguments)}function or(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:kt(t.route)}function ir(t){var e,n=t[0],r=or(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=or(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}var ur=(f(tr={},S.V1_1,er),f(tr,S.V2,Fn),f(tr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return se(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return ar.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return sr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(tr,"generateTxns",(function(t){return t.isUsingSwapRouter||t.pool.contractVersion!==S.V1_1?Fn.generateTxns(t):er.generateTxns(t)})),f(tr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&function(t){if(t.type===exports.SwapQuoteType.Direct)return t.quoteWithPool.pool.contractVersion;return S.V2}(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return er.signTxns(o({pool:e},t))}return Fn.signTxns(t)})),f(tr,"execute",(function(t){return t.contractVersion===S.V1_1?er.execute(t):Fn.execute(t)})),tr);function cr(){return(cr=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,pr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,Z(n,[o]);case 7:return u=t.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:tt(a),confirmedRound:d,txnID:l,groupID:et(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function pr(t){return lr.apply(this,arguments)}function lr(){return(lr=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,u=v(o,1),c=u[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dr(){return dr=c(i().mark((function t(e){var n,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:tt(e),groupID:et(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return u=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(i().mark((function a(s,o){var c,p,l,d,f,m;return i().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?u[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,Z(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),dr.apply(this,arguments)}function fr(){return(fr=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,u=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[rt("redeem")],note:st.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(u),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(u),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[rt("commit"),u],note:B([rt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function gr(){return(gr=c(i().mark((function e(n){var r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=mr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[rt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=lt,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=mn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Fe,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=dt,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Qn,exports.Swap=ur,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=xn,exports.V2AddLiquidityTxnIndices=je,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=W,exports.calculateAccountMinimumRequiredBalance=Vt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=ce,exports.prepareCommitTransactions=function(t){return gr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return dr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return cr.apply(this,arguments)},exports.sendAndWaitRawTransaction=Z,exports.sumUpTxnFees=tt,exports.tinymanContract_v1_1=Rt,exports.tinymanContract_v2=yt,exports.tinymanJSSDKConfig=st; +i=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",s=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function u(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,s=Object.create(a.prototype),o=new w(r||[]);return s._invoke=function(t,e,n){var r="suspendedStart";return function(a,s){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw s;return D()}for(n.method=a,n.arg=s;;){var o=n.delegate;if(o){var i=y(o,n);if(i){if(i===l)continue;return i}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var u=p(t,e,n);if("normal"===u.type){if(r=n.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:n.done}}"throw"===u.type&&(r="completed",n.method="throw",n.arg=u.arg)}}}(t,n,o),s}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};u(g,a,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,a)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,s,o,i){var u=p(t[a],t,s);if("throw"!==u.type){var c=u.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,i)}),(function(t){r("throw",t,o,i)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,i)}))}i(u.arg)}var a;this._invoke=function(t,n){function s(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(s,s):s()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function w(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,s=function e(){for(;++r=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function u(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){u(s,r,a,o,i,"next",t)}function i(t){u(s,r,a,o,i,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={type:"logicsig",logic:{bytecode:"BoAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgQBbNQA0ADEYEkQxGYEBEkSBAUM="},name:"pool_logicsig"},O={type:"app",global_state_schema:{num_uints:0,num_byte_slices:3},local_state_schema:{num_uints:12,num_byte_slices:2},name:"validator_app"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function z(t,e){var n=Number(t);return Y({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function J(t,e){return Y({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function Y(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(H(e),2),s=a[0],o=a[1],i=v(H(Math.round(Number(K(s,o+r)))),2),u=i[0],c=i[1];return Number(K(u,c-r))}return Math.round(e)}function K(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function H(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(i().mark((function t(e,n){var r,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return u=t.sent,c=u.txId,t.next=13,q(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function tt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function et(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function nt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function rt(t){return(new TextEncoder).encode(t)}var at,st=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return rt("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),ot=(f(at={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(at,S.V2,{testnet:148607e3,mainnet:1002541853}),at);function it(t,e){var n=ot[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ut(){return(ut=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:it(a,s),note:st.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ct(){return(ct=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:it(a,s),note:st.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),lt={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},dt={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"};function ft(){return(ft=c(i().mark((function t(e){var n,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.assetID,s=e.initiatorAddr,t.prev=1,t.next=4,n.getTransactionParams().do();case 4:return o=t.sent,u=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:s,assetIndex:a,amount:0,suggestedParams:o}),t.abrupt("return",[{txn:u,signers:[s]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function mt(t,e){var n=Number(t.id),r=Number(e.id);return n>r?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function gt(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function xt(t){return 0===Number(t)}var It,At,ht,Tt,yt=new(function(e){m(r,pt);var n=y(r);function r(t,e){var a;return p(this,r),(a=n.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,a}return d(r,[{key:"generateLogicSigAccountForPool",value:function(e){if(e.asset1ID===e.asset2ID)throw new Error("Assets are the same");var n=it(e.network,S.V2),r=v(gt(e.asset1ID,e.asset2ID),2),a=r[0],s=r[1],o={bytes:Array.from(Buffer.from(this.poolLogicSigContractTemplate,"base64")),validatorAppId:Array.from(t.encodeUint64(n)),asset1ID:Array.from(t.encodeUint64(a)),asset2ID:Array.from(t.encodeUint64(s))},i=[].concat(_(o.bytes.slice(0,3)),_(o.validatorAppId.slice(0,8)),_(o.asset1ID.slice(0,8)),_(o.asset2ID.slice(0,8)),_(o.bytes.slice(27)));return new t.LogicSigAccount(new Uint8Array(i))}}]),r}())(O,E);exports.SwapType=void 0,(It=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",It.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(Tt||(Tt={}));var vt=(f(At={},exports.SwapType.FixedInput,1),f(At,exports.SwapType.FixedOutput,2),At),_t=rt("swap"),wt=(f(ht={},exports.SwapType.FixedInput,rt("fixed-input")),f(ht,exports.SwapType.FixedOutput,rt("fixed-output")),ht),bt={ASSET_OPT_IN:rt("asset_opt_in")};function Dt(){return(Dt=c(i().mark((function e(n){var a,s,o,u,c,p,l,d;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,u=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s,appArgs:[bt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),console.log(p.fee,"router app opt-in fee"),d=r.default.assignGroupID([p]),e.abrupt("return",[{txn:d[0],signers:[u]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function St(t){return Et.apply(this,arguments)}function Et(){return(Et=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.assetIn,o=n.assetOut,u=n.client,c=n.intermediaryAssetID,p=n.routerAppID,l=n.network,d=n.swapType,e.next=3,u.getTransactionParams().do();case 3:return f=e.sent,m=yt.generateLogicSigAccountForPool({asset1ID:s.id,asset2ID:c,network:l}),g=m.address(),x=yt.generateLogicSigAccountForPool({asset1ID:c,asset2ID:o.id,network:l}),I=x.address(),A=xt(s.id),h=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(p),amount:s.amount,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(p),amount:s.amount,assetIndex:s.id,suggestedParams:f}),T=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:p,appArgs:[_t,wt[d],r.default.encodeUint64(o.amount)],foreignApps:[it(l,S.V2)],foreignAssets:[s.id,c,o.id],accounts:[g,I],suggestedParams:f}),y=d===exports.SwapType.FixedInput?7:8,T.fee=t.ALGORAND_MIN_TX_FEE*(y+1),v=r.default.assignGroupID([h,T]),e.abrupt("return",[{txn:v[Tt.INPUT_TXN],signers:[a]},{txn:v[Tt.APP_CALL_TXN],signers:[a]}]);case 15:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ot(){return(Ot=c(i().mark((function e(n){var r,a,s,o,u,c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nt(t){return Pt.apply(this,arguments)}function Pt(){return(Pt=c(i().mark((function t(e){var n,r,a,s,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.next=4,fetch("https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});case 4:if(200===(u=t.sent).status){t.next=7;break}throw new Error("Failed to fetch swap route quotes");case 7:return t.next=9,u.json();case 9:return c=t.sent,p=c.route,t.abrupt("return",p);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function kt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return z(r.asset.decimals,Number(r.amount))/z(n.asset.decimals,Number(n.amount))}var Lt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Ft={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Rt=new(function(n){m(a,pt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=it(n.network,S.V1_1),a=v(gt(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(i().mark((function t(a,s){var u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:u=t.sent,a(o(o({},u),{},{minimum_required_balance:Vt(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Ct(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?R({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Vt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Xt,jt,Qt=rt("e");function Ut(t){return qt.apply(this,arguments)}function qt(){return(qt=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,u=a.accountAddr,n.next=3,s.accountInformation(u).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=R({stateArray:A}),T=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Gt(){return(Gt=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,u=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==u})),d=[],l&&l["key-value"])for(f=R({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mJt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Jt,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Zt(){return Zt=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d=arguments;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,Mt(n,r);case 6:return o=t.sent,u=Ct(o,it(a,S.V1_1)),c=null,u&&(l=o["created-assets"][0],p=l.index,c={asset1ID:u[Wt[S.V1_1].asset1],asset2ID:u[Wt[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Zt.apply(this,arguments)}var $t=Object.freeze({__proto__:null,getPoolInfo:Yt,getPoolReserves:function(t,e){return Ht.apply(this,arguments)},getPoolAssets:function(t){return Zt.apply(this,arguments)}});function te(t){return ee.apply(this,arguments)}function ee(){return(ee=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Bt(S.V2),u=o.generateLogicSigAccountForPool(e),c=it(r,S.V2),p=u.address(),l=gt(a,s),t.next=8,Mt(n,p);case 8:return d=t.sent,f=Ct(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[Wt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Wt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Wt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Wt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Wt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Wt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Wt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Wt.v2.totalFeeShare]),m.poolTokenID=Number(f[Wt.v2.poolTokenID]),m.asset1ID=Number(f[Wt.v2.asset1]),m.asset2ID=Number(f[Wt.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ne(){return(ne=c(i().mark((function t(e,n){var r,a,s;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Mt(e,n.account.address());case 2:return r=t.sent,a=Ct(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[Wt.v2.asset1Reserves]),s.asset2=BigInt(a[Wt.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[Wt.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function re(){return(re=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,Mt(n,r);case 3:return s=t.sent,o=Ct(s,it(a,S.V2)),u=null,o&&(u={asset1ID:o[Wt[S.V2].asset1],asset2ID:o[Wt[S.V2].asset2],poolTokenID:o[Wt[S.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ae=Object.freeze({__proto__:null,getPoolInfo:te,getPoolReserves:function(t,e){return ne.apply(this,arguments)},getPoolAssets:function(t){return re.apply(this,arguments)}});function se(t){return Boolean(t&&!(t.asset1+t.asset2))}var oe,ie,ue=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=se(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=z(t.asset1,e.asset1)/z(t.asset2,e.asset2)),r},isPoolEmpty:se,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Yt(t),te(t)])}}),ce=o((f(oe={},S.V1_1,o(o({},$t),ue)),f(oe,S.V2,o(o({},ae),ue)),oe),ue);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(ie||(ie={}));var pe=96e4,le=859e3;function de(){return(de=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,O,N;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,u=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=mt(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=xt(x),h=it(a,S.V1_1),T=Rt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:st.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[rt("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:dt.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:fe(A),suggestedParams:c}),(E=[])[ie.FUNDING_TXN]=D,E[ie.VALIDATOR_APP_CALL]=_,E[ie.POOL_TOKEN_CREATE]=w,E[ie.ASSET1_OPT_IN]=b,A||(E[ie.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),O=r.default.assignGroupID(E),N=[{txn:O[ie.FUNDING_TXN],signers:[u]},{txn:O[ie.VALIDATOR_APP_CALL],signers:[y]},{txn:O[ie.POOL_TOKEN_CREATE],signers:[y]},{txn:O[ie.ASSET1_OPT_IN],signers:[y]}],O[ie.ASSET2_OPT_IN]&&N.push({txn:O[ie.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",N);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(t){return t?pe:le}function me(){return(me=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=gt(o,u),f=v(d,2),m=f[0],g=f[1],x=Rt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===ie.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ge(t){return xe.apply(this,arguments)}function xe(){return(xe=c(i().mark((function t(e){var n,r,a,s,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,q(n,a[ie.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Ie(){return(Ie=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,ge({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",ce.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ae,he={generateTxns:function(t){return de.apply(this,arguments)},signTxns:function(t){return me.apply(this,arguments)},execute:function(t){return Ie.apply(this,arguments)},getBootstrapFundingTxnAmount:fe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ae||(Ae={}));var Te=5,ye=6;function ve(t,e){return _e.apply(this,arguments)}function _e(){return(_e=c(i().mark((function e(n,r){var a,s,o;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function we(t,e){return be.apply(this,arguments)}function be(){return(be=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function De(){return(De=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,u=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=it(s,S.V2),d=t.getApplicationAddress(l),f=mt(o,u),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,ce.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(u.unit_name," already exists"));case 12:return I=yt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=xt(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[rt("bootstrap")],note:st.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ee(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Se(h),suggestedParams:p}),(_=[])[Ae.FUNDING_TXN]=y,_[Ae.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Ae.FUNDING_TXN]={txn:w[Ae.FUNDING_TXN],signers:[c]},b[Ae.VALIDATOR_APP_CALL]={txn:w[Ae.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Se(t){return function(t,e){var n=Bt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ee(t)+1e5}function Ee(e){return((e?Te:ye)+1)*t.ALGORAND_MIN_TX_FEE}function Oe(){return(Oe=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=gt(o,u),f=v(d,2),m=f[0],g=f[1],x=yt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ae.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ne(){return(Ne=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,ve(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Wt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",ce.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Pe,ke={generateTxns:function(t){return De.apply(this,arguments)},signTxns:function(t){return Oe.apply(this,arguments)},execute:function(t){return Ne.apply(this,arguments)},getBootstrapFundingTxnAmount:Se,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Se(e)}};var Le,Fe=(f(Pe={},S.V1_1,he),f(Pe,S.V2,ke),f(Pe,"generateTxns",(function(t){return t.contractVersion===S.V1_1?he.generateTxns(t):ke.generateTxns(t)})),f(Pe,"signTxns",(function(t){return t.contractVersion===S.V1_1?he.signTxns(t):ke.signTxns(t)})),f(Pe,"execute",(function(t){return t.contractVersion===S.V1_1?he.execute(t):ke.execute(t)})),f(Pe,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?he.getBootstrapFundingTxnAmount(n):ke.getBootstrapFundingTxnAmount(n)})),Pe);exports.V1_1AddLiquidityTxnIndices=void 0,(Le=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Le.FEE_TXN=0]="FEE_TXN",Le[Le.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Le[Le.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Le[Le.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Le[Le.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Re,Be,Me,Ce,Ve=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Ce=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Ce.FLEXIBLE="flexible",Ce.INITIAL="initial";var Xe,je=(f(Re={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Re,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Re,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Re),Qe=(f(Be={},exports.V2AddLiquidityType.INITIAL,1),f(Be,exports.V2AddLiquidityType.SINGLE,2),f(Be,exports.V2AddLiquidityType.FLEXIBLE,2),Be),Ue=(f(Me={},exports.V2AddLiquidityType.INITIAL,3),f(Me,exports.V2AddLiquidityType.FLEXIBLE,3),f(Me,exports.V2AddLiquidityType.SINGLE,2),Me);function qe(t){var e=t.assetIn,n=t.assetOut;return z(n.decimals,Number(n.amount))/z(e.decimals,Number(e.amount))}function Ge(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=qe({assetIn:r,assetOut:a}),o=z(a.decimals,Number(n))/z(r.decimals,Number(e));return Y({decimalPlaces:5},Math.abs(s/o-1))}function We(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,i=t.asset1Amount,u=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(i),d=s.asset2+BigInt(u),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(i)-x,h=BigInt(u)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=ze(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=ze(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:Ge({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function ze(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Je(e){return(Qe[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ye=rt("add_liquidity"),Ke=(f(Xe={},S.V1_1,[rt("mint")]),f(Xe,S.V2,{INITIAL_LIQUIDITY:[rt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ye,rt("single")],FLEXIBLE_MODE:[Ye,rt("flexible")]}),Xe);function He(){return(He=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,u=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=W("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:it(s,S.V1_1),appArgs:Ke.v1_1,accounts:[d],note:st.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[u.id,p.id]:[u.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ze(){return(Ze=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $e(){return($e=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,u=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Ut({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,Z(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=tt(a),x=et(a),t.next=17,Ut({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var tn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function wn(){return(wn=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!e.isUsingSwapRouter){t.next=2;break}return t.abrupt("return",St(e));case 2:if(n=e.assetIn,a=e.assetOut,s=e.client,o=e.initiatorAddr,u=e.pool,c=e.slippage,p=e.swapType,l=u.account.address(),(d=[u.asset1ID,u.asset2ID]).includes(n.id)&&d.includes(a.id)&&n.id!==a.id){t.next=7;break}throw new F({pool:u,assetIn:n,assetOut:a},"Input asset (#".concat(n.id,") and output asset (#").concat(a.id,") provided to generate transactions do not belong to the pool ").concat(l,"."));case 7:return t.next=9,s.getTransactionParams().do();case 9:return f=t.sent,m=xt(n.id),g=p===exports.SwapType.FixedInput?n.amount:W("positive",c,n.amount),x=p===exports.SwapType.FixedOutput?a.amount:W("negative",c,a.amount),I=m?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,assetIndex:n.id,suggestedParams:f}),(A=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:u.validatorAppID,appArgs:[_t,wt[p],r.default.encodeUint64(x)],note:st.getAppCallTxnNoteWithClientName(S.V2),accounts:[l],foreignAssets:[u.asset1ID,u.asset2ID],suggestedParams:f})).fee=bn(p),(h=[])[Tt.INPUT_TXN]=I,h[Tt.APP_CALL_TXN]=A,T=r.default.assignGroupID(h),t.abrupt("return",[{txn:T[Tt.INPUT_TXN],signers:[o]},{txn:T[Tt.APP_CALL_TXN],signers:[o]}]);case 21:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function bn(e){return(vt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Dn(){return(Dn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,u=e.signedTxns,c=e.assetIn,t.next=3,Z(a,[u]);case 3:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=10,we(a,o);case 10:return g=t.sent,x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.route[s.route.length-1].quote.amount_out.asset.id),I=null==g||null===(n=g.find((function(t){return t.txn.txn.xaid===c.id})))||void 0===n?void 0:n.txn.txn,A=null==g||null===(r=g.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:f,assetIn:I&&{amount:BigInt(c.amount)-BigInt(I.aamt||0),id:c.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:m});case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Sn(){return(Sn=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.asset,r=e.decimals,a=e.pool,s=e.type,o=e.isSwapRouterEnabled,s!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,En({pool:a,assetIn:n,decimals:r,isSwapRouterEnabled:o});case 4:u=t.sent,t.next=10;break;case 7:return t.next=9,Nn({pool:a,assetOut:n,decimals:r,isSwapRouterEnabled:o});case 9:u=t.sent;case 10:return t.abrupt("return",u);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(t){return On.apply(this,arguments)}function On(){return(On=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetIn,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:n,assetIn:r},"Trying to swap on a non-existent pool");case 3:if(o=BigInt(r.amount),u=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}c=n.asset2ID,p=n.asset1Reserves,l=n.asset2Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}c=n.asset1ID,p=n.asset2Reserves,l=n.asset1Reserves,t.next=18;break;case 17:throw new F({pool:n,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(d=kn({inputSupply:p,outputSupply:l,swapInputAmount:o,totalFeeShare:u,decimals:a}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact,!(f>l)){t.next=21;break}throw new _n;case 21:if(x={assetInID:r.id,assetInAmount:o,assetOutID:c,assetOutAmount:f,swapFee:Number(m),rate:z(a.assetOut,Number(f))/z(a.assetIn,Number(o)),priceImpact:g},!s){t.next=28;break}return t.next=25,Nt({amount:r.amount,assetInID:Number(r.id),assetOutID:c,swapType:exports.SwapType.FixedInput});case 25:if(!((I=t.sent).length>1&&BigInt(I[I.length-1].quote.amount_out.amount)>x.assetOutAmount)){t.next=28;break}return t.abrupt("return",{route:I,type:exports.SwapQuoteType.Router});case 28:return t.abrupt("return",{quoteWithPool:{pool:n,quote:x},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nn(t){return Pn.apply(this,arguments)}function Pn(){return(Pn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetOut,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:n,assetOut:r},"Trying to swap on a non-existent pool");case 3:if(o=BigInt(r.amount),u=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}c=n.asset2ID,p=n.asset2Reserves,l=n.asset1Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}c=n.asset1ID,p=n.asset1Reserves,l=n.asset2Reserves,t.next=18;break;case 17:throw new F({pool:n,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(d=Ln({inputSupply:p,outputSupply:l,swapOutputAmount:o,totalFeeShare:u,decimals:a}),f=d.swapInputAmount,m=d.totalFeeAmount,g=d.priceImpact,!(o>l)){t.next=21;break}throw new _n;case 21:if(x={assetInID:c,assetInAmount:f,assetOutID:r.id,assetOutAmount:o,swapFee:Number(m),rate:z(a.assetOut,Number(o))/z(a.assetIn,Number(f)),priceImpact:g},!s){t.next=28;break}return t.next=25,Nt({amount:r.amount,assetInID:c,assetOutID:r.id,swapType:exports.SwapType.FixedInput});case 25:if(!((I=t.sent).length>1&&BigInt(I[I.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Bn(){return(Bn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,u=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s.validatorAppID,note:st.getAppCallTxnNoteWithClientName(S.V2),appArgs:[In,r.default.encodeUint64(W("negative",l,c)),r.default.encodeUint64(W("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[gn.ASSET_TRANSFER_TXN]=g,I[gn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[gn.ASSET_TRANSFER_TXN],signers:[u]},{txn:A[gn.APP_CALL_TXN],signers:[u]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,u=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=W("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:u,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:st.getAppCallTxnNoteWithClientName(S.V2),appArgs:[In,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[gn.ASSET_TRANSFER_TXN]=T,v[gn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[gn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[gn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Cn(){return(Cn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,Z(r,[s]);case 3:return o=t.sent,u=v(o,1),c=u[0].txnID,t.next=8,we(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Vn,Xn,jn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Rn(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=Rn(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Fn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Fn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Bn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Mn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Cn.apply(this,arguments)}},Qn=(f(Vn={},S.V1_1,vn),f(Vn,S.V2,jn),Vn),Un=3n,qn=1000n;function Gn(){return(Gn=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,u=v(o,2),c=u[0],p=u[1],l=a.map((function(t,e){return e===Xn.FEE_TXN_INDEX?c:e===Xn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Wn(){return(Wn=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,u=e.assetIn,c=e.assetOut,p=e.slippage,l=e.initiatorAddr,d=s.account.address(),(f=[s.asset1ID,s.asset2ID]).includes(u.id)&&f.includes(c.id)&&u.id!==c.id){t.next=5;break}throw new F({pool:s,assetIn:u,assetOut:c},"Input asset (#".concat(u.id,") and output asset (#").concat(c.id,") provided to generate transactions do not belong to the pool ").concat(d,"."));case 5:return t.next=7,n.getTransactionParams().do();case 7:return m=t.sent,g=[rt("swap"),o===exports.SwapType.FixedInput?rt("fi"):rt("fo")],x=r.default.makeApplicationNoOpTxnFromObject({from:d,appIndex:s.validatorAppID,appArgs:g,accounts:[l],note:st.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:m}),I=o===exports.SwapType.FixedOutput?W("positive",p,u.amount):u.amount,A=0===u.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:u.id,amount:I,suggestedParams:m}),h=o===exports.SwapType.FixedInput?W("negative",p,c.amount):c.amount,T=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:l,amount:h,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:l,assetIndex:c.id,amount:h,suggestedParams:m}),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.fee+T.fee,note:a,suggestedParams:m}),v=r.default.assignGroupID([y,x,A,T]),t.abrupt("return",[{txn:v[0],signers:[l]},{txn:v[1],signers:[d]},{txn:v[2],signers:[l]},{txn:v[3],signers:[d]}]);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function zn(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,i,u,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*Un/qn,l=u-i*u/(i+(c-p));if(l>u)throw new _n;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:qe(d),priceImpact:Ge(o({inputSupply:i,outputSupply:u},d))}}function Jn(t){return Yn.apply(this,arguments)}function Yn(){return(Yn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,Ut({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Z(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ut({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Kn(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new _n;var c=o*i/(i-u)-o,p=c*qn/(qn-Un),l=p-c,d=z(a.assetOut,Number(u))/z(a.assetIn,Number(p)),f=z(a.assetOut,Number(i))/z(a.assetIn,Number(o)),m=Y({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m}}function Hn(t){return Zn.apply(this,arguments)}function Zn(){return(Zn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,Ut({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Z(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ut({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(){return($n=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Xn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Xn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Xn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Xn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,Jn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Hn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:et(s),fees:tt(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Xn||(Xn={}));var tr,er={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?zn({pool:e,reserves:n,assetIn:r,decimals:a}):Kn({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:zn,getFixedOutputSwapQuote:Kn,generateTxns:function(t){return Wn.apply(this,arguments)},signTxns:function(t){return Gn.apply(this,arguments)},execute:function(t){return $n.apply(this,arguments)},executeFixedOutputSwap:Hn},nr=4*t.ALGORAND_MIN_TX_FEE;function rr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof _n})))throw new _n;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function ar(){return ar=c(i().mark((function t(e){var n,r,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,u=s.map((function(t){return new Promise(function(){var e=c(i().mark((function e(s,u){var c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:er.getFixedInputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Fn.getFixedInputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),u(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,rr(u);case 4:return p=t.sent,t.abrupt("return",ir(p));case 6:case"end":return t.stop()}}),t)}))),ar.apply(this,arguments)}function sr(){return sr=c(i().mark((function t(e){var n,r,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,u=s.map((function(t){return new Promise(function(){var e=c(i().mark((function e(s,u){var c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:er.getFixedOutputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Fn.getFixedOutputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),u(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,rr(u);case 4:return p=t.sent,t.abrupt("return",ir(p));case 6:case"end":return t.stop()}}),t)}))),sr.apply(this,arguments)}function or(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:kt(t.route)}function ir(t){var e,n=t[0],r=or(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=or(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}var ur=(f(tr={},S.V1_1,er),f(tr,S.V2,Fn),f(tr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return se(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return ar.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return sr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(tr,"generateTxns",(function(t){return t.isUsingSwapRouter||t.pool.contractVersion!==S.V1_1?Fn.generateTxns(t):er.generateTxns(t)})),f(tr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&function(t){if(t.type===exports.SwapQuoteType.Direct)return t.quoteWithPool.pool.contractVersion;return S.V2}(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return er.signTxns(o({pool:e},t))}return Fn.signTxns(t)})),f(tr,"execute",(function(t){return t.contractVersion===S.V1_1?er.execute(t):Fn.execute(t)})),tr);function cr(){return(cr=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,pr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,Z(n,[o]);case 7:return u=t.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:tt(a),confirmedRound:d,txnID:l,groupID:et(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function pr(t){return lr.apply(this,arguments)}function lr(){return(lr=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,u=v(o,1),c=u[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dr(){return dr=c(i().mark((function t(e){var n,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:tt(e),groupID:et(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return u=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(i().mark((function a(s,o){var c,p,l,d,f,m;return i().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?u[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,Z(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),dr.apply(this,arguments)}function fr(){return(fr=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,u=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[rt("redeem")],note:st.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(u),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(u),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[rt("commit"),u],note:B([rt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function gr(){return(gr=c(i().mark((function e(n){var r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=mr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[rt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=lt,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=mn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Fe,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=dt,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Qn,exports.Swap=ur,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=xn,exports.V2AddLiquidityTxnIndices=je,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=W,exports.calculateAccountMinimumRequiredBalance=Vt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=ce,exports.prepareCommitTransactions=function(t){return gr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return dr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return cr.apply(this,arguments)},exports.sendAndWaitRawTransaction=Z,exports.sumUpTxnFees=tt,exports.tinymanContract_v1_1=Rt,exports.tinymanContract_v2=yt,exports.tinymanJSSDKConfig=st; diff --git a/src/swap/utils.ts b/src/swap/utils.ts index 77ed2982..d688bb19 100644 --- a/src/swap/utils.ts +++ b/src/swap/utils.ts @@ -72,7 +72,7 @@ function validateQuotes(promises: Promise[]): Promise { export async function getFixedInputSwapQuote( params: GetSwapQuoteBySwapTypeParams ): Promise { - const {amount, assetIn, assetOut, pools} = params; + const {amount, assetIn, assetOut, pools, isSwapRouterEnabled} = params; const quotePromises = pools.map>((pool) => { return new Promise(async (resolve, reject) => { @@ -82,7 +82,8 @@ export async function getFixedInputSwapQuote( pool: pool.info, assetIn: {amount, id: Number(assetIn.id)}, decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, - reserves: pool.reserves + reserves: pool.reserves, + isSwapRouterEnabled }; try { @@ -117,7 +118,7 @@ export async function getFixedInputSwapQuote( export async function getFixedOutputSwapQuote( params: GetSwapQuoteBySwapTypeParams ): Promise { - const {amount, assetIn, assetOut, pools} = params; + const {amount, assetIn, assetOut, pools, isSwapRouterEnabled} = params; const quotePromises = pools.map>((pool) => { return new Promise(async (resolve, reject) => { @@ -126,7 +127,8 @@ export async function getFixedOutputSwapQuote( pool: pool.info, assetOut: {amount, id: Number(assetOut.id)}, decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, - reserves: pool.reserves + reserves: pool.reserves, + isSwapRouterEnabled }; try { From e99d69326cfd8dd08f8ceafe7ffb3b65cdcc11ab Mon Sep 17 00:00:00 2001 From: anlerkan Date: Tue, 28 Feb 2023 04:18:51 +0300 Subject: [PATCH 04/39] fix(swap/util): remove unnecessary parameter from signTxns --- dist/swap/utils.d.ts | 1 - src/swap/utils.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/dist/swap/utils.d.ts b/dist/swap/utils.d.ts index dbf9fb23..c9d583c1 100644 --- a/dist/swap/utils.d.ts +++ b/dist/swap/utils.d.ts @@ -25,7 +25,6 @@ export declare function signTxns(params: { quote: SwapQuote; txGroup: SignerTransaction[]; initiatorSigner: InitiatorSigner; - network: SupportedNetwork; }): Promise; interface ExecuteCommonParams { swapType: SwapType; diff --git a/src/swap/utils.ts b/src/swap/utils.ts index d688bb19..e07b07d4 100644 --- a/src/swap/utils.ts +++ b/src/swap/utils.ts @@ -206,7 +206,6 @@ export function signTxns(params: { quote: SwapQuote; txGroup: SignerTransaction[]; initiatorSigner: InitiatorSigner; - network: SupportedNetwork; }): Promise { if ( params.quote.type === SwapQuoteType.Direct && From 20db48fee1a74b47dba80a4abfce36d517322090 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Tue, 28 Feb 2023 14:01:19 +0300 Subject: [PATCH 05/39] chore: add price_impact field --- dist/index.js | 2 +- dist/swap/types.d.ts | 6 ++-- dist/swap/v2/router/swap-router.d.ts | 4 +-- src/swap/types.ts | 6 ++-- src/swap/v2/index.ts | 12 ++++---- src/swap/v2/router/swap-router.ts | 43 +++++++++++++--------------- 6 files changed, 35 insertions(+), 38 deletions(-) diff --git a/dist/index.js b/dist/index.js index 0bc4e2c2..f14df2b9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),a=Uint8Array.from([1]);function s(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function u(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){u(s,r,a,o,i,"next",t)}function i(t){u(s,r,a,o,i,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={type:"logicsig",logic:{bytecode:"BoAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgQBbNQA0ADEYEkQxGYEBEkSBAUM="},name:"pool_logicsig"},O={type:"app",global_state_schema:{num_uints:0,num_byte_slices:3},local_state_schema:{num_uints:12,num_byte_slices:2},name:"validator_app"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function z(t,e){var n=Number(t);return Y({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function J(t,e){return Y({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function Y(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(H(e),2),s=a[0],o=a[1],i=v(H(Math.round(Number(K(s,o+r)))),2),u=i[0],c=i[1];return Number(K(u,c-r))}return Math.round(e)}function K(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function H(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(i().mark((function t(e,n){var r,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return u=t.sent,c=u.txId,t.next=13,q(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function tt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function et(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function nt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function rt(t){return(new TextEncoder).encode(t)}var at,st=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return rt("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),ot=(f(at={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(at,S.V2,{testnet:148607e3,mainnet:1002541853}),at);function it(t,e){var n=ot[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ut(){return(ut=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:it(a,s),note:st.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ct(){return(ct=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:it(a,s),note:st.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),lt={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},dt={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"};function ft(){return(ft=c(i().mark((function t(e){var n,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.assetID,s=e.initiatorAddr,t.prev=1,t.next=4,n.getTransactionParams().do();case 4:return o=t.sent,u=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:s,assetIndex:a,amount:0,suggestedParams:o}),t.abrupt("return",[{txn:u,signers:[s]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function mt(t,e){var n=Number(t.id),r=Number(e.id);return n>r?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function gt(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function xt(t){return 0===Number(t)}var It,At,ht,Tt,yt=new(function(e){m(r,pt);var n=y(r);function r(t,e){var a;return p(this,r),(a=n.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,a}return d(r,[{key:"generateLogicSigAccountForPool",value:function(e){if(e.asset1ID===e.asset2ID)throw new Error("Assets are the same");var n=it(e.network,S.V2),r=v(gt(e.asset1ID,e.asset2ID),2),a=r[0],s=r[1],o={bytes:Array.from(Buffer.from(this.poolLogicSigContractTemplate,"base64")),validatorAppId:Array.from(t.encodeUint64(n)),asset1ID:Array.from(t.encodeUint64(a)),asset2ID:Array.from(t.encodeUint64(s))},i=[].concat(_(o.bytes.slice(0,3)),_(o.validatorAppId.slice(0,8)),_(o.asset1ID.slice(0,8)),_(o.asset2ID.slice(0,8)),_(o.bytes.slice(27)));return new t.LogicSigAccount(new Uint8Array(i))}}]),r}())(O,E);exports.SwapType=void 0,(It=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",It.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(Tt||(Tt={}));var vt=(f(At={},exports.SwapType.FixedInput,1),f(At,exports.SwapType.FixedOutput,2),At),_t=rt("swap"),wt=(f(ht={},exports.SwapType.FixedInput,rt("fixed-input")),f(ht,exports.SwapType.FixedOutput,rt("fixed-output")),ht),bt={ASSET_OPT_IN:rt("asset_opt_in")};function Dt(){return(Dt=c(i().mark((function e(n){var a,s,o,u,c,p,l,d;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,u=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s,appArgs:[bt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),console.log(p.fee,"router app opt-in fee"),d=r.default.assignGroupID([p]),e.abrupt("return",[{txn:d[0],signers:[u]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function St(t){return Et.apply(this,arguments)}function Et(){return(Et=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.assetIn,o=n.assetOut,u=n.client,c=n.intermediaryAssetID,p=n.routerAppID,l=n.network,d=n.swapType,e.next=3,u.getTransactionParams().do();case 3:return f=e.sent,m=yt.generateLogicSigAccountForPool({asset1ID:s.id,asset2ID:c,network:l}),g=m.address(),x=yt.generateLogicSigAccountForPool({asset1ID:c,asset2ID:o.id,network:l}),I=x.address(),A=xt(s.id),h=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(p),amount:s.amount,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(p),amount:s.amount,assetIndex:s.id,suggestedParams:f}),T=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:p,appArgs:[_t,wt[d],r.default.encodeUint64(o.amount)],foreignApps:[it(l,S.V2)],foreignAssets:[s.id,c,o.id],accounts:[g,I],suggestedParams:f}),y=d===exports.SwapType.FixedInput?7:8,T.fee=t.ALGORAND_MIN_TX_FEE*(y+1),v=r.default.assignGroupID([h,T]),e.abrupt("return",[{txn:v[Tt.INPUT_TXN],signers:[a]},{txn:v[Tt.APP_CALL_TXN],signers:[a]}]);case 15:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ot(){return(Ot=c(i().mark((function e(n){var r,a,s,o,u,c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nt(t){return Pt.apply(this,arguments)}function Pt(){return(Pt=c(i().mark((function t(e){var n,r,a,s,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.next=4,fetch("https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});case 4:if(200===(u=t.sent).status){t.next=7;break}throw new Error("Failed to fetch swap route quotes");case 7:return t.next=9,u.json();case 9:return c=t.sent,p=c.route,t.abrupt("return",p);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function kt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return z(r.asset.decimals,Number(r.amount))/z(n.asset.decimals,Number(n.amount))}var Lt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Ft={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Rt=new(function(n){m(a,pt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=it(n.network,S.V1_1),a=v(gt(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(i().mark((function t(a,s){var u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:u=t.sent,a(o(o({},u),{},{minimum_required_balance:Vt(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Ct(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?R({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Vt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Xt,jt,Qt=rt("e");function Ut(t){return qt.apply(this,arguments)}function qt(){return(qt=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,u=a.accountAddr,n.next=3,s.accountInformation(u).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=R({stateArray:A}),T=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Gt(){return(Gt=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,u=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==u})),d=[],l&&l["key-value"])for(f=R({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mJt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Jt,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Zt(){return Zt=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d=arguments;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,Mt(n,r);case 6:return o=t.sent,u=Ct(o,it(a,S.V1_1)),c=null,u&&(l=o["created-assets"][0],p=l.index,c={asset1ID:u[Wt[S.V1_1].asset1],asset2ID:u[Wt[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Zt.apply(this,arguments)}var $t=Object.freeze({__proto__:null,getPoolInfo:Yt,getPoolReserves:function(t,e){return Ht.apply(this,arguments)},getPoolAssets:function(t){return Zt.apply(this,arguments)}});function te(t){return ee.apply(this,arguments)}function ee(){return(ee=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Bt(S.V2),u=o.generateLogicSigAccountForPool(e),c=it(r,S.V2),p=u.address(),l=gt(a,s),t.next=8,Mt(n,p);case 8:return d=t.sent,f=Ct(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[Wt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Wt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Wt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Wt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Wt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Wt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Wt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Wt.v2.totalFeeShare]),m.poolTokenID=Number(f[Wt.v2.poolTokenID]),m.asset1ID=Number(f[Wt.v2.asset1]),m.asset2ID=Number(f[Wt.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ne(){return(ne=c(i().mark((function t(e,n){var r,a,s;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Mt(e,n.account.address());case 2:return r=t.sent,a=Ct(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[Wt.v2.asset1Reserves]),s.asset2=BigInt(a[Wt.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[Wt.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function re(){return(re=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,Mt(n,r);case 3:return s=t.sent,o=Ct(s,it(a,S.V2)),u=null,o&&(u={asset1ID:o[Wt[S.V2].asset1],asset2ID:o[Wt[S.V2].asset2],poolTokenID:o[Wt[S.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ae=Object.freeze({__proto__:null,getPoolInfo:te,getPoolReserves:function(t,e){return ne.apply(this,arguments)},getPoolAssets:function(t){return re.apply(this,arguments)}});function se(t){return Boolean(t&&!(t.asset1+t.asset2))}var oe,ie,ue=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=se(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=z(t.asset1,e.asset1)/z(t.asset2,e.asset2)),r},isPoolEmpty:se,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Yt(t),te(t)])}}),ce=o((f(oe={},S.V1_1,o(o({},$t),ue)),f(oe,S.V2,o(o({},ae),ue)),oe),ue);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(ie||(ie={}));var pe=96e4,le=859e3;function de(){return(de=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,O,N;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,u=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=mt(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=xt(x),h=it(a,S.V1_1),T=Rt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:st.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[rt("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:dt.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:fe(A),suggestedParams:c}),(E=[])[ie.FUNDING_TXN]=D,E[ie.VALIDATOR_APP_CALL]=_,E[ie.POOL_TOKEN_CREATE]=w,E[ie.ASSET1_OPT_IN]=b,A||(E[ie.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),O=r.default.assignGroupID(E),N=[{txn:O[ie.FUNDING_TXN],signers:[u]},{txn:O[ie.VALIDATOR_APP_CALL],signers:[y]},{txn:O[ie.POOL_TOKEN_CREATE],signers:[y]},{txn:O[ie.ASSET1_OPT_IN],signers:[y]}],O[ie.ASSET2_OPT_IN]&&N.push({txn:O[ie.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",N);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(t){return t?pe:le}function me(){return(me=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=gt(o,u),f=v(d,2),m=f[0],g=f[1],x=Rt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===ie.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ge(t){return xe.apply(this,arguments)}function xe(){return(xe=c(i().mark((function t(e){var n,r,a,s,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,q(n,a[ie.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Ie(){return(Ie=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,ge({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",ce.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ae,he={generateTxns:function(t){return de.apply(this,arguments)},signTxns:function(t){return me.apply(this,arguments)},execute:function(t){return Ie.apply(this,arguments)},getBootstrapFundingTxnAmount:fe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ae||(Ae={}));var Te=5,ye=6;function ve(t,e){return _e.apply(this,arguments)}function _e(){return(_e=c(i().mark((function e(n,r){var a,s,o;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function we(t,e){return be.apply(this,arguments)}function be(){return(be=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function De(){return(De=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,u=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=it(s,S.V2),d=t.getApplicationAddress(l),f=mt(o,u),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,ce.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(u.unit_name," already exists"));case 12:return I=yt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=xt(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[rt("bootstrap")],note:st.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ee(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Se(h),suggestedParams:p}),(_=[])[Ae.FUNDING_TXN]=y,_[Ae.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Ae.FUNDING_TXN]={txn:w[Ae.FUNDING_TXN],signers:[c]},b[Ae.VALIDATOR_APP_CALL]={txn:w[Ae.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Se(t){return function(t,e){var n=Bt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ee(t)+1e5}function Ee(e){return((e?Te:ye)+1)*t.ALGORAND_MIN_TX_FEE}function Oe(){return(Oe=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=gt(o,u),f=v(d,2),m=f[0],g=f[1],x=yt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ae.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ne(){return(Ne=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,ve(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Wt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",ce.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Pe,ke={generateTxns:function(t){return De.apply(this,arguments)},signTxns:function(t){return Oe.apply(this,arguments)},execute:function(t){return Ne.apply(this,arguments)},getBootstrapFundingTxnAmount:Se,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Se(e)}};var Le,Fe=(f(Pe={},S.V1_1,he),f(Pe,S.V2,ke),f(Pe,"generateTxns",(function(t){return t.contractVersion===S.V1_1?he.generateTxns(t):ke.generateTxns(t)})),f(Pe,"signTxns",(function(t){return t.contractVersion===S.V1_1?he.signTxns(t):ke.signTxns(t)})),f(Pe,"execute",(function(t){return t.contractVersion===S.V1_1?he.execute(t):ke.execute(t)})),f(Pe,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?he.getBootstrapFundingTxnAmount(n):ke.getBootstrapFundingTxnAmount(n)})),Pe);exports.V1_1AddLiquidityTxnIndices=void 0,(Le=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Le.FEE_TXN=0]="FEE_TXN",Le[Le.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Le[Le.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Le[Le.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Le[Le.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Re,Be,Me,Ce,Ve=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Ce=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Ce.FLEXIBLE="flexible",Ce.INITIAL="initial";var Xe,je=(f(Re={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Re,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Re,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Re),Qe=(f(Be={},exports.V2AddLiquidityType.INITIAL,1),f(Be,exports.V2AddLiquidityType.SINGLE,2),f(Be,exports.V2AddLiquidityType.FLEXIBLE,2),Be),Ue=(f(Me={},exports.V2AddLiquidityType.INITIAL,3),f(Me,exports.V2AddLiquidityType.FLEXIBLE,3),f(Me,exports.V2AddLiquidityType.SINGLE,2),Me);function qe(t){var e=t.assetIn,n=t.assetOut;return z(n.decimals,Number(n.amount))/z(e.decimals,Number(e.amount))}function Ge(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=qe({assetIn:r,assetOut:a}),o=z(a.decimals,Number(n))/z(r.decimals,Number(e));return Y({decimalPlaces:5},Math.abs(s/o-1))}function We(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,i=t.asset1Amount,u=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(i),d=s.asset2+BigInt(u),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(i)-x,h=BigInt(u)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=ze(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=ze(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:Ge({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function ze(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Je(e){return(Qe[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ye=rt("add_liquidity"),Ke=(f(Xe={},S.V1_1,[rt("mint")]),f(Xe,S.V2,{INITIAL_LIQUIDITY:[rt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ye,rt("single")],FLEXIBLE_MODE:[Ye,rt("flexible")]}),Xe);function He(){return(He=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,u=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=W("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:it(s,S.V1_1),appArgs:Ke.v1_1,accounts:[d],note:st.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[u.id,p.id]:[u.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ze(){return(Ze=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $e(){return($e=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,u=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Ut({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,Z(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=tt(a),x=et(a),t.next=17,Ut({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var tn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function wn(){return(wn=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!e.isUsingSwapRouter){t.next=2;break}return t.abrupt("return",St(e));case 2:if(n=e.assetIn,a=e.assetOut,s=e.client,o=e.initiatorAddr,u=e.pool,c=e.slippage,p=e.swapType,l=u.account.address(),(d=[u.asset1ID,u.asset2ID]).includes(n.id)&&d.includes(a.id)&&n.id!==a.id){t.next=7;break}throw new F({pool:u,assetIn:n,assetOut:a},"Input asset (#".concat(n.id,") and output asset (#").concat(a.id,") provided to generate transactions do not belong to the pool ").concat(l,"."));case 7:return t.next=9,s.getTransactionParams().do();case 9:return f=t.sent,m=xt(n.id),g=p===exports.SwapType.FixedInput?n.amount:W("positive",c,n.amount),x=p===exports.SwapType.FixedOutput?a.amount:W("negative",c,a.amount),I=m?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,assetIndex:n.id,suggestedParams:f}),(A=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:u.validatorAppID,appArgs:[_t,wt[p],r.default.encodeUint64(x)],note:st.getAppCallTxnNoteWithClientName(S.V2),accounts:[l],foreignAssets:[u.asset1ID,u.asset2ID],suggestedParams:f})).fee=bn(p),(h=[])[Tt.INPUT_TXN]=I,h[Tt.APP_CALL_TXN]=A,T=r.default.assignGroupID(h),t.abrupt("return",[{txn:T[Tt.INPUT_TXN],signers:[o]},{txn:T[Tt.APP_CALL_TXN],signers:[o]}]);case 21:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function bn(e){return(vt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Dn(){return(Dn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,u=e.signedTxns,c=e.assetIn,t.next=3,Z(a,[u]);case 3:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=10,we(a,o);case 10:return g=t.sent,x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.route[s.route.length-1].quote.amount_out.asset.id),I=null==g||null===(n=g.find((function(t){return t.txn.txn.xaid===c.id})))||void 0===n?void 0:n.txn.txn,A=null==g||null===(r=g.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:f,assetIn:I&&{amount:BigInt(c.amount)-BigInt(I.aamt||0),id:c.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:m});case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Sn(){return(Sn=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.asset,r=e.decimals,a=e.pool,s=e.type,o=e.isSwapRouterEnabled,s!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,En({pool:a,assetIn:n,decimals:r,isSwapRouterEnabled:o});case 4:u=t.sent,t.next=10;break;case 7:return t.next=9,Nn({pool:a,assetOut:n,decimals:r,isSwapRouterEnabled:o});case 9:u=t.sent;case 10:return t.abrupt("return",u);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(t){return On.apply(this,arguments)}function On(){return(On=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetIn,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:n,assetIn:r},"Trying to swap on a non-existent pool");case 3:if(o=BigInt(r.amount),u=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}c=n.asset2ID,p=n.asset1Reserves,l=n.asset2Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}c=n.asset1ID,p=n.asset2Reserves,l=n.asset1Reserves,t.next=18;break;case 17:throw new F({pool:n,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(d=kn({inputSupply:p,outputSupply:l,swapInputAmount:o,totalFeeShare:u,decimals:a}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact,!(f>l)){t.next=21;break}throw new _n;case 21:if(x={assetInID:r.id,assetInAmount:o,assetOutID:c,assetOutAmount:f,swapFee:Number(m),rate:z(a.assetOut,Number(f))/z(a.assetIn,Number(o)),priceImpact:g},!s){t.next=28;break}return t.next=25,Nt({amount:r.amount,assetInID:Number(r.id),assetOutID:c,swapType:exports.SwapType.FixedInput});case 25:if(!((I=t.sent).length>1&&BigInt(I[I.length-1].quote.amount_out.amount)>x.assetOutAmount)){t.next=28;break}return t.abrupt("return",{route:I,type:exports.SwapQuoteType.Router});case 28:return t.abrupt("return",{quoteWithPool:{pool:n,quote:x},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nn(t){return Pn.apply(this,arguments)}function Pn(){return(Pn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetOut,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:n,assetOut:r},"Trying to swap on a non-existent pool");case 3:if(o=BigInt(r.amount),u=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}c=n.asset2ID,p=n.asset2Reserves,l=n.asset1Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}c=n.asset1ID,p=n.asset1Reserves,l=n.asset2Reserves,t.next=18;break;case 17:throw new F({pool:n,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(d=Ln({inputSupply:p,outputSupply:l,swapOutputAmount:o,totalFeeShare:u,decimals:a}),f=d.swapInputAmount,m=d.totalFeeAmount,g=d.priceImpact,!(o>l)){t.next=21;break}throw new _n;case 21:if(x={assetInID:c,assetInAmount:f,assetOutID:r.id,assetOutAmount:o,swapFee:Number(m),rate:z(a.assetOut,Number(o))/z(a.assetIn,Number(f)),priceImpact:g},!s){t.next=28;break}return t.next=25,Nt({amount:r.amount,assetInID:c,assetOutID:r.id,swapType:exports.SwapType.FixedInput});case 25:if(!((I=t.sent).length>1&&BigInt(I[I.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Bn(){return(Bn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,u=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s.validatorAppID,note:st.getAppCallTxnNoteWithClientName(S.V2),appArgs:[In,r.default.encodeUint64(W("negative",l,c)),r.default.encodeUint64(W("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[gn.ASSET_TRANSFER_TXN]=g,I[gn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[gn.ASSET_TRANSFER_TXN],signers:[u]},{txn:A[gn.APP_CALL_TXN],signers:[u]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,u=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=W("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:u,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:st.getAppCallTxnNoteWithClientName(S.V2),appArgs:[In,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[gn.ASSET_TRANSFER_TXN]=T,v[gn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[gn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[gn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Cn(){return(Cn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,Z(r,[s]);case 3:return o=t.sent,u=v(o,1),c=u[0].txnID,t.next=8,we(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Vn,Xn,jn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Rn(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=Rn(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Fn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Fn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Bn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Mn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Cn.apply(this,arguments)}},Qn=(f(Vn={},S.V1_1,vn),f(Vn,S.V2,jn),Vn),Un=3n,qn=1000n;function Gn(){return(Gn=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,u=v(o,2),c=u[0],p=u[1],l=a.map((function(t,e){return e===Xn.FEE_TXN_INDEX?c:e===Xn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Wn(){return(Wn=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,u=e.assetIn,c=e.assetOut,p=e.slippage,l=e.initiatorAddr,d=s.account.address(),(f=[s.asset1ID,s.asset2ID]).includes(u.id)&&f.includes(c.id)&&u.id!==c.id){t.next=5;break}throw new F({pool:s,assetIn:u,assetOut:c},"Input asset (#".concat(u.id,") and output asset (#").concat(c.id,") provided to generate transactions do not belong to the pool ").concat(d,"."));case 5:return t.next=7,n.getTransactionParams().do();case 7:return m=t.sent,g=[rt("swap"),o===exports.SwapType.FixedInput?rt("fi"):rt("fo")],x=r.default.makeApplicationNoOpTxnFromObject({from:d,appIndex:s.validatorAppID,appArgs:g,accounts:[l],note:st.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:m}),I=o===exports.SwapType.FixedOutput?W("positive",p,u.amount):u.amount,A=0===u.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:u.id,amount:I,suggestedParams:m}),h=o===exports.SwapType.FixedInput?W("negative",p,c.amount):c.amount,T=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:l,amount:h,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:l,assetIndex:c.id,amount:h,suggestedParams:m}),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.fee+T.fee,note:a,suggestedParams:m}),v=r.default.assignGroupID([y,x,A,T]),t.abrupt("return",[{txn:v[0],signers:[l]},{txn:v[1],signers:[d]},{txn:v[2],signers:[l]},{txn:v[3],signers:[d]}]);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function zn(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,i,u,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*Un/qn,l=u-i*u/(i+(c-p));if(l>u)throw new _n;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:qe(d),priceImpact:Ge(o({inputSupply:i,outputSupply:u},d))}}function Jn(t){return Yn.apply(this,arguments)}function Yn(){return(Yn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,Ut({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Z(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ut({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Kn(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new _n;var c=o*i/(i-u)-o,p=c*qn/(qn-Un),l=p-c,d=z(a.assetOut,Number(u))/z(a.assetIn,Number(p)),f=z(a.assetOut,Number(i))/z(a.assetIn,Number(o)),m=Y({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m}}function Hn(t){return Zn.apply(this,arguments)}function Zn(){return(Zn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,Ut({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Z(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ut({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(){return($n=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Xn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Xn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Xn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Xn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,Jn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Hn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:et(s),fees:tt(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Xn||(Xn={}));var tr,er={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?zn({pool:e,reserves:n,assetIn:r,decimals:a}):Kn({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:zn,getFixedOutputSwapQuote:Kn,generateTxns:function(t){return Wn.apply(this,arguments)},signTxns:function(t){return Gn.apply(this,arguments)},execute:function(t){return $n.apply(this,arguments)},executeFixedOutputSwap:Hn},nr=4*t.ALGORAND_MIN_TX_FEE;function rr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof _n})))throw new _n;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function ar(){return ar=c(i().mark((function t(e){var n,r,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,u=s.map((function(t){return new Promise(function(){var e=c(i().mark((function e(s,u){var c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:er.getFixedInputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Fn.getFixedInputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),u(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,rr(u);case 4:return p=t.sent,t.abrupt("return",ir(p));case 6:case"end":return t.stop()}}),t)}))),ar.apply(this,arguments)}function sr(){return sr=c(i().mark((function t(e){var n,r,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,u=s.map((function(t){return new Promise(function(){var e=c(i().mark((function e(s,u){var c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:er.getFixedOutputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Fn.getFixedOutputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),u(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,rr(u);case 4:return p=t.sent,t.abrupt("return",ir(p));case 6:case"end":return t.stop()}}),t)}))),sr.apply(this,arguments)}function or(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:kt(t.route)}function ir(t){var e,n=t[0],r=or(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=or(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}var ur=(f(tr={},S.V1_1,er),f(tr,S.V2,Fn),f(tr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return se(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return ar.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return sr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(tr,"generateTxns",(function(t){return t.isUsingSwapRouter||t.pool.contractVersion!==S.V1_1?Fn.generateTxns(t):er.generateTxns(t)})),f(tr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&function(t){if(t.type===exports.SwapQuoteType.Direct)return t.quoteWithPool.pool.contractVersion;return S.V2}(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return er.signTxns(o({pool:e},t))}return Fn.signTxns(t)})),f(tr,"execute",(function(t){return t.contractVersion===S.V1_1?er.execute(t):Fn.execute(t)})),tr);function cr(){return(cr=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,pr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,Z(n,[o]);case 7:return u=t.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:tt(a),confirmedRound:d,txnID:l,groupID:et(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function pr(t){return lr.apply(this,arguments)}function lr(){return(lr=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,u=v(o,1),c=u[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dr(){return dr=c(i().mark((function t(e){var n,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:tt(e),groupID:et(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return u=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(i().mark((function a(s,o){var c,p,l,d,f,m;return i().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?u[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,Z(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),dr.apply(this,arguments)}function fr(){return(fr=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,u=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[rt("redeem")],note:st.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(u),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(u),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[rt("commit"),u],note:B([rt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function gr(){return(gr=c(i().mark((function e(n){var r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=mr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[rt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=lt,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=mn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Fe,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=dt,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Qn,exports.Swap=ur,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=xn,exports.V2AddLiquidityTxnIndices=je,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=W,exports.calculateAccountMinimumRequiredBalance=Vt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=ce,exports.prepareCommitTransactions=function(t){return gr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return dr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return cr.apply(this,arguments)},exports.sendAndWaitRawTransaction=Z,exports.sumUpTxnFees=tt,exports.tinymanContract_v1_1=Rt,exports.tinymanContract_v2=yt,exports.tinymanJSSDKConfig=st; +i=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",s=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function u(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,s=Object.create(a.prototype),o=new w(r||[]);return s._invoke=function(t,e,n){var r="suspendedStart";return function(a,s){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw s;return D()}for(n.method=a,n.arg=s;;){var o=n.delegate;if(o){var i=y(o,n);if(i){if(i===l)continue;return i}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var u=p(t,e,n);if("normal"===u.type){if(r=n.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:n.done}}"throw"===u.type&&(r="completed",n.method="throw",n.arg=u.arg)}}}(t,n,o),s}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};u(g,a,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,a)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,s,o,i){var u=p(t[a],t,s);if("throw"!==u.type){var c=u.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,i)}),(function(t){r("throw",t,o,i)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,i)}))}i(u.arg)}var a;this._invoke=function(t,n){function s(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(s,s):s()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function w(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,s=function e(){for(;++r=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function u(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){u(s,r,a,o,i,"next",t)}function i(t){u(s,r,a,o,i,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={type:"logicsig",logic:{bytecode:"BoAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgQBbNQA0ADEYEkQxGYEBEkSBAUM="},name:"pool_logicsig"},O={type:"app",global_state_schema:{num_uints:0,num_byte_slices:3},local_state_schema:{num_uints:12,num_byte_slices:2},name:"validator_app"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function z(t,e){var n=Number(t);return Y({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function J(t,e){return Y({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function Y(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(H(e),2),s=a[0],o=a[1],i=v(H(Math.round(Number(K(s,o+r)))),2),u=i[0],c=i[1];return Number(K(u,c-r))}return Math.round(e)}function K(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function H(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(i().mark((function t(e,n){var r,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return u=t.sent,c=u.txId,t.next=13,q(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function tt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function et(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function nt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function rt(t){return(new TextEncoder).encode(t)}var at,st=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return rt("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),ot=(f(at={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(at,S.V2,{testnet:148607e3,mainnet:1002541853}),at);function it(t,e){var n=ot[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ut(){return(ut=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:it(a,s),note:st.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ct(){return(ct=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:it(a,s),note:st.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),lt={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},dt={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"};function ft(){return(ft=c(i().mark((function t(e){var n,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.assetID,s=e.initiatorAddr,t.prev=1,t.next=4,n.getTransactionParams().do();case 4:return o=t.sent,u=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:s,assetIndex:a,amount:0,suggestedParams:o}),t.abrupt("return",[{txn:u,signers:[s]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function mt(t,e){var n=Number(t.id),r=Number(e.id);return n>r?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function gt(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function xt(t){return 0===Number(t)}var It,At,ht,Tt,yt=new(function(e){m(r,pt);var n=y(r);function r(t,e){var a;return p(this,r),(a=n.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,a}return d(r,[{key:"generateLogicSigAccountForPool",value:function(e){if(e.asset1ID===e.asset2ID)throw new Error("Assets are the same");var n=it(e.network,S.V2),r=v(gt(e.asset1ID,e.asset2ID),2),a=r[0],s=r[1],o={bytes:Array.from(Buffer.from(this.poolLogicSigContractTemplate,"base64")),validatorAppId:Array.from(t.encodeUint64(n)),asset1ID:Array.from(t.encodeUint64(a)),asset2ID:Array.from(t.encodeUint64(s))},i=[].concat(_(o.bytes.slice(0,3)),_(o.validatorAppId.slice(0,8)),_(o.asset1ID.slice(0,8)),_(o.asset2ID.slice(0,8)),_(o.bytes.slice(27)));return new t.LogicSigAccount(new Uint8Array(i))}}]),r}())(O,E);exports.SwapType=void 0,(It=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",It.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(Tt||(Tt={}));var vt=(f(At={},exports.SwapType.FixedInput,1),f(At,exports.SwapType.FixedOutput,2),At),_t=rt("swap"),wt=(f(ht={},exports.SwapType.FixedInput,rt("fixed-input")),f(ht,exports.SwapType.FixedOutput,rt("fixed-output")),ht),bt={ASSET_OPT_IN:rt("asset_opt_in")};function Dt(){return(Dt=c(i().mark((function e(n){var a,s,o,u,c,p,l,d;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,u=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s,appArgs:[bt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),d=r.default.assignGroupID([p]),e.abrupt("return",[{txn:d[0],signers:[u]}]);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function St(t){return Et.apply(this,arguments)}function Et(){return(Et=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.assetIn,o=n.assetOut,u=n.client,c=n.intermediaryAssetID,p=n.routerAppID,l=n.network,d=n.swapType,e.next=3,u.getTransactionParams().do();case 3:return f=e.sent,m=yt.generateLogicSigAccountForPool({asset1ID:s.id,asset2ID:c,network:l}),g=m.address(),x=yt.generateLogicSigAccountForPool({asset1ID:c,asset2ID:o.id,network:l}),I=x.address(),A=xt(s.id),h=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(p),amount:s.amount,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(p),amount:s.amount,assetIndex:s.id,suggestedParams:f}),T=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:p,appArgs:[_t,wt[d],r.default.encodeUint64(o.amount)],foreignApps:[it(l,S.V2)],foreignAssets:[s.id,c,o.id],accounts:[g,I],suggestedParams:f}),y=d===exports.SwapType.FixedInput?7:8,T.fee=t.ALGORAND_MIN_TX_FEE*(y+1),v=r.default.assignGroupID([h,T]),e.abrupt("return",[{txn:v[Tt.INPUT_TXN],signers:[a]},{txn:v[Tt.APP_CALL_TXN],signers:[a]}]);case 15:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ot(){return(Ot=c(i().mark((function e(n){var r,a,s,o,u,c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nt(t){return Pt.apply(this,arguments)}function Pt(){return(Pt=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});case 5:return u=t.sent,t.abrupt("return",u.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),console.log("buraya girdi",t.t0),t.t0;case 14:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}function kt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return z(r.asset.decimals,Number(r.amount))/z(n.asset.decimals,Number(n.amount))}var Lt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Ft={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Rt=new(function(n){m(a,pt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=it(n.network,S.V1_1),a=v(gt(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(i().mark((function t(a,s){var u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:u=t.sent,a(o(o({},u),{},{minimum_required_balance:Vt(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Ct(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?R({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Vt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Xt,jt,Qt=rt("e");function Ut(t){return qt.apply(this,arguments)}function qt(){return(qt=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,u=a.accountAddr,n.next=3,s.accountInformation(u).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=R({stateArray:A}),T=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Gt(){return(Gt=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,u=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==u})),d=[],l&&l["key-value"])for(f=R({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mJt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Jt,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Zt(){return Zt=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d=arguments;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,Mt(n,r);case 6:return o=t.sent,u=Ct(o,it(a,S.V1_1)),c=null,u&&(l=o["created-assets"][0],p=l.index,c={asset1ID:u[Wt[S.V1_1].asset1],asset2ID:u[Wt[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Zt.apply(this,arguments)}var $t=Object.freeze({__proto__:null,getPoolInfo:Yt,getPoolReserves:function(t,e){return Ht.apply(this,arguments)},getPoolAssets:function(t){return Zt.apply(this,arguments)}});function te(t){return ee.apply(this,arguments)}function ee(){return(ee=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Bt(S.V2),u=o.generateLogicSigAccountForPool(e),c=it(r,S.V2),p=u.address(),l=gt(a,s),t.next=8,Mt(n,p);case 8:return d=t.sent,f=Ct(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[Wt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Wt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Wt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Wt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Wt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Wt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Wt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Wt.v2.totalFeeShare]),m.poolTokenID=Number(f[Wt.v2.poolTokenID]),m.asset1ID=Number(f[Wt.v2.asset1]),m.asset2ID=Number(f[Wt.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ne(){return(ne=c(i().mark((function t(e,n){var r,a,s;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Mt(e,n.account.address());case 2:return r=t.sent,a=Ct(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[Wt.v2.asset1Reserves]),s.asset2=BigInt(a[Wt.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[Wt.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function re(){return(re=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,Mt(n,r);case 3:return s=t.sent,o=Ct(s,it(a,S.V2)),u=null,o&&(u={asset1ID:o[Wt[S.V2].asset1],asset2ID:o[Wt[S.V2].asset2],poolTokenID:o[Wt[S.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ae=Object.freeze({__proto__:null,getPoolInfo:te,getPoolReserves:function(t,e){return ne.apply(this,arguments)},getPoolAssets:function(t){return re.apply(this,arguments)}});function se(t){return Boolean(t&&!(t.asset1+t.asset2))}var oe,ie,ue=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=se(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=z(t.asset1,e.asset1)/z(t.asset2,e.asset2)),r},isPoolEmpty:se,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Yt(t),te(t)])}}),ce=o((f(oe={},S.V1_1,o(o({},$t),ue)),f(oe,S.V2,o(o({},ae),ue)),oe),ue);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(ie||(ie={}));var pe=96e4,le=859e3;function de(){return(de=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,O,N;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,u=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=mt(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=xt(x),h=it(a,S.V1_1),T=Rt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:st.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[rt("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:dt.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:fe(A),suggestedParams:c}),(E=[])[ie.FUNDING_TXN]=D,E[ie.VALIDATOR_APP_CALL]=_,E[ie.POOL_TOKEN_CREATE]=w,E[ie.ASSET1_OPT_IN]=b,A||(E[ie.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),O=r.default.assignGroupID(E),N=[{txn:O[ie.FUNDING_TXN],signers:[u]},{txn:O[ie.VALIDATOR_APP_CALL],signers:[y]},{txn:O[ie.POOL_TOKEN_CREATE],signers:[y]},{txn:O[ie.ASSET1_OPT_IN],signers:[y]}],O[ie.ASSET2_OPT_IN]&&N.push({txn:O[ie.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",N);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(t){return t?pe:le}function me(){return(me=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=gt(o,u),f=v(d,2),m=f[0],g=f[1],x=Rt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===ie.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ge(t){return xe.apply(this,arguments)}function xe(){return(xe=c(i().mark((function t(e){var n,r,a,s,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,q(n,a[ie.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Ie(){return(Ie=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,ge({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",ce.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ae,he={generateTxns:function(t){return de.apply(this,arguments)},signTxns:function(t){return me.apply(this,arguments)},execute:function(t){return Ie.apply(this,arguments)},getBootstrapFundingTxnAmount:fe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ae||(Ae={}));var Te=5,ye=6;function ve(t,e){return _e.apply(this,arguments)}function _e(){return(_e=c(i().mark((function e(n,r){var a,s,o;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function we(t,e){return be.apply(this,arguments)}function be(){return(be=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function De(){return(De=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,u=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=it(s,S.V2),d=t.getApplicationAddress(l),f=mt(o,u),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,ce.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(u.unit_name," already exists"));case 12:return I=yt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=xt(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[rt("bootstrap")],note:st.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ee(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Se(h),suggestedParams:p}),(_=[])[Ae.FUNDING_TXN]=y,_[Ae.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Ae.FUNDING_TXN]={txn:w[Ae.FUNDING_TXN],signers:[c]},b[Ae.VALIDATOR_APP_CALL]={txn:w[Ae.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Se(t){return function(t,e){var n=Bt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ee(t)+1e5}function Ee(e){return((e?Te:ye)+1)*t.ALGORAND_MIN_TX_FEE}function Oe(){return(Oe=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=gt(o,u),f=v(d,2),m=f[0],g=f[1],x=yt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ae.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ne(){return(Ne=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,ve(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Wt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",ce.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Pe,ke={generateTxns:function(t){return De.apply(this,arguments)},signTxns:function(t){return Oe.apply(this,arguments)},execute:function(t){return Ne.apply(this,arguments)},getBootstrapFundingTxnAmount:Se,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Se(e)}};var Le,Fe=(f(Pe={},S.V1_1,he),f(Pe,S.V2,ke),f(Pe,"generateTxns",(function(t){return t.contractVersion===S.V1_1?he.generateTxns(t):ke.generateTxns(t)})),f(Pe,"signTxns",(function(t){return t.contractVersion===S.V1_1?he.signTxns(t):ke.signTxns(t)})),f(Pe,"execute",(function(t){return t.contractVersion===S.V1_1?he.execute(t):ke.execute(t)})),f(Pe,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?he.getBootstrapFundingTxnAmount(n):ke.getBootstrapFundingTxnAmount(n)})),Pe);exports.V1_1AddLiquidityTxnIndices=void 0,(Le=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Le.FEE_TXN=0]="FEE_TXN",Le[Le.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Le[Le.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Le[Le.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Le[Le.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Re,Be,Me,Ce,Ve=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Ce=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Ce.FLEXIBLE="flexible",Ce.INITIAL="initial";var Xe,je=(f(Re={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Re,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Re,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Re),Qe=(f(Be={},exports.V2AddLiquidityType.INITIAL,1),f(Be,exports.V2AddLiquidityType.SINGLE,2),f(Be,exports.V2AddLiquidityType.FLEXIBLE,2),Be),Ue=(f(Me={},exports.V2AddLiquidityType.INITIAL,3),f(Me,exports.V2AddLiquidityType.FLEXIBLE,3),f(Me,exports.V2AddLiquidityType.SINGLE,2),Me);function qe(t){var e=t.assetIn,n=t.assetOut;return z(n.decimals,Number(n.amount))/z(e.decimals,Number(e.amount))}function Ge(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=qe({assetIn:r,assetOut:a}),o=z(a.decimals,Number(n))/z(r.decimals,Number(e));return Y({decimalPlaces:5},Math.abs(s/o-1))}function We(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,i=t.asset1Amount,u=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(i),d=s.asset2+BigInt(u),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(i)-x,h=BigInt(u)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=ze(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=ze(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:Ge({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function ze(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Je(e){return(Qe[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ye=rt("add_liquidity"),Ke=(f(Xe={},S.V1_1,[rt("mint")]),f(Xe,S.V2,{INITIAL_LIQUIDITY:[rt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ye,rt("single")],FLEXIBLE_MODE:[Ye,rt("flexible")]}),Xe);function He(){return(He=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,u=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=W("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:it(s,S.V1_1),appArgs:Ke.v1_1,accounts:[d],note:st.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[u.id,p.id]:[u.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ze(){return(Ze=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $e(){return($e=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,u=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Ut({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,Z(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=tt(a),x=et(a),t.next=17,Ut({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var tn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function wn(){return(wn=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!e.isUsingSwapRouter){t.next=2;break}return t.abrupt("return",St(e));case 2:if(n=e.assetIn,a=e.assetOut,s=e.client,o=e.initiatorAddr,u=e.pool,c=e.slippage,p=e.swapType,l=u.account.address(),(d=[u.asset1ID,u.asset2ID]).includes(n.id)&&d.includes(a.id)&&n.id!==a.id){t.next=7;break}throw new F({pool:u,assetIn:n,assetOut:a},"Input asset (#".concat(n.id,") and output asset (#").concat(a.id,") provided to generate transactions do not belong to the pool ").concat(l,"."));case 7:return t.next=9,s.getTransactionParams().do();case 9:return f=t.sent,m=xt(n.id),g=p===exports.SwapType.FixedInput?n.amount:W("positive",c,n.amount),x=p===exports.SwapType.FixedOutput?a.amount:W("negative",c,a.amount),I=m?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,assetIndex:n.id,suggestedParams:f}),(A=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:u.validatorAppID,appArgs:[_t,wt[p],r.default.encodeUint64(x)],note:st.getAppCallTxnNoteWithClientName(S.V2),accounts:[l],foreignAssets:[u.asset1ID,u.asset2ID],suggestedParams:f})).fee=bn(p),(h=[])[Tt.INPUT_TXN]=I,h[Tt.APP_CALL_TXN]=A,T=r.default.assignGroupID(h),t.abrupt("return",[{txn:T[Tt.INPUT_TXN],signers:[o]},{txn:T[Tt.APP_CALL_TXN],signers:[o]}]);case 21:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function bn(e){return(vt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Dn(){return(Dn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,u=e.signedTxns,c=e.assetIn,t.next=3,Z(a,[u]);case 3:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=10,we(a,o);case 10:return g=t.sent,x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.route[s.route.length-1].quote.amount_out.asset.id),I=null==g||null===(n=g.find((function(t){return t.txn.txn.xaid===c.id})))||void 0===n?void 0:n.txn.txn,A=null==g||null===(r=g.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:f,assetIn:I&&{amount:BigInt(c.amount)-BigInt(I.aamt||0),id:c.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:m});case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Sn(){return(Sn=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.asset,r=e.decimals,a=e.pool,s=e.type,o=e.isSwapRouterEnabled,s!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,En({pool:a,assetIn:n,decimals:r,isSwapRouterEnabled:o});case 4:u=t.sent,t.next=10;break;case 7:return t.next=9,Nn({pool:a,assetOut:n,decimals:r,isSwapRouterEnabled:o});case 9:u=t.sent;case 10:return t.abrupt("return",u);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(t){return On.apply(this,arguments)}function On(){return(On=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetIn,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:n,assetIn:r},"Trying to swap on a non-existent pool");case 3:if(u=BigInt(r.amount),c=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}p=n.asset2ID,l=n.asset1Reserves,d=n.asset2Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}p=n.asset1ID,l=n.asset2Reserves,d=n.asset1Reserves,t.next=18;break;case 17:throw new F({pool:n,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(f=kn({inputSupply:l,outputSupply:d,swapInputAmount:u,totalFeeShare:c,decimals:a}),m=f.swapOutputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(m>d)){t.next=21;break}throw new _n;case 21:if(I={assetInID:r.id,assetInAmount:u,assetOutID:p,assetOutAmount:m,swapFee:Number(g),rate:z(a.assetOut,Number(m))/z(a.assetIn,Number(u)),priceImpact:x},!s){t.next=28;break}return t.next=25,Nt({amount:r.amount,assetInID:Number(r.id),assetOutID:p,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_out.amount)>I.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},A),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:n,quote:I},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nn(t){return Pn.apply(this,arguments)}function Pn(){return(Pn=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetOut,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:n,assetOut:r},"Trying to swap on a non-existent pool");case 3:if(u=BigInt(r.amount),c=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}p=n.asset2ID,l=n.asset2Reserves,d=n.asset1Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}p=n.asset1ID,l=n.asset1Reserves,d=n.asset2Reserves,t.next=18;break;case 17:throw new F({pool:n,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(f=Ln({inputSupply:l,outputSupply:d,swapOutputAmount:u,totalFeeShare:c,decimals:a}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(u>d)){t.next=21;break}throw new _n;case 21:if(I={assetInID:p,assetInAmount:m,assetOutID:r.id,assetOutAmount:u,swapFee:Number(g),rate:z(a.assetOut,Number(u))/z(a.assetIn,Number(m)),priceImpact:x},!s){t.next=28;break}return t.next=25,Nt({amount:r.amount,assetInID:p,assetOutID:r.id,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Bn(){return(Bn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,u=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s.validatorAppID,note:st.getAppCallTxnNoteWithClientName(S.V2),appArgs:[In,r.default.encodeUint64(W("negative",l,c)),r.default.encodeUint64(W("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[gn.ASSET_TRANSFER_TXN]=g,I[gn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[gn.ASSET_TRANSFER_TXN],signers:[u]},{txn:A[gn.APP_CALL_TXN],signers:[u]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,u=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=W("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:u,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:st.getAppCallTxnNoteWithClientName(S.V2),appArgs:[In,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[gn.ASSET_TRANSFER_TXN]=T,v[gn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[gn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[gn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Cn(){return(Cn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,Z(r,[s]);case 3:return o=t.sent,u=v(o,1),c=u[0].txnID,t.next=8,we(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Vn,Xn,jn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Rn(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=Rn(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Fn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Fn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Bn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Mn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Cn.apply(this,arguments)}},Qn=(f(Vn={},S.V1_1,vn),f(Vn,S.V2,jn),Vn),Un=3n,qn=1000n;function Gn(){return(Gn=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,u=v(o,2),c=u[0],p=u[1],l=a.map((function(t,e){return e===Xn.FEE_TXN_INDEX?c:e===Xn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Wn(){return(Wn=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,u=e.assetIn,c=e.assetOut,p=e.slippage,l=e.initiatorAddr,d=s.account.address(),(f=[s.asset1ID,s.asset2ID]).includes(u.id)&&f.includes(c.id)&&u.id!==c.id){t.next=5;break}throw new F({pool:s,assetIn:u,assetOut:c},"Input asset (#".concat(u.id,") and output asset (#").concat(c.id,") provided to generate transactions do not belong to the pool ").concat(d,"."));case 5:return t.next=7,n.getTransactionParams().do();case 7:return m=t.sent,g=[rt("swap"),o===exports.SwapType.FixedInput?rt("fi"):rt("fo")],x=r.default.makeApplicationNoOpTxnFromObject({from:d,appIndex:s.validatorAppID,appArgs:g,accounts:[l],note:st.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:m}),I=o===exports.SwapType.FixedOutput?W("positive",p,u.amount):u.amount,A=0===u.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:u.id,amount:I,suggestedParams:m}),h=o===exports.SwapType.FixedInput?W("negative",p,c.amount):c.amount,T=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:l,amount:h,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:l,assetIndex:c.id,amount:h,suggestedParams:m}),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.fee+T.fee,note:a,suggestedParams:m}),v=r.default.assignGroupID([y,x,A,T]),t.abrupt("return",[{txn:v[0],signers:[l]},{txn:v[1],signers:[d]},{txn:v[2],signers:[l]},{txn:v[3],signers:[d]}]);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function zn(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,i,u,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*Un/qn,l=u-i*u/(i+(c-p));if(l>u)throw new _n;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:qe(d),priceImpact:Ge(o({inputSupply:i,outputSupply:u},d))}}function Jn(t){return Yn.apply(this,arguments)}function Yn(){return(Yn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,Ut({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Z(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ut({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Kn(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new _n;var c=o*i/(i-u)-o,p=c*qn/(qn-Un),l=p-c,d=z(a.assetOut,Number(u))/z(a.assetIn,Number(p)),f=z(a.assetOut,Number(i))/z(a.assetIn,Number(o)),m=Y({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m}}function Hn(t){return Zn.apply(this,arguments)}function Zn(){return(Zn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,Ut({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Z(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ut({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(){return($n=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Xn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Xn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Xn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Xn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,Jn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Hn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:et(s),fees:tt(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Xn||(Xn={}));var tr,er={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?zn({pool:e,reserves:n,assetIn:r,decimals:a}):Kn({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:zn,getFixedOutputSwapQuote:Kn,generateTxns:function(t){return Wn.apply(this,arguments)},signTxns:function(t){return Gn.apply(this,arguments)},execute:function(t){return $n.apply(this,arguments)},executeFixedOutputSwap:Hn},nr=4*t.ALGORAND_MIN_TX_FEE;function rr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof _n})))throw new _n;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function ar(){return ar=c(i().mark((function t(e){var n,r,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,u=s.map((function(t){return new Promise(function(){var e=c(i().mark((function e(s,u){var c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:er.getFixedInputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Fn.getFixedInputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),u(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,rr(u);case 4:return p=t.sent,t.abrupt("return",ir(p));case 6:case"end":return t.stop()}}),t)}))),ar.apply(this,arguments)}function sr(){return sr=c(i().mark((function t(e){var n,r,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,u=s.map((function(t){return new Promise(function(){var e=c(i().mark((function e(s,u){var c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:er.getFixedOutputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Fn.getFixedOutputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),u(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,rr(u);case 4:return p=t.sent,t.abrupt("return",ir(p));case 6:case"end":return t.stop()}}),t)}))),sr.apply(this,arguments)}function or(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:kt(t.route)}function ir(t){var e,n=t[0],r=or(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=or(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}var ur=(f(tr={},S.V1_1,er),f(tr,S.V2,Fn),f(tr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return se(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return ar.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return sr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(tr,"generateTxns",(function(t){return t.isUsingSwapRouter||t.pool.contractVersion!==S.V1_1?Fn.generateTxns(t):er.generateTxns(t)})),f(tr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&function(t){if(t.type===exports.SwapQuoteType.Direct)return t.quoteWithPool.pool.contractVersion;return S.V2}(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return er.signTxns(o({pool:e},t))}return Fn.signTxns(t)})),f(tr,"execute",(function(t){return t.contractVersion===S.V1_1?er.execute(t):Fn.execute(t)})),tr);function cr(){return(cr=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,pr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,Z(n,[o]);case 7:return u=t.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:tt(a),confirmedRound:d,txnID:l,groupID:et(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function pr(t){return lr.apply(this,arguments)}function lr(){return(lr=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,u=v(o,1),c=u[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dr(){return dr=c(i().mark((function t(e){var n,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:tt(e),groupID:et(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return u=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(i().mark((function a(s,o){var c,p,l,d,f,m;return i().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?u[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,Z(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),dr.apply(this,arguments)}function fr(){return(fr=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,u=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[rt("redeem")],note:st.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(u),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(u),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[rt("commit"),u],note:B([rt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function gr(){return(gr=c(i().mark((function e(n){var r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=mr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[rt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=lt,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=mn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Fe,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=dt,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Qn,exports.Swap=ur,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=xn,exports.V2AddLiquidityTxnIndices=je,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=W,exports.calculateAccountMinimumRequiredBalance=Vt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=ce,exports.prepareCommitTransactions=function(t){return gr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return dr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return cr.apply(this,arguments)},exports.sendAndWaitRawTransaction=Z,exports.sumUpTxnFees=tt,exports.tinymanContract_v1_1=Rt,exports.tinymanContract_v2=yt,exports.tinymanJSSDKConfig=st; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index 532ee85f..87d7ca2d 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -82,10 +82,9 @@ export interface SwapQuoteWithPool { export declare type SwapQuote = { quoteWithPool: SwapQuoteWithPool; type: SwapQuoteType.Direct; -} | { - route: SwapRoute; +} | (FetchSwapRouteQuotesResponse & { type: SwapQuoteType.Router; -}; +}); export declare type GetSwapQuoteBySwapTypeParams = Omit; export declare type GetSwapQuoteWithContractVersionParams = { type: SwapType; @@ -175,6 +174,7 @@ export interface SwapRouterQuote { } export declare type FetchSwapRouteQuotesResponse = FetchSwapRouteQuotesPayload & { route: SwapRoute; + price_impact: string; status: { round_number: string; round_datetime: string; diff --git a/dist/swap/v2/router/swap-router.d.ts b/dist/swap/v2/router/swap-router.d.ts index dbde43b8..8c01a6f9 100644 --- a/dist/swap/v2/router/swap-router.d.ts +++ b/dist/swap/v2/router/swap-router.d.ts @@ -2,7 +2,7 @@ import algosdk from "algosdk"; import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; import { SignerTransaction } from "../../../util/commonTypes"; import { SwapType } from "../../constants"; -import { GenerateSwapRouterTxnsParams, SwapRoute } from "../../types"; +import { FetchSwapRouteQuotesResponse, GenerateSwapRouterTxnsParams } from "../../types"; export declare function generateSwapRouterAssetOptInTransaction({ client, routerAppID, assetIDs, accountAddress }: { client: AlgodClient; routerAppID: number; @@ -23,4 +23,4 @@ export declare function getSwapRoute({ amount, assetInID, assetOutID, swapType } assetOutID: number; swapType: SwapType; amount: number | bigint; -}): Promise; +}): Promise; diff --git a/src/swap/types.ts b/src/swap/types.ts index 966bac1d..628ea501 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -88,10 +88,9 @@ export type SwapQuote = quoteWithPool: SwapQuoteWithPool; type: SwapQuoteType.Direct; } - | { - route: SwapRoute; + | (FetchSwapRouteQuotesResponse & { type: SwapQuoteType.Router; - }; + }); export type GetSwapQuoteBySwapTypeParams = Omit; @@ -201,6 +200,7 @@ export interface SwapRouterQuote { export type FetchSwapRouteQuotesResponse = FetchSwapRouteQuotesPayload & { route: SwapRoute; + price_impact: string; status: { round_number: string; round_datetime: string; diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index 41901fa7..c77a197b 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -291,12 +291,12 @@ async function getFixedInputSwapQuote({ }); if ( - swapRoute.length > 1 && - BigInt(swapRoute[swapRoute.length - 1].quote.amount_out.amount) > + swapRoute.route.length > 1 && + BigInt(swapRoute.route[swapRoute.route.length - 1].quote.amount_out.amount) > directSwapQuote.assetOutAmount ) { return { - route: swapRoute, + ...swapRoute, type: SwapQuoteType.Router }; } @@ -380,12 +380,12 @@ async function getFixedOutputSwapQuote({ }); if ( - swapRoute.length > 1 && - BigInt(swapRoute[swapRoute.length - 1].quote.amount_in.amount) < + swapRoute.route.length > 1 && + BigInt(swapRoute.route[swapRoute.route.length - 1].quote.amount_in.amount) < directSwapQuote.assetInAmount ) { return { - route: swapRoute, + ...swapRoute, type: SwapQuoteType.Router }; } diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index d13fa7c3..e3f0965b 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -10,8 +10,7 @@ import {SwapType} from "../../constants"; import { FetchSwapRouteQuotesPayload, FetchSwapRouteQuotesResponse, - GenerateSwapRouterTxnsParams, - SwapRoute + GenerateSwapRouterTxnsParams } from "../../types"; import { V2SwapTxnGroupIndices, @@ -47,8 +46,6 @@ export async function generateSwapRouterAssetOptInTransaction({ // multiplied by the number of inner transactions assetOptInTxn.fee = ALGORAND_MIN_TX_FEE * (innerTransactionCount + 1); - console.log(assetOptInTxn.fee, "router app opt-in fee"); - const txGroup = algosdk.assignGroupID([assetOptInTxn]); return [{txn: txGroup[0], signers: [accountAddress]}]; @@ -159,7 +156,7 @@ export async function getSwapRoute({ assetOutID: number; swapType: SwapType; amount: number | bigint; -}): Promise { +}): Promise { const payload: FetchSwapRouteQuotesPayload = { asset_in_id: String(assetInID), asset_out_id: String(assetOutID), @@ -167,23 +164,23 @@ export async function getSwapRoute({ amount: String(amount) }; - const response = await fetch( - "https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/", - { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(payload) - } - ); - - // TODO: Handle all errors properly - if (response.status !== 200) { - throw new Error("Failed to fetch swap route quotes"); + try { + const response = await fetch( + "https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/", + { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify(payload) + } + ); + + return response.json(); + // TODO: Handle all errors properly + } catch (error) { + console.error(error); + + throw error; } - - const {route}: FetchSwapRouteQuotesResponse = await response.json(); - - return route; } From e8ed68a0519d02416b0354ab6a713820c962e972 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Tue, 28 Feb 2023 15:14:10 +0300 Subject: [PATCH 06/39] refactor(swap): simplify generating swap router txns --- dist/contract/index.d.ts | 2 +- dist/index.js | 4 +-- dist/swap/types.d.ts | 5 +-- dist/swap/v2/router/constants.d.ts | 2 ++ dist/swap/v2/router/swap-router.d.ts | 2 +- dist/swap/v2/router/util.d.ts | 2 ++ src/swap/types.ts | 5 +-- src/swap/v2/router/constants.ts | 7 +++++ src/swap/v2/router/swap-router.ts | 47 +++++++++++++--------------- src/swap/v2/router/util.ts | 12 +++++++ 10 files changed, 51 insertions(+), 37 deletions(-) create mode 100644 dist/swap/v2/router/constants.d.ts create mode 100644 src/swap/v2/router/constants.ts diff --git a/dist/contract/index.d.ts b/dist/contract/index.d.ts index 775db32c..75a11a79 100644 --- a/dist/contract/index.d.ts +++ b/dist/contract/index.d.ts @@ -1,3 +1,3 @@ import { ContractVersionValue } from "./types"; export declare function getIsV2ContractVersion(contractVersion: ContractVersionValue): boolean; -export declare function getContract(contractVersion: ContractVersionValue): import("./v2/contract").TinymanContractV2 | import("./v1_1/contract").TinymanContractV1_1; +export declare function getContract(contractVersion: ContractVersionValue): import("./v1_1/contract").TinymanContractV1_1 | import("./v2/contract").TinymanContractV2; diff --git a/dist/index.js b/dist/index.js index f14df2b9..ee80b331 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),a=Uint8Array.from([1]);function s(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function u(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){u(s,r,a,o,i,"next",t)}function i(t){u(s,r,a,o,i,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={type:"logicsig",logic:{bytecode:"BoAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgQBbNQA0ADEYEkQxGYEBEkSBAUM="},name:"pool_logicsig"},O={type:"app",global_state_schema:{num_uints:0,num_byte_slices:3},local_state_schema:{num_uints:12,num_byte_slices:2},name:"validator_app"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function z(t,e){var n=Number(t);return Y({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function J(t,e){return Y({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function Y(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(H(e),2),s=a[0],o=a[1],i=v(H(Math.round(Number(K(s,o+r)))),2),u=i[0],c=i[1];return Number(K(u,c-r))}return Math.round(e)}function K(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function H(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(i().mark((function t(e,n){var r,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return u=t.sent,c=u.txId,t.next=13,q(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function tt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function et(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function nt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function rt(t){return(new TextEncoder).encode(t)}var at,st=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return rt("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),ot=(f(at={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(at,S.V2,{testnet:148607e3,mainnet:1002541853}),at);function it(t,e){var n=ot[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ut(){return(ut=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:it(a,s),note:st.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ct(){return(ct=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:it(a,s),note:st.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),lt={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},dt={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"};function ft(){return(ft=c(i().mark((function t(e){var n,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.assetID,s=e.initiatorAddr,t.prev=1,t.next=4,n.getTransactionParams().do();case 4:return o=t.sent,u=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:s,assetIndex:a,amount:0,suggestedParams:o}),t.abrupt("return",[{txn:u,signers:[s]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function mt(t,e){var n=Number(t.id),r=Number(e.id);return n>r?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function gt(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function xt(t){return 0===Number(t)}var It,At,ht,Tt,yt=new(function(e){m(r,pt);var n=y(r);function r(t,e){var a;return p(this,r),(a=n.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,a}return d(r,[{key:"generateLogicSigAccountForPool",value:function(e){if(e.asset1ID===e.asset2ID)throw new Error("Assets are the same");var n=it(e.network,S.V2),r=v(gt(e.asset1ID,e.asset2ID),2),a=r[0],s=r[1],o={bytes:Array.from(Buffer.from(this.poolLogicSigContractTemplate,"base64")),validatorAppId:Array.from(t.encodeUint64(n)),asset1ID:Array.from(t.encodeUint64(a)),asset2ID:Array.from(t.encodeUint64(s))},i=[].concat(_(o.bytes.slice(0,3)),_(o.validatorAppId.slice(0,8)),_(o.asset1ID.slice(0,8)),_(o.asset2ID.slice(0,8)),_(o.bytes.slice(27)));return new t.LogicSigAccount(new Uint8Array(i))}}]),r}())(O,E);exports.SwapType=void 0,(It=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",It.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(Tt||(Tt={}));var vt=(f(At={},exports.SwapType.FixedInput,1),f(At,exports.SwapType.FixedOutput,2),At),_t=rt("swap"),wt=(f(ht={},exports.SwapType.FixedInput,rt("fixed-input")),f(ht,exports.SwapType.FixedOutput,rt("fixed-output")),ht),bt={ASSET_OPT_IN:rt("asset_opt_in")};function Dt(){return(Dt=c(i().mark((function e(n){var a,s,o,u,c,p,l,d;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,u=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s,appArgs:[bt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),d=r.default.assignGroupID([p]),e.abrupt("return",[{txn:d[0],signers:[u]}]);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function St(t){return Et.apply(this,arguments)}function Et(){return(Et=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.assetIn,o=n.assetOut,u=n.client,c=n.intermediaryAssetID,p=n.routerAppID,l=n.network,d=n.swapType,e.next=3,u.getTransactionParams().do();case 3:return f=e.sent,m=yt.generateLogicSigAccountForPool({asset1ID:s.id,asset2ID:c,network:l}),g=m.address(),x=yt.generateLogicSigAccountForPool({asset1ID:c,asset2ID:o.id,network:l}),I=x.address(),A=xt(s.id),h=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(p),amount:s.amount,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(p),amount:s.amount,assetIndex:s.id,suggestedParams:f}),T=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:p,appArgs:[_t,wt[d],r.default.encodeUint64(o.amount)],foreignApps:[it(l,S.V2)],foreignAssets:[s.id,c,o.id],accounts:[g,I],suggestedParams:f}),y=d===exports.SwapType.FixedInput?7:8,T.fee=t.ALGORAND_MIN_TX_FEE*(y+1),v=r.default.assignGroupID([h,T]),e.abrupt("return",[{txn:v[Tt.INPUT_TXN],signers:[a]},{txn:v[Tt.APP_CALL_TXN],signers:[a]}]);case 15:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ot(){return(Ot=c(i().mark((function e(n){var r,a,s,o,u,c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nt(t){return Pt.apply(this,arguments)}function Pt(){return(Pt=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});case 5:return u=t.sent,t.abrupt("return",u.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),console.log("buraya girdi",t.t0),t.t0;case 14:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}function kt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return z(r.asset.decimals,Number(r.amount))/z(n.asset.decimals,Number(n.amount))}var Lt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Ft={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Rt=new(function(n){m(a,pt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=it(n.network,S.V1_1),a=v(gt(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(i().mark((function t(a,s){var u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:u=t.sent,a(o(o({},u),{},{minimum_required_balance:Vt(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Ct(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?R({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Vt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Xt,jt,Qt=rt("e");function Ut(t){return qt.apply(this,arguments)}function qt(){return(qt=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,u=a.accountAddr,n.next=3,s.accountInformation(u).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=R({stateArray:A}),T=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(B([r.default.decodeAddress(m).publicKey,Qt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Gt(){return(Gt=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,u=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==u})),d=[],l&&l["key-value"])for(f=R({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mJt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Jt,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Zt(){return Zt=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d=arguments;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,Mt(n,r);case 6:return o=t.sent,u=Ct(o,it(a,S.V1_1)),c=null,u&&(l=o["created-assets"][0],p=l.index,c={asset1ID:u[Wt[S.V1_1].asset1],asset2ID:u[Wt[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Zt.apply(this,arguments)}var $t=Object.freeze({__proto__:null,getPoolInfo:Yt,getPoolReserves:function(t,e){return Ht.apply(this,arguments)},getPoolAssets:function(t){return Zt.apply(this,arguments)}});function te(t){return ee.apply(this,arguments)}function ee(){return(ee=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Bt(S.V2),u=o.generateLogicSigAccountForPool(e),c=it(r,S.V2),p=u.address(),l=gt(a,s),t.next=8,Mt(n,p);case 8:return d=t.sent,f=Ct(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[Wt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Wt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Wt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Wt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Wt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Wt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Wt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Wt.v2.totalFeeShare]),m.poolTokenID=Number(f[Wt.v2.poolTokenID]),m.asset1ID=Number(f[Wt.v2.asset1]),m.asset2ID=Number(f[Wt.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ne(){return(ne=c(i().mark((function t(e,n){var r,a,s;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Mt(e,n.account.address());case 2:return r=t.sent,a=Ct(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[Wt.v2.asset1Reserves]),s.asset2=BigInt(a[Wt.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[Wt.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function re(){return(re=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,Mt(n,r);case 3:return s=t.sent,o=Ct(s,it(a,S.V2)),u=null,o&&(u={asset1ID:o[Wt[S.V2].asset1],asset2ID:o[Wt[S.V2].asset2],poolTokenID:o[Wt[S.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ae=Object.freeze({__proto__:null,getPoolInfo:te,getPoolReserves:function(t,e){return ne.apply(this,arguments)},getPoolAssets:function(t){return re.apply(this,arguments)}});function se(t){return Boolean(t&&!(t.asset1+t.asset2))}var oe,ie,ue=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=se(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=z(t.asset1,e.asset1)/z(t.asset2,e.asset2)),r},isPoolEmpty:se,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Yt(t),te(t)])}}),ce=o((f(oe={},S.V1_1,o(o({},$t),ue)),f(oe,S.V2,o(o({},ae),ue)),oe),ue);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(ie||(ie={}));var pe=96e4,le=859e3;function de(){return(de=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,O,N;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,u=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=mt(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=xt(x),h=it(a,S.V1_1),T=Rt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:st.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[rt("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:dt.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:fe(A),suggestedParams:c}),(E=[])[ie.FUNDING_TXN]=D,E[ie.VALIDATOR_APP_CALL]=_,E[ie.POOL_TOKEN_CREATE]=w,E[ie.ASSET1_OPT_IN]=b,A||(E[ie.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),O=r.default.assignGroupID(E),N=[{txn:O[ie.FUNDING_TXN],signers:[u]},{txn:O[ie.VALIDATOR_APP_CALL],signers:[y]},{txn:O[ie.POOL_TOKEN_CREATE],signers:[y]},{txn:O[ie.ASSET1_OPT_IN],signers:[y]}],O[ie.ASSET2_OPT_IN]&&N.push({txn:O[ie.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",N);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(t){return t?pe:le}function me(){return(me=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=gt(o,u),f=v(d,2),m=f[0],g=f[1],x=Rt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===ie.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ge(t){return xe.apply(this,arguments)}function xe(){return(xe=c(i().mark((function t(e){var n,r,a,s,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,q(n,a[ie.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Ie(){return(Ie=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,ge({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",ce.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ae,he={generateTxns:function(t){return de.apply(this,arguments)},signTxns:function(t){return me.apply(this,arguments)},execute:function(t){return Ie.apply(this,arguments)},getBootstrapFundingTxnAmount:fe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ae||(Ae={}));var Te=5,ye=6;function ve(t,e){return _e.apply(this,arguments)}function _e(){return(_e=c(i().mark((function e(n,r){var a,s,o;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function we(t,e){return be.apply(this,arguments)}function be(){return(be=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function De(){return(De=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,u=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=it(s,S.V2),d=t.getApplicationAddress(l),f=mt(o,u),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,ce.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(u.unit_name," already exists"));case 12:return I=yt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=xt(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[rt("bootstrap")],note:st.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ee(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Se(h),suggestedParams:p}),(_=[])[Ae.FUNDING_TXN]=y,_[Ae.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Ae.FUNDING_TXN]={txn:w[Ae.FUNDING_TXN],signers:[c]},b[Ae.VALIDATOR_APP_CALL]={txn:w[Ae.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Se(t){return function(t,e){var n=Bt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ee(t)+1e5}function Ee(e){return((e?Te:ye)+1)*t.ALGORAND_MIN_TX_FEE}function Oe(){return(Oe=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=gt(o,u),f=v(d,2),m=f[0],g=f[1],x=yt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ae.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ne(){return(Ne=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,ve(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Wt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",ce.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Pe,ke={generateTxns:function(t){return De.apply(this,arguments)},signTxns:function(t){return Oe.apply(this,arguments)},execute:function(t){return Ne.apply(this,arguments)},getBootstrapFundingTxnAmount:Se,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Se(e)}};var Le,Fe=(f(Pe={},S.V1_1,he),f(Pe,S.V2,ke),f(Pe,"generateTxns",(function(t){return t.contractVersion===S.V1_1?he.generateTxns(t):ke.generateTxns(t)})),f(Pe,"signTxns",(function(t){return t.contractVersion===S.V1_1?he.signTxns(t):ke.signTxns(t)})),f(Pe,"execute",(function(t){return t.contractVersion===S.V1_1?he.execute(t):ke.execute(t)})),f(Pe,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?he.getBootstrapFundingTxnAmount(n):ke.getBootstrapFundingTxnAmount(n)})),Pe);exports.V1_1AddLiquidityTxnIndices=void 0,(Le=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Le.FEE_TXN=0]="FEE_TXN",Le[Le.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Le[Le.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Le[Le.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Le[Le.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Re,Be,Me,Ce,Ve=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Ce=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Ce.FLEXIBLE="flexible",Ce.INITIAL="initial";var Xe,je=(f(Re={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Re,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Re,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Re),Qe=(f(Be={},exports.V2AddLiquidityType.INITIAL,1),f(Be,exports.V2AddLiquidityType.SINGLE,2),f(Be,exports.V2AddLiquidityType.FLEXIBLE,2),Be),Ue=(f(Me={},exports.V2AddLiquidityType.INITIAL,3),f(Me,exports.V2AddLiquidityType.FLEXIBLE,3),f(Me,exports.V2AddLiquidityType.SINGLE,2),Me);function qe(t){var e=t.assetIn,n=t.assetOut;return z(n.decimals,Number(n.amount))/z(e.decimals,Number(e.amount))}function Ge(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=qe({assetIn:r,assetOut:a}),o=z(a.decimals,Number(n))/z(r.decimals,Number(e));return Y({decimalPlaces:5},Math.abs(s/o-1))}function We(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,i=t.asset1Amount,u=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(i),d=s.asset2+BigInt(u),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(i)-x,h=BigInt(u)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=ze(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=ze(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:Ge({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function ze(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Je(e){return(Qe[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ye=rt("add_liquidity"),Ke=(f(Xe={},S.V1_1,[rt("mint")]),f(Xe,S.V2,{INITIAL_LIQUIDITY:[rt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ye,rt("single")],FLEXIBLE_MODE:[Ye,rt("flexible")]}),Xe);function He(){return(He=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,u=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=W("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:it(s,S.V1_1),appArgs:Ke.v1_1,accounts:[d],note:st.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[u.id,p.id]:[u.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ze(){return(Ze=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $e(){return($e=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,u=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Ut({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,Z(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=tt(a),x=et(a),t.next=17,Ut({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var tn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function wn(){return(wn=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!e.isUsingSwapRouter){t.next=2;break}return t.abrupt("return",St(e));case 2:if(n=e.assetIn,a=e.assetOut,s=e.client,o=e.initiatorAddr,u=e.pool,c=e.slippage,p=e.swapType,l=u.account.address(),(d=[u.asset1ID,u.asset2ID]).includes(n.id)&&d.includes(a.id)&&n.id!==a.id){t.next=7;break}throw new F({pool:u,assetIn:n,assetOut:a},"Input asset (#".concat(n.id,") and output asset (#").concat(a.id,") provided to generate transactions do not belong to the pool ").concat(l,"."));case 7:return t.next=9,s.getTransactionParams().do();case 9:return f=t.sent,m=xt(n.id),g=p===exports.SwapType.FixedInput?n.amount:W("positive",c,n.amount),x=p===exports.SwapType.FixedOutput?a.amount:W("negative",c,a.amount),I=m?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,assetIndex:n.id,suggestedParams:f}),(A=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:u.validatorAppID,appArgs:[_t,wt[p],r.default.encodeUint64(x)],note:st.getAppCallTxnNoteWithClientName(S.V2),accounts:[l],foreignAssets:[u.asset1ID,u.asset2ID],suggestedParams:f})).fee=bn(p),(h=[])[Tt.INPUT_TXN]=I,h[Tt.APP_CALL_TXN]=A,T=r.default.assignGroupID(h),t.abrupt("return",[{txn:T[Tt.INPUT_TXN],signers:[o]},{txn:T[Tt.APP_CALL_TXN],signers:[o]}]);case 21:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function bn(e){return(vt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Dn(){return(Dn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,u=e.signedTxns,c=e.assetIn,t.next=3,Z(a,[u]);case 3:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=10,we(a,o);case 10:return g=t.sent,x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.route[s.route.length-1].quote.amount_out.asset.id),I=null==g||null===(n=g.find((function(t){return t.txn.txn.xaid===c.id})))||void 0===n?void 0:n.txn.txn,A=null==g||null===(r=g.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:f,assetIn:I&&{amount:BigInt(c.amount)-BigInt(I.aamt||0),id:c.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:m});case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Sn(){return(Sn=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.asset,r=e.decimals,a=e.pool,s=e.type,o=e.isSwapRouterEnabled,s!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,En({pool:a,assetIn:n,decimals:r,isSwapRouterEnabled:o});case 4:u=t.sent,t.next=10;break;case 7:return t.next=9,Nn({pool:a,assetOut:n,decimals:r,isSwapRouterEnabled:o});case 9:u=t.sent;case 10:return t.abrupt("return",u);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(t){return On.apply(this,arguments)}function On(){return(On=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetIn,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:n,assetIn:r},"Trying to swap on a non-existent pool");case 3:if(u=BigInt(r.amount),c=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}p=n.asset2ID,l=n.asset1Reserves,d=n.asset2Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}p=n.asset1ID,l=n.asset2Reserves,d=n.asset1Reserves,t.next=18;break;case 17:throw new F({pool:n,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(f=kn({inputSupply:l,outputSupply:d,swapInputAmount:u,totalFeeShare:c,decimals:a}),m=f.swapOutputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(m>d)){t.next=21;break}throw new _n;case 21:if(I={assetInID:r.id,assetInAmount:u,assetOutID:p,assetOutAmount:m,swapFee:Number(g),rate:z(a.assetOut,Number(m))/z(a.assetIn,Number(u)),priceImpact:x},!s){t.next=28;break}return t.next=25,Nt({amount:r.amount,assetInID:Number(r.id),assetOutID:p,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_out.amount)>I.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},A),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:n,quote:I},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nn(t){return Pn.apply(this,arguments)}function Pn(){return(Pn=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetOut,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:n,assetOut:r},"Trying to swap on a non-existent pool");case 3:if(u=BigInt(r.amount),c=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}p=n.asset2ID,l=n.asset2Reserves,d=n.asset1Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}p=n.asset1ID,l=n.asset1Reserves,d=n.asset2Reserves,t.next=18;break;case 17:throw new F({pool:n,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(f=Ln({inputSupply:l,outputSupply:d,swapOutputAmount:u,totalFeeShare:c,decimals:a}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(u>d)){t.next=21;break}throw new _n;case 21:if(I={assetInID:p,assetInAmount:m,assetOutID:r.id,assetOutAmount:u,swapFee:Number(g),rate:z(a.assetOut,Number(u))/z(a.assetIn,Number(m)),priceImpact:x},!s){t.next=28;break}return t.next=25,Nt({amount:r.amount,assetInID:p,assetOutID:r.id,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Bn(){return(Bn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,u=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s.validatorAppID,note:st.getAppCallTxnNoteWithClientName(S.V2),appArgs:[In,r.default.encodeUint64(W("negative",l,c)),r.default.encodeUint64(W("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[gn.ASSET_TRANSFER_TXN]=g,I[gn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[gn.ASSET_TRANSFER_TXN],signers:[u]},{txn:A[gn.APP_CALL_TXN],signers:[u]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,u=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=W("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:u,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:st.getAppCallTxnNoteWithClientName(S.V2),appArgs:[In,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[gn.ASSET_TRANSFER_TXN]=T,v[gn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[gn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[gn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Cn(){return(Cn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,Z(r,[s]);case 3:return o=t.sent,u=v(o,1),c=u[0].txnID,t.next=8,we(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Vn,Xn,jn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Rn(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=Rn(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Fn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Fn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Bn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Mn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Cn.apply(this,arguments)}},Qn=(f(Vn={},S.V1_1,vn),f(Vn,S.V2,jn),Vn),Un=3n,qn=1000n;function Gn(){return(Gn=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,u=v(o,2),c=u[0],p=u[1],l=a.map((function(t,e){return e===Xn.FEE_TXN_INDEX?c:e===Xn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Wn(){return(Wn=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,u=e.assetIn,c=e.assetOut,p=e.slippage,l=e.initiatorAddr,d=s.account.address(),(f=[s.asset1ID,s.asset2ID]).includes(u.id)&&f.includes(c.id)&&u.id!==c.id){t.next=5;break}throw new F({pool:s,assetIn:u,assetOut:c},"Input asset (#".concat(u.id,") and output asset (#").concat(c.id,") provided to generate transactions do not belong to the pool ").concat(d,"."));case 5:return t.next=7,n.getTransactionParams().do();case 7:return m=t.sent,g=[rt("swap"),o===exports.SwapType.FixedInput?rt("fi"):rt("fo")],x=r.default.makeApplicationNoOpTxnFromObject({from:d,appIndex:s.validatorAppID,appArgs:g,accounts:[l],note:st.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:m}),I=o===exports.SwapType.FixedOutput?W("positive",p,u.amount):u.amount,A=0===u.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:u.id,amount:I,suggestedParams:m}),h=o===exports.SwapType.FixedInput?W("negative",p,c.amount):c.amount,T=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:l,amount:h,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:l,assetIndex:c.id,amount:h,suggestedParams:m}),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.fee+T.fee,note:a,suggestedParams:m}),v=r.default.assignGroupID([y,x,A,T]),t.abrupt("return",[{txn:v[0],signers:[l]},{txn:v[1],signers:[d]},{txn:v[2],signers:[l]},{txn:v[3],signers:[d]}]);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function zn(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,i,u,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*Un/qn,l=u-i*u/(i+(c-p));if(l>u)throw new _n;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:qe(d),priceImpact:Ge(o({inputSupply:i,outputSupply:u},d))}}function Jn(t){return Yn.apply(this,arguments)}function Yn(){return(Yn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,Ut({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Z(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ut({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Kn(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new _n;var c=o*i/(i-u)-o,p=c*qn/(qn-Un),l=p-c,d=z(a.assetOut,Number(u))/z(a.assetIn,Number(p)),f=z(a.assetOut,Number(i))/z(a.assetIn,Number(o)),m=Y({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m}}function Hn(t){return Zn.apply(this,arguments)}function Zn(){return(Zn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,Ut({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Z(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ut({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(){return($n=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Xn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Xn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Xn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Xn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,Jn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Hn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:et(s),fees:tt(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Xn||(Xn={}));var tr,er={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?zn({pool:e,reserves:n,assetIn:r,decimals:a}):Kn({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:zn,getFixedOutputSwapQuote:Kn,generateTxns:function(t){return Wn.apply(this,arguments)},signTxns:function(t){return Gn.apply(this,arguments)},execute:function(t){return $n.apply(this,arguments)},executeFixedOutputSwap:Hn},nr=4*t.ALGORAND_MIN_TX_FEE;function rr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof _n})))throw new _n;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function ar(){return ar=c(i().mark((function t(e){var n,r,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,u=s.map((function(t){return new Promise(function(){var e=c(i().mark((function e(s,u){var c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:er.getFixedInputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Fn.getFixedInputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),u(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,rr(u);case 4:return p=t.sent,t.abrupt("return",ir(p));case 6:case"end":return t.stop()}}),t)}))),ar.apply(this,arguments)}function sr(){return sr=c(i().mark((function t(e){var n,r,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,u=s.map((function(t){return new Promise(function(){var e=c(i().mark((function e(s,u){var c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:er.getFixedOutputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Fn.getFixedOutputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),u(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,rr(u);case 4:return p=t.sent,t.abrupt("return",ir(p));case 6:case"end":return t.stop()}}),t)}))),sr.apply(this,arguments)}function or(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:kt(t.route)}function ir(t){var e,n=t[0],r=or(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=or(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}var ur=(f(tr={},S.V1_1,er),f(tr,S.V2,Fn),f(tr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return se(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return ar.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return sr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(tr,"generateTxns",(function(t){return t.isUsingSwapRouter||t.pool.contractVersion!==S.V1_1?Fn.generateTxns(t):er.generateTxns(t)})),f(tr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&function(t){if(t.type===exports.SwapQuoteType.Direct)return t.quoteWithPool.pool.contractVersion;return S.V2}(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return er.signTxns(o({pool:e},t))}return Fn.signTxns(t)})),f(tr,"execute",(function(t){return t.contractVersion===S.V1_1?er.execute(t):Fn.execute(t)})),tr);function cr(){return(cr=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,pr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,Z(n,[o]);case 7:return u=t.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:tt(a),confirmedRound:d,txnID:l,groupID:et(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function pr(t){return lr.apply(this,arguments)}function lr(){return(lr=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,u=v(o,1),c=u[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dr(){return dr=c(i().mark((function t(e){var n,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:tt(e),groupID:et(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return u=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(i().mark((function a(s,o){var c,p,l,d,f,m;return i().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?u[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,Z(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),dr.apply(this,arguments)}function fr(){return(fr=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,u=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[rt("redeem")],note:st.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(u),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(u),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[rt("commit"),u],note:B([rt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function gr(){return(gr=c(i().mark((function e(n){var r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=mr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[rt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=lt,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=mn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Fe,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=dt,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Qn,exports.Swap=ur,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=xn,exports.V2AddLiquidityTxnIndices=je,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=W,exports.calculateAccountMinimumRequiredBalance=Vt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=ce,exports.prepareCommitTransactions=function(t){return gr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return dr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return cr.apply(this,arguments)},exports.sendAndWaitRawTransaction=Z,exports.sumUpTxnFees=tt,exports.tinymanContract_v1_1=Rt,exports.tinymanContract_v2=yt,exports.tinymanJSSDKConfig=st; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",s=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,s=Object.create(a.prototype),o=new w(r||[]);return s._invoke=function(t,e,n){var r="suspendedStart";return function(a,s){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw s;return D()}for(n.method=a,n.arg=s;;){var o=n.delegate;if(o){var u=y(o,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,o),s}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,a)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,s,o,u){var i=p(t[a],t,s);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,u)}),(function(t){r("throw",t,o,u)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function s(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(s,s):s()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function w(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,s=function e(){for(;++r=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E="- would result negative",N="logic eval error:",O="exceeds schema integer count",P=/transaction \w+:/,k=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function X(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[s?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function j(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=D(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var Q=100000n,U=100000n,q=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return Q+U*BigInt(o.length)+q*BigInt(s.length+a.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(nt(e),2),s=a[0],o=a[1],u=v(nt(Math.round(Number(et(s,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return at.apply(this,arguments)}function at(){return(at=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new k(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function st(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),Tt=it("swap"),yt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),vt={ASSET_OPT_IN:it("asset_opt_in")},_t={testnet:159521633,mainnet:0};function wt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function bt(t){if(!_t[t])throw new Error("Unknown network or network not supported: ".concat(t));return _t[t]}function Dt(){return(Dt=c(u().mark((function e(n){var a,s,o,i,c,p,l,d;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[vt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),d=r.default.assignGroupID([p]),e.abrupt("return",[{txn:d[0],signers:[i]}]);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function St(t){return Et.apply(this,arguments)}function Et(){return(Et=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(c[0].quote.amount_in.amount),Number(c[c.length-1].quote.amount_out.amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=bt(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[Tt,yt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p}),b=i===exports.SwapType.FixedInput?7:8,w.fee=t.ALGORAND_MIN_TX_FEE*(b+1),D=r.default.assignGroupID([_,w]),e.abrupt("return",[{txn:D[It.INPUT_TXN],signers:[a]},{txn:D[It.APP_CALL_TXN],signers:[a]}]);case 15:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nt(){return(Nt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ot(t){return Pt.apply(this,arguments)}function Pt(){return(Pt=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});case 5:return i=t.sent,t.abrupt("return",i.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var kt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Lt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Ft=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Rt=new(function(n){m(a,Ft);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:Qt(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function jt(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?X({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Qt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Ut,qt,Gt=it("e");function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=X({stateArray:A}),T=e.fromByteArray(j([r.default.decodeAddress(m).publicKey,Gt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(j([r.default.decodeAddress(m).publicKey,Gt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(j([r.default.decodeAddress(m).publicKey,Gt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Jt(){return(Jt=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=X({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mHt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ht,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ee(){return ee=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,Xt(n,r);case 6:return o=t.sent,i=jt(o,dt(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[Yt[S.V1_1].asset1],asset2ID:i[Yt[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ee.apply(this,arguments)}var ne=Object.freeze({__proto__:null,getPoolInfo:Zt,getPoolReserves:function(t,e){return te.apply(this,arguments)},getPoolAssets:function(t){return ee.apply(this,arguments)}});function re(t){return ae.apply(this,arguments)}function ae(){return(ae=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Vt(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(a,s),t.next=8,Xt(n,p);case 8:return d=t.sent,f=jt(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[Yt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Yt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Yt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Yt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Yt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Yt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Yt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Yt.v2.totalFeeShare]),m.poolTokenID=Number(f[Yt.v2.poolTokenID]),m.asset1ID=Number(f[Yt.v2.asset1]),m.asset2ID=Number(f[Yt.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function se(){return(se=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Xt(e,n.account.address());case 2:return r=t.sent,a=jt(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[Yt.v2.asset1Reserves]),s.asset2=BigInt(a[Yt.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[Yt.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oe(){return(oe=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,Xt(n,r);case 3:return s=t.sent,o=jt(s,dt(a,S.V2)),i=null,o&&(i={asset1ID:o[Yt[S.V2].asset1],asset2ID:o[Yt[S.V2].asset2],poolTokenID:o[Yt[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ue=Object.freeze({__proto__:null,getPoolInfo:re,getPoolReserves:function(t,e){return se.apply(this,arguments)},getPoolAssets:function(t){return oe.apply(this,arguments)}});function ie(t){return Boolean(t&&!(t.asset1+t.asset2))}var ce,pe,le=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ie(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:ie,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Zt(t),re(t)])}}),de=o((f(ce={},S.V1_1,o(o({},ne),le)),f(ce,S.V2,o(o({},ue),le)),ce),le);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(pe||(pe={}));var fe=96e4,me=859e3;function ge(){return(ge=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(a,S.V1_1),T=Rt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:F.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:xe(A),suggestedParams:c}),(E=[])[pe.FUNDING_TXN]=D,E[pe.VALIDATOR_APP_CALL]=_,E[pe.POOL_TOKEN_CREATE]=w,E[pe.ASSET1_OPT_IN]=b,A||(E[pe.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),O=[{txn:N[pe.FUNDING_TXN],signers:[i]},{txn:N[pe.VALIDATOR_APP_CALL],signers:[y]},{txn:N[pe.POOL_TOKEN_CREATE],signers:[y]},{txn:N[pe.ASSET1_OPT_IN],signers:[y]}],N[pe.ASSET2_OPT_IN]&&O.push({txn:N[pe.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xe(t){return t?fe:me}function Ie(){return(Ie=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Rt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===pe.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ae(t){return he.apply(this,arguments)}function he(){return(he=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,a[pe.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Te(){return(Te=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Ae({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",de.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ye,ve={generateTxns:function(t){return ge.apply(this,arguments)},signTxns:function(t){return Ie.apply(this,arguments)},execute:function(t){return Te.apply(this,arguments)},getBootstrapFundingTxnAmount:xe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(ye||(ye={}));var _e=5,we=6;function be(t,e){return De.apply(this,arguments)}function De(){return(De=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Se(t,e){return Ee.apply(this,arguments)}function Ee(){return(Ee=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,be(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ne(){return(Ne=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=dt(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,de.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Ct.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Pe(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Oe(h),suggestedParams:p}),(_=[])[ye.FUNDING_TXN]=y,_[ye.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[ye.FUNDING_TXN]={txn:w[ye.FUNDING_TXN],signers:[c]},b[ye.VALIDATOR_APP_CALL]={txn:w[ye.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Oe(t){return function(t,e){var n=Vt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Pe(t)+1e5}function Pe(e){return((e?_e:we)+1)*t.ALGORAND_MIN_TX_FEE}function ke(){return(ke=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Ct.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===ye.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Le(){return(Le=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,be(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Yt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",de.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new k(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Fe,Re={generateTxns:function(t){return Ne.apply(this,arguments)},signTxns:function(t){return ke.apply(this,arguments)},execute:function(t){return Le.apply(this,arguments)},getBootstrapFundingTxnAmount:Oe,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Oe(e)}};var Be,Me=(f(Fe={},S.V1_1,ve),f(Fe,S.V2,Re),f(Fe,"generateTxns",(function(t){return t.contractVersion===S.V1_1?ve.generateTxns(t):Re.generateTxns(t)})),f(Fe,"signTxns",(function(t){return t.contractVersion===S.V1_1?ve.signTxns(t):Re.signTxns(t)})),f(Fe,"execute",(function(t){return t.contractVersion===S.V1_1?ve.execute(t):Re.execute(t)})),f(Fe,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?ve.getBootstrapFundingTxnAmount(n):Re.getBootstrapFundingTxnAmount(n)})),Fe);exports.V1_1AddLiquidityTxnIndices=void 0,(Be=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Be.FEE_TXN=0]="FEE_TXN",Be[Be.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Be[Be.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Be[Be.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Be[Be.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Ce,Ve,Xe,je,Qe=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(je=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",je.FLEXIBLE="flexible",je.INITIAL="initial";var Ue,qe=(f(Ce={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Ce,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Ce,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Ce),Ge=(f(Ve={},exports.V2AddLiquidityType.INITIAL,1),f(Ve,exports.V2AddLiquidityType.SINGLE,2),f(Ve,exports.V2AddLiquidityType.FLEXIBLE,2),Ve),We=(f(Xe={},exports.V2AddLiquidityType.INITIAL,3),f(Xe,exports.V2AddLiquidityType.FLEXIBLE,3),f(Xe,exports.V2AddLiquidityType.SINGLE,2),Xe);function ze(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function Je(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ze({assetIn:r,assetOut:a}),o=Z(a.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(s/o-1))}function Ye(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,u=t.asset1Amount,i=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(u),d=s.asset2+BigInt(i),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(u)-x,h=BigInt(i)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=Ke(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=Ke(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:Je({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function Ke(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function He(e){return(Ge[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ze=it("add_liquidity"),$e=(f(Ue={},S.V1_1,[it("mint")]),f(Ue,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ze,it("single")],FLEXIBLE_MODE:[Ze,it("flexible")]}),Ue);function tn(){return(tn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(s,S.V1_1),appArgs:$e.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(){return(en=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nn(){return(nn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Wt({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=st(a),x=ot(a),t.next=17,Wt({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new k(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var rn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Sn(){return(Sn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!e.isUsingSwapRouter){t.next=2;break}return t.abrupt("return",St(e));case 2:if(n=e.assetIn,a=e.assetOut,s=e.client,o=e.initiatorAddr,i=e.pool,c=e.slippage,p=e.swapType,l=i.account.address(),(d=[i.asset1ID,i.asset2ID]).includes(n.id)&&d.includes(a.id)&&n.id!==a.id){t.next=7;break}throw new k({pool:i,assetIn:n,assetOut:a},"Input asset (#".concat(n.id,") and output asset (#").concat(a.id,") provided to generate transactions do not belong to the pool ").concat(l,"."));case 7:return t.next=9,s.getTransactionParams().do();case 9:return f=t.sent,m=C(n.id),g=p===exports.SwapType.FixedInput?n.amount:H("positive",c,n.amount),x=p===exports.SwapType.FixedOutput?a.amount:H("negative",c,a.amount),I=m?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,assetIndex:n.id,suggestedParams:f}),(A=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:i.validatorAppID,appArgs:[Tt,yt[p],r.default.encodeUint64(x)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[l],foreignAssets:[i.asset1ID,i.asset2ID],suggestedParams:f})).fee=En(p),(h=[])[It.INPUT_TXN]=I,h[It.APP_CALL_TXN]=A,T=r.default.assignGroupID(h),t.abrupt("return",[{txn:T[It.INPUT_TXN],signers:[o]},{txn:T[It.APP_CALL_TXN],signers:[o]}]);case 21:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(e){return(ht[e]+1)*t.ALGORAND_MIN_TX_FEE}function Nn(){return(Nn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,c=e.assetIn,t.next=3,rt(a,[i]);case 3:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=10,Se(a,o);case 10:return g=t.sent,x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.route[s.route.length-1].quote.amount_out.asset.id),I=null==g||null===(n=g.find((function(t){return t.txn.txn.xaid===c.id})))||void 0===n?void 0:n.txn.txn,A=null==g||null===(r=g.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:f,assetIn:I&&{amount:BigInt(c.amount)-BigInt(I.aamt||0),id:c.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:m});case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function On(){return(On=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.asset,r=e.decimals,a=e.pool,s=e.type,o=e.isSwapRouterEnabled,s!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,Pn({pool:a,assetIn:n,decimals:r,isSwapRouterEnabled:o});case 4:i=t.sent,t.next=10;break;case 7:return t.next=9,Ln({pool:a,assetOut:n,decimals:r,isSwapRouterEnabled:o});case 9:i=t.sent;case 10:return t.abrupt("return",i);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(t){return kn.apply(this,arguments)}function kn(){return(kn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetIn,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:n,assetIn:r},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(r.amount),c=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}p=n.asset2ID,l=n.asset1Reserves,d=n.asset2Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}p=n.asset1ID,l=n.asset2Reserves,d=n.asset1Reserves,t.next=18;break;case 17:throw new k({pool:n,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(f=Rn({inputSupply:l,outputSupply:d,swapInputAmount:i,totalFeeShare:c,decimals:a}),m=f.swapOutputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(m>d)){t.next=21;break}throw new Dn;case 21:if(I={assetInID:r.id,assetInAmount:i,assetOutID:p,assetOutAmount:m,swapFee:Number(g),rate:Z(a.assetOut,Number(m))/Z(a.assetIn,Number(i)),priceImpact:x},!s){t.next=28;break}return t.next=25,Ot({amount:r.amount,assetInID:Number(r.id),assetOutID:p,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_out.amount)>I.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},A),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:n,quote:I},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ln(t){return Fn.apply(this,arguments)}function Fn(){return(Fn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetOut,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:n,assetOut:r},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(r.amount),c=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}p=n.asset2ID,l=n.asset2Reserves,d=n.asset1Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}p=n.asset1ID,l=n.asset1Reserves,d=n.asset2Reserves,t.next=18;break;case 17:throw new k({pool:n,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(f=Bn({inputSupply:l,outputSupply:d,swapOutputAmount:i,totalFeeShare:c,decimals:a}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(i>d)){t.next=21;break}throw new Dn;case 21:if(I={assetInID:p,assetInAmount:m,assetOutID:r.id,assetOutAmount:i,swapFee:Number(g),rate:Z(a.assetOut,Number(i))/Z(a.assetIn,Number(m)),priceImpact:x},!s){t.next=28;break}return t.next=25,Ot({amount:r.amount,assetInID:p,assetOutID:r.id,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Vn(){return(Vn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Tn,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[An.ASSET_TRANSFER_TXN]=g,I[An.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[An.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[An.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xn(){return(Xn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Tn,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[An.ASSET_TRANSFER_TXN]=T,v[An.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[An.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[An.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jn(){return(jn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,rt(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Se(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qn,Un,qn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Cn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=Cn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Mn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Mn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Vn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Xn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return jn.apply(this,arguments)}},Gn=(f(Qn={},S.V1_1,bn),f(Qn,S.V2,qn),Qn),Wn=3n,zn=1000n;function Jn(){return(Jn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===Un.FEE_TXN_INDEX?c:e===Un.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yn(){return(Yn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.assetIn,c=e.assetOut,p=e.slippage,l=e.initiatorAddr,d=s.account.address(),(f=[s.asset1ID,s.asset2ID]).includes(i.id)&&f.includes(c.id)&&i.id!==c.id){t.next=5;break}throw new k({pool:s,assetIn:i,assetOut:c},"Input asset (#".concat(i.id,") and output asset (#").concat(c.id,") provided to generate transactions do not belong to the pool ").concat(d,"."));case 5:return t.next=7,n.getTransactionParams().do();case 7:return m=t.sent,g=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],x=r.default.makeApplicationNoOpTxnFromObject({from:d,appIndex:s.validatorAppID,appArgs:g,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:m}),I=o===exports.SwapType.FixedOutput?H("positive",p,i.amount):i.amount,A=0===i.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:i.id,amount:I,suggestedParams:m}),h=o===exports.SwapType.FixedInput?H("negative",p,c.amount):c.amount,T=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:l,amount:h,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:l,assetIndex:c.id,amount:h,suggestedParams:m}),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.fee+T.fee,note:a,suggestedParams:m}),v=r.default.assignGroupID([y,x,A,T]),t.abrupt("return",[{txn:v[0],signers:[l]},{txn:v[1],signers:[d]},{txn:v[2],signers:[l]},{txn:v[3],signers:[d]}]);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Kn(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new k({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new k({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*Wn/zn,l=i-u*i/(u+(c-p));if(l>i)throw new Dn;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:ze(d),priceImpact:Je(o({inputSupply:u,outputSupply:i},d))}}function Hn(t){return Zn.apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Wt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Wt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new k({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new k({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new Dn;var c=o*u/(u-i)-o,p=c*zn/(zn-Wn),l=p-c,d=Z(a.assetOut,Number(i))/Z(a.assetIn,Number(p)),f=Z(a.assetOut,Number(u))/Z(a.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Wt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Wt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Un.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Un.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Un.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Un.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,Hn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,tr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(s),fees:st(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new k(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Un||(Un={}));var rr,ar={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Kn({pool:e,reserves:n,assetIn:r,decimals:a}):$n({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Kn,getFixedOutputSwapQuote:$n,generateTxns:function(t){return Yn.apply(this,arguments)},signTxns:function(t){return Jn.apply(this,arguments)},execute:function(t){return nr.apply(this,arguments)},executeFixedOutputSwap:tr},sr=4*t.ALGORAND_MIN_TX_FEE;function or(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Dn})))throw new Dn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function ur(){return ur=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ar.getFixedInputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Mn.getFixedInputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,or(i);case 4:return p=t.sent,t.abrupt("return",pr(p));case 6:case"end":return t.stop()}}),t)}))),ur.apply(this,arguments)}function ir(){return ir=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ar.getFixedOutputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Mn.getFixedOutputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,or(i);case 4:return p=t.sent,t.abrupt("return",pr(p));case 6:case"end":return t.stop()}}),t)}))),ir.apply(this,arguments)}function cr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:wt(t.route)}function pr(t){var e,n=t[0],r=cr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=cr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}var lr=(f(rr={},S.V1_1,ar),f(rr,S.V2,Mn),f(rr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ie(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return ur.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return ir.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(rr,"generateTxns",(function(t){return t.isUsingSwapRouter||t.pool.contractVersion!==S.V1_1?Mn.generateTxns(t):ar.generateTxns(t)})),f(rr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&function(t){if(t.type===exports.SwapQuoteType.Direct)return t.quoteWithPool.pool.contractVersion;return S.V2}(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return ar.signTxns(o({pool:e},t))}return Mn.signTxns(t)})),f(rr,"execute",(function(t){return t.contractVersion===S.V1_1?ar.execute(t):Mn.execute(t)})),rr);function dr(){return(dr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,fr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:st(a),confirmedRound:d,txnID:l,groupID:ot(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function fr(t){return mr.apply(this,arguments)}function mr(){return(mr=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function gr(){return gr=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:st(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,rt(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),gr.apply(this,arguments)}function xr(){return(xr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ir(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:j([it("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function Ar(){return(Ar=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Ir({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=L,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=In,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Me,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=F,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Gn,exports.Swap=lr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=hn,exports.V2AddLiquidityTxnIndices=qe,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=Qt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=de,exports.prepareCommitTransactions=function(t){return Ar.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return gr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return dr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=st,exports.tinymanContract_v1_1=Rt,exports.tinymanContract_v2=Ct,exports.tinymanJSSDKConfig=pt; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index 87d7ca2d..eaa3eb43 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -139,12 +139,9 @@ export interface GenerateSwapTxnsWithoutRouterParams { } export interface GenerateSwapRouterTxnsParams { client: Algodv2; - routerAppID: number; initiatorAddr: string; - assetIn: AssetWithIdAndAmount; - assetOut: AssetWithIdAndAmount; - intermediaryAssetID: number; swapType: SwapType; + route: SwapRoute; network: SupportedNetwork; } export declare type GenerateSwapTxnsParams = (GenerateSwapTxnsWithoutRouterParams & { diff --git a/dist/swap/v2/router/constants.d.ts b/dist/swap/v2/router/constants.d.ts new file mode 100644 index 00000000..f4fa2ddf --- /dev/null +++ b/dist/swap/v2/router/constants.d.ts @@ -0,0 +1,2 @@ +import { SupportedNetwork } from "../../../util/commonTypes"; +export declare const SWAP_ROUTER_APP_ID: Record; diff --git a/dist/swap/v2/router/swap-router.d.ts b/dist/swap/v2/router/swap-router.d.ts index 8c01a6f9..125e010f 100644 --- a/dist/swap/v2/router/swap-router.d.ts +++ b/dist/swap/v2/router/swap-router.d.ts @@ -9,7 +9,7 @@ export declare function generateSwapRouterAssetOptInTransaction({ client, router assetIDs: number[]; accountAddress: string; }): Promise; -export declare function generateSwapRouterTxns({ initiatorAddr, assetIn, assetOut, client, intermediaryAssetID, routerAppID, network, swapType }: GenerateSwapRouterTxnsParams): Promise<{ +export declare function generateSwapRouterTxns({ initiatorAddr, client, network, swapType, route }: GenerateSwapRouterTxnsParams): Promise<{ txn: algosdk.Transaction; signers: string[]; }[]>; diff --git a/dist/swap/v2/router/util.d.ts b/dist/swap/v2/router/util.d.ts index 4e996e84..6a4818e2 100644 --- a/dist/swap/v2/router/util.d.ts +++ b/dist/swap/v2/router/util.d.ts @@ -1,3 +1,5 @@ +import { SupportedNetwork } from "../../../util/commonTypes"; import { SwapRoute, SwapRouterQuote } from "../../types"; export declare function getSwapFeesFromSwapRoute(route: SwapRouterQuote[]): number; export declare function getSwapRouteRate(route: SwapRoute): number; +export declare function getSwapRouterAppID(network: SupportedNetwork): number; diff --git a/src/swap/types.ts b/src/swap/types.ts index 628ea501..7a586976 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -159,12 +159,9 @@ export interface GenerateSwapTxnsWithoutRouterParams { export interface GenerateSwapRouterTxnsParams { client: Algodv2; - routerAppID: number; initiatorAddr: string; - assetIn: AssetWithIdAndAmount; - assetOut: AssetWithIdAndAmount; - intermediaryAssetID: number; swapType: SwapType; + route: SwapRoute; network: SupportedNetwork; } diff --git a/src/swap/v2/router/constants.ts b/src/swap/v2/router/constants.ts new file mode 100644 index 00000000..3c654d79 --- /dev/null +++ b/src/swap/v2/router/constants.ts @@ -0,0 +1,7 @@ +import {SupportedNetwork} from "../../../util/commonTypes"; + +export const SWAP_ROUTER_APP_ID: Record = { + testnet: 159521633, + // TODO: Update this when the mainnet app is deployed + mainnet: 0 +}; diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index e3f0965b..ae5cf6fb 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -2,8 +2,7 @@ import algosdk, {ALGORAND_MIN_TX_FEE, getApplicationAddress} from "algosdk"; import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; import {CONTRACT_VERSION} from "../../../contract/constants"; -import {tinymanContract_v2} from "../../../contract/v2/contract"; -import {isAlgo} from "../../../util/asset/assetUtils"; +import {getAssetId, isAlgo} from "../../../util/asset/assetUtils"; import {SignerTransaction} from "../../../util/commonTypes"; import {getValidatorAppID} from "../../../validator"; import {SwapType} from "../../constants"; @@ -18,6 +17,7 @@ import { V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED, V2_SWAP_ROUTER_APP_ARGS_ENCODED } from "../constants"; +import {getSwapRouterAppID} from "./util"; export async function generateSwapRouterAssetOptInTransaction({ client, @@ -53,43 +53,40 @@ export async function generateSwapRouterAssetOptInTransaction({ export async function generateSwapRouterTxns({ initiatorAddr, - assetIn, - assetOut, client, - intermediaryAssetID, - routerAppID, network, - swapType + swapType, + route }: GenerateSwapRouterTxnsParams) { const suggestedParams = await client.getTransactionParams().do(); - const pool1LogicSig = tinymanContract_v2.generateLogicSigAccountForPool({ - asset1ID: assetIn.id, - asset2ID: intermediaryAssetID, - network - }); - const pool1Address = pool1LogicSig.address(); - const pool2LogicSig = tinymanContract_v2.generateLogicSigAccountForPool({ - asset1ID: intermediaryAssetID, - asset2ID: assetOut.id, - network - }); - const pool2Address = pool2LogicSig.address(); + const [assetInID, intermediaryAssetID, assetOutID] = [ + getAssetId(route[0].quote.amount_in.asset), + getAssetId(route[0].quote.amount_out.asset), + getAssetId(route[1].quote.amount_out.asset) + ]; + const [assetInAmount, assetOutAmount] = [ + Number(route[0].quote.amount_in.amount), + Number(route[route.length - 1].quote.amount_out.amount) + ]; + const [pool1Address, pool2Address] = [route[0].pool.address, route[1].pool.address]; + + const isAssetInAlgo = isAlgo(assetInID); - const isAssetInAlgo = isAlgo(assetIn.id); + const routerAppID = getSwapRouterAppID(network); const inputTxn = isAssetInAlgo ? algosdk.makePaymentTxnWithSuggestedParamsFromObject({ from: initiatorAddr, to: getApplicationAddress(routerAppID), - amount: assetIn.amount, + amount: assetInAmount, suggestedParams }) : algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ from: initiatorAddr, to: getApplicationAddress(routerAppID), - amount: assetIn.amount, - assetIndex: assetIn.id, + amount: assetInAmount, + assetIndex: assetInID, suggestedParams }); @@ -99,10 +96,10 @@ export async function generateSwapRouterTxns({ appArgs: [ V2_SWAP_APP_CALL_ARG_ENCODED, V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED[swapType], - algosdk.encodeUint64(assetOut.amount) + algosdk.encodeUint64(assetOutAmount) ], foreignApps: [getValidatorAppID(network, CONTRACT_VERSION.V2)], - foreignAssets: [assetIn.id, intermediaryAssetID, assetOut.id], + foreignAssets: [assetInID, intermediaryAssetID, assetOutID], accounts: [pool1Address, pool2Address], suggestedParams }); diff --git a/src/swap/v2/router/util.ts b/src/swap/v2/router/util.ts index 5588e876..7821986b 100644 --- a/src/swap/v2/router/util.ts +++ b/src/swap/v2/router/util.ts @@ -1,5 +1,7 @@ +import {SupportedNetwork} from "../../../util/commonTypes"; import {convertFromBaseUnits} from "../../../util/util"; import {SwapRoute, SwapRouterQuote} from "../../types"; +import {SWAP_ROUTER_APP_ID} from "./constants"; export function getSwapFeesFromSwapRoute(route: SwapRouterQuote[]): number { return route.reduce((acc, quote) => acc + Number(quote.swap_fees.amount), 0); @@ -16,3 +18,13 @@ export function getSwapRouteRate(route: SwapRoute) { convertFromBaseUnits(assetIn.asset.decimals, Number(assetIn.amount)) ); } + +export function getSwapRouterAppID(network: SupportedNetwork) { + const id = SWAP_ROUTER_APP_ID[network]; + + if (!id) { + throw new Error(`Unknown network or network not supported: ${network}`); + } + + return SWAP_ROUTER_APP_ID[network]; +} From 2dd9f2df12252659e373c7f965e797f09990e5bc Mon Sep 17 00:00:00 2001 From: anlerkan Date: Tue, 28 Feb 2023 15:22:06 +0300 Subject: [PATCH 07/39] refactor(swap-router): add swap router app opt in transactions on the sdk --- dist/index.js | 2 +- dist/swap/v2/router/swap-router.d.ts | 6 +----- src/swap/v2/router/swap-router.ts | 23 ++++++++++++++++++++++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/dist/index.js b/dist/index.js index ee80b331..7e52cde1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),a=Uint8Array.from([1]);function s(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E="- would result negative",N="logic eval error:",O="exceeds schema integer count",P=/transaction \w+:/,k=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function X(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[s?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function j(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=D(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var Q=100000n,U=100000n,q=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return Q+U*BigInt(o.length)+q*BigInt(s.length+a.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(nt(e),2),s=a[0],o=a[1],u=v(nt(Math.round(Number(et(s,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return at.apply(this,arguments)}function at(){return(at=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new k(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function st(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),Tt=it("swap"),yt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),vt={ASSET_OPT_IN:it("asset_opt_in")},_t={testnet:159521633,mainnet:0};function wt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function bt(t){if(!_t[t])throw new Error("Unknown network or network not supported: ".concat(t));return _t[t]}function Dt(){return(Dt=c(u().mark((function e(n){var a,s,o,i,c,p,l,d;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[vt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),d=r.default.assignGroupID([p]),e.abrupt("return",[{txn:d[0],signers:[i]}]);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function St(t){return Et.apply(this,arguments)}function Et(){return(Et=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(c[0].quote.amount_in.amount),Number(c[c.length-1].quote.amount_out.amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=bt(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[Tt,yt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p}),b=i===exports.SwapType.FixedInput?7:8,w.fee=t.ALGORAND_MIN_TX_FEE*(b+1),D=r.default.assignGroupID([_,w]),e.abrupt("return",[{txn:D[It.INPUT_TXN],signers:[a]},{txn:D[It.APP_CALL_TXN],signers:[a]}]);case 15:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nt(){return(Nt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ot(t){return Pt.apply(this,arguments)}function Pt(){return(Pt=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});case 5:return i=t.sent,t.abrupt("return",i.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var kt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Lt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Ft=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Rt=new(function(n){m(a,Ft);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:Qt(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function jt(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?X({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Qt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Ut,qt,Gt=it("e");function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=X({stateArray:A}),T=e.fromByteArray(j([r.default.decodeAddress(m).publicKey,Gt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(j([r.default.decodeAddress(m).publicKey,Gt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(j([r.default.decodeAddress(m).publicKey,Gt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Jt(){return(Jt=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=X({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mHt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ht,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ee(){return ee=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,Xt(n,r);case 6:return o=t.sent,i=jt(o,dt(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[Yt[S.V1_1].asset1],asset2ID:i[Yt[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ee.apply(this,arguments)}var ne=Object.freeze({__proto__:null,getPoolInfo:Zt,getPoolReserves:function(t,e){return te.apply(this,arguments)},getPoolAssets:function(t){return ee.apply(this,arguments)}});function re(t){return ae.apply(this,arguments)}function ae(){return(ae=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Vt(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(a,s),t.next=8,Xt(n,p);case 8:return d=t.sent,f=jt(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[Yt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Yt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Yt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Yt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Yt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Yt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Yt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Yt.v2.totalFeeShare]),m.poolTokenID=Number(f[Yt.v2.poolTokenID]),m.asset1ID=Number(f[Yt.v2.asset1]),m.asset2ID=Number(f[Yt.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function se(){return(se=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Xt(e,n.account.address());case 2:return r=t.sent,a=jt(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[Yt.v2.asset1Reserves]),s.asset2=BigInt(a[Yt.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[Yt.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oe(){return(oe=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,Xt(n,r);case 3:return s=t.sent,o=jt(s,dt(a,S.V2)),i=null,o&&(i={asset1ID:o[Yt[S.V2].asset1],asset2ID:o[Yt[S.V2].asset2],poolTokenID:o[Yt[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ue=Object.freeze({__proto__:null,getPoolInfo:re,getPoolReserves:function(t,e){return se.apply(this,arguments)},getPoolAssets:function(t){return oe.apply(this,arguments)}});function ie(t){return Boolean(t&&!(t.asset1+t.asset2))}var ce,pe,le=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ie(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:ie,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Zt(t),re(t)])}}),de=o((f(ce={},S.V1_1,o(o({},ne),le)),f(ce,S.V2,o(o({},ue),le)),ce),le);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(pe||(pe={}));var fe=96e4,me=859e3;function ge(){return(ge=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(a,S.V1_1),T=Rt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:F.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:xe(A),suggestedParams:c}),(E=[])[pe.FUNDING_TXN]=D,E[pe.VALIDATOR_APP_CALL]=_,E[pe.POOL_TOKEN_CREATE]=w,E[pe.ASSET1_OPT_IN]=b,A||(E[pe.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),O=[{txn:N[pe.FUNDING_TXN],signers:[i]},{txn:N[pe.VALIDATOR_APP_CALL],signers:[y]},{txn:N[pe.POOL_TOKEN_CREATE],signers:[y]},{txn:N[pe.ASSET1_OPT_IN],signers:[y]}],N[pe.ASSET2_OPT_IN]&&O.push({txn:N[pe.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xe(t){return t?fe:me}function Ie(){return(Ie=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Rt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===pe.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ae(t){return he.apply(this,arguments)}function he(){return(he=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,a[pe.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Te(){return(Te=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Ae({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",de.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ye,ve={generateTxns:function(t){return ge.apply(this,arguments)},signTxns:function(t){return Ie.apply(this,arguments)},execute:function(t){return Te.apply(this,arguments)},getBootstrapFundingTxnAmount:xe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(ye||(ye={}));var _e=5,we=6;function be(t,e){return De.apply(this,arguments)}function De(){return(De=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Se(t,e){return Ee.apply(this,arguments)}function Ee(){return(Ee=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,be(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ne(){return(Ne=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=dt(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,de.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Ct.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Pe(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Oe(h),suggestedParams:p}),(_=[])[ye.FUNDING_TXN]=y,_[ye.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[ye.FUNDING_TXN]={txn:w[ye.FUNDING_TXN],signers:[c]},b[ye.VALIDATOR_APP_CALL]={txn:w[ye.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Oe(t){return function(t,e){var n=Vt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Pe(t)+1e5}function Pe(e){return((e?_e:we)+1)*t.ALGORAND_MIN_TX_FEE}function ke(){return(ke=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Ct.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===ye.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Le(){return(Le=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,be(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Yt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",de.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new k(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Fe,Re={generateTxns:function(t){return Ne.apply(this,arguments)},signTxns:function(t){return ke.apply(this,arguments)},execute:function(t){return Le.apply(this,arguments)},getBootstrapFundingTxnAmount:Oe,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Oe(e)}};var Be,Me=(f(Fe={},S.V1_1,ve),f(Fe,S.V2,Re),f(Fe,"generateTxns",(function(t){return t.contractVersion===S.V1_1?ve.generateTxns(t):Re.generateTxns(t)})),f(Fe,"signTxns",(function(t){return t.contractVersion===S.V1_1?ve.signTxns(t):Re.signTxns(t)})),f(Fe,"execute",(function(t){return t.contractVersion===S.V1_1?ve.execute(t):Re.execute(t)})),f(Fe,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?ve.getBootstrapFundingTxnAmount(n):Re.getBootstrapFundingTxnAmount(n)})),Fe);exports.V1_1AddLiquidityTxnIndices=void 0,(Be=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Be.FEE_TXN=0]="FEE_TXN",Be[Be.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Be[Be.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Be[Be.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Be[Be.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Ce,Ve,Xe,je,Qe=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(je=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",je.FLEXIBLE="flexible",je.INITIAL="initial";var Ue,qe=(f(Ce={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Ce,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Ce,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Ce),Ge=(f(Ve={},exports.V2AddLiquidityType.INITIAL,1),f(Ve,exports.V2AddLiquidityType.SINGLE,2),f(Ve,exports.V2AddLiquidityType.FLEXIBLE,2),Ve),We=(f(Xe={},exports.V2AddLiquidityType.INITIAL,3),f(Xe,exports.V2AddLiquidityType.FLEXIBLE,3),f(Xe,exports.V2AddLiquidityType.SINGLE,2),Xe);function ze(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function Je(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ze({assetIn:r,assetOut:a}),o=Z(a.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(s/o-1))}function Ye(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,u=t.asset1Amount,i=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(u),d=s.asset2+BigInt(i),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(u)-x,h=BigInt(i)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=Ke(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=Ke(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:Je({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function Ke(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function He(e){return(Ge[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ze=it("add_liquidity"),$e=(f(Ue={},S.V1_1,[it("mint")]),f(Ue,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ze,it("single")],FLEXIBLE_MODE:[Ze,it("flexible")]}),Ue);function tn(){return(tn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(s,S.V1_1),appArgs:$e.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(){return(en=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nn(){return(nn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Wt({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=st(a),x=ot(a),t.next=17,Wt({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new k(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var rn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Sn(){return(Sn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!e.isUsingSwapRouter){t.next=2;break}return t.abrupt("return",St(e));case 2:if(n=e.assetIn,a=e.assetOut,s=e.client,o=e.initiatorAddr,i=e.pool,c=e.slippage,p=e.swapType,l=i.account.address(),(d=[i.asset1ID,i.asset2ID]).includes(n.id)&&d.includes(a.id)&&n.id!==a.id){t.next=7;break}throw new k({pool:i,assetIn:n,assetOut:a},"Input asset (#".concat(n.id,") and output asset (#").concat(a.id,") provided to generate transactions do not belong to the pool ").concat(l,"."));case 7:return t.next=9,s.getTransactionParams().do();case 9:return f=t.sent,m=C(n.id),g=p===exports.SwapType.FixedInput?n.amount:H("positive",c,n.amount),x=p===exports.SwapType.FixedOutput?a.amount:H("negative",c,a.amount),I=m?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,assetIndex:n.id,suggestedParams:f}),(A=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:i.validatorAppID,appArgs:[Tt,yt[p],r.default.encodeUint64(x)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[l],foreignAssets:[i.asset1ID,i.asset2ID],suggestedParams:f})).fee=En(p),(h=[])[It.INPUT_TXN]=I,h[It.APP_CALL_TXN]=A,T=r.default.assignGroupID(h),t.abrupt("return",[{txn:T[It.INPUT_TXN],signers:[o]},{txn:T[It.APP_CALL_TXN],signers:[o]}]);case 21:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(e){return(ht[e]+1)*t.ALGORAND_MIN_TX_FEE}function Nn(){return(Nn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,c=e.assetIn,t.next=3,rt(a,[i]);case 3:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=10,Se(a,o);case 10:return g=t.sent,x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.route[s.route.length-1].quote.amount_out.asset.id),I=null==g||null===(n=g.find((function(t){return t.txn.txn.xaid===c.id})))||void 0===n?void 0:n.txn.txn,A=null==g||null===(r=g.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:f,assetIn:I&&{amount:BigInt(c.amount)-BigInt(I.aamt||0),id:c.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:m});case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function On(){return(On=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.asset,r=e.decimals,a=e.pool,s=e.type,o=e.isSwapRouterEnabled,s!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,Pn({pool:a,assetIn:n,decimals:r,isSwapRouterEnabled:o});case 4:i=t.sent,t.next=10;break;case 7:return t.next=9,Ln({pool:a,assetOut:n,decimals:r,isSwapRouterEnabled:o});case 9:i=t.sent;case 10:return t.abrupt("return",i);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(t){return kn.apply(this,arguments)}function kn(){return(kn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetIn,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:n,assetIn:r},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(r.amount),c=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}p=n.asset2ID,l=n.asset1Reserves,d=n.asset2Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}p=n.asset1ID,l=n.asset2Reserves,d=n.asset1Reserves,t.next=18;break;case 17:throw new k({pool:n,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(f=Rn({inputSupply:l,outputSupply:d,swapInputAmount:i,totalFeeShare:c,decimals:a}),m=f.swapOutputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(m>d)){t.next=21;break}throw new Dn;case 21:if(I={assetInID:r.id,assetInAmount:i,assetOutID:p,assetOutAmount:m,swapFee:Number(g),rate:Z(a.assetOut,Number(m))/Z(a.assetIn,Number(i)),priceImpact:x},!s){t.next=28;break}return t.next=25,Ot({amount:r.amount,assetInID:Number(r.id),assetOutID:p,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_out.amount)>I.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},A),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:n,quote:I},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ln(t){return Fn.apply(this,arguments)}function Fn(){return(Fn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetOut,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:n,assetOut:r},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(r.amount),c=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}p=n.asset2ID,l=n.asset2Reserves,d=n.asset1Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}p=n.asset1ID,l=n.asset1Reserves,d=n.asset2Reserves,t.next=18;break;case 17:throw new k({pool:n,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(f=Bn({inputSupply:l,outputSupply:d,swapOutputAmount:i,totalFeeShare:c,decimals:a}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(i>d)){t.next=21;break}throw new Dn;case 21:if(I={assetInID:p,assetInAmount:m,assetOutID:r.id,assetOutAmount:i,swapFee:Number(g),rate:Z(a.assetOut,Number(i))/Z(a.assetIn,Number(m)),priceImpact:x},!s){t.next=28;break}return t.next=25,Ot({amount:r.amount,assetInID:p,assetOutID:r.id,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Vn(){return(Vn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Tn,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[An.ASSET_TRANSFER_TXN]=g,I[An.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[An.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[An.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xn(){return(Xn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Tn,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[An.ASSET_TRANSFER_TXN]=T,v[An.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[An.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[An.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jn(){return(jn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,rt(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Se(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qn,Un,qn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Cn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=Cn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Mn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Mn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Vn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Xn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return jn.apply(this,arguments)}},Gn=(f(Qn={},S.V1_1,bn),f(Qn,S.V2,qn),Qn),Wn=3n,zn=1000n;function Jn(){return(Jn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===Un.FEE_TXN_INDEX?c:e===Un.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yn(){return(Yn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.assetIn,c=e.assetOut,p=e.slippage,l=e.initiatorAddr,d=s.account.address(),(f=[s.asset1ID,s.asset2ID]).includes(i.id)&&f.includes(c.id)&&i.id!==c.id){t.next=5;break}throw new k({pool:s,assetIn:i,assetOut:c},"Input asset (#".concat(i.id,") and output asset (#").concat(c.id,") provided to generate transactions do not belong to the pool ").concat(d,"."));case 5:return t.next=7,n.getTransactionParams().do();case 7:return m=t.sent,g=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],x=r.default.makeApplicationNoOpTxnFromObject({from:d,appIndex:s.validatorAppID,appArgs:g,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:m}),I=o===exports.SwapType.FixedOutput?H("positive",p,i.amount):i.amount,A=0===i.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:i.id,amount:I,suggestedParams:m}),h=o===exports.SwapType.FixedInput?H("negative",p,c.amount):c.amount,T=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:l,amount:h,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:l,assetIndex:c.id,amount:h,suggestedParams:m}),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.fee+T.fee,note:a,suggestedParams:m}),v=r.default.assignGroupID([y,x,A,T]),t.abrupt("return",[{txn:v[0],signers:[l]},{txn:v[1],signers:[d]},{txn:v[2],signers:[l]},{txn:v[3],signers:[d]}]);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Kn(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new k({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new k({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*Wn/zn,l=i-u*i/(u+(c-p));if(l>i)throw new Dn;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:ze(d),priceImpact:Je(o({inputSupply:u,outputSupply:i},d))}}function Hn(t){return Zn.apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Wt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Wt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new k({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new k({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new Dn;var c=o*u/(u-i)-o,p=c*zn/(zn-Wn),l=p-c,d=Z(a.assetOut,Number(i))/Z(a.assetIn,Number(p)),f=Z(a.assetOut,Number(u))/Z(a.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Wt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Wt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Un.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Un.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Un.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Un.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,Hn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,tr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(s),fees:st(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new k(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Un||(Un={}));var rr,ar={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Kn({pool:e,reserves:n,assetIn:r,decimals:a}):$n({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Kn,getFixedOutputSwapQuote:$n,generateTxns:function(t){return Yn.apply(this,arguments)},signTxns:function(t){return Jn.apply(this,arguments)},execute:function(t){return nr.apply(this,arguments)},executeFixedOutputSwap:tr},sr=4*t.ALGORAND_MIN_TX_FEE;function or(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Dn})))throw new Dn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function ur(){return ur=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ar.getFixedInputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Mn.getFixedInputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,or(i);case 4:return p=t.sent,t.abrupt("return",pr(p));case 6:case"end":return t.stop()}}),t)}))),ur.apply(this,arguments)}function ir(){return ir=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ar.getFixedOutputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Mn.getFixedOutputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,or(i);case 4:return p=t.sent,t.abrupt("return",pr(p));case 6:case"end":return t.stop()}}),t)}))),ir.apply(this,arguments)}function cr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:wt(t.route)}function pr(t){var e,n=t[0],r=cr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=cr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}var lr=(f(rr={},S.V1_1,ar),f(rr,S.V2,Mn),f(rr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ie(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return ur.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return ir.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(rr,"generateTxns",(function(t){return t.isUsingSwapRouter||t.pool.contractVersion!==S.V1_1?Mn.generateTxns(t):ar.generateTxns(t)})),f(rr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&function(t){if(t.type===exports.SwapQuoteType.Direct)return t.quoteWithPool.pool.contractVersion;return S.V2}(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return ar.signTxns(o({pool:e},t))}return Mn.signTxns(t)})),f(rr,"execute",(function(t){return t.contractVersion===S.V1_1?ar.execute(t):Mn.execute(t)})),rr);function dr(){return(dr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,fr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:st(a),confirmedRound:d,txnID:l,groupID:ot(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function fr(t){return mr.apply(this,arguments)}function mr(){return(mr=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function gr(){return gr=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:st(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,rt(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),gr.apply(this,arguments)}function xr(){return(xr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ir(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:j([it("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function Ar(){return(Ar=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Ir({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=L,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=In,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Me,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=F,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Gn,exports.Swap=lr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=hn,exports.V2AddLiquidityTxnIndices=qe,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=Qt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=de,exports.prepareCommitTransactions=function(t){return Ar.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return gr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return dr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=st,exports.tinymanContract_v1_1=Rt,exports.tinymanContract_v2=Ct,exports.tinymanJSSDKConfig=pt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",s=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,s=Object.create(a.prototype),o=new w(r||[]);return s._invoke=function(t,e,n){var r="suspendedStart";return function(a,s){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw s;return D()}for(n.method=a,n.arg=s;;){var o=n.delegate;if(o){var u=y(o,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,o),s}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,a)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,s,o,u){var i=p(t[a],t,s);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,u)}),(function(t){r("throw",t,o,u)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function s(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(s,s):s()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function w(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,s=function e(){for(;++r=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E="- would result negative",N="logic eval error:",O="exceeds schema integer count",P=/transaction \w+:/,k=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function X(t,e){return j.apply(this,arguments)}function j(){return(j=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Q(t,e){return U.apply(this,arguments)}function U(){return(U=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,X(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function q(){for(var e=arguments.length,n=new Array(e),r=0;r1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function rt(t,e){var n=Number(t);return st({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function at(t,e){return st({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function st(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(ut(e),2),s=a[0],o=a[1],u=v(ut(Math.round(Number(ot(s,o+r)))),2),i=u[0],c=u[1];return Number(ot(i,c-r))}return Math.round(e)}function ot(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ut(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function it(t,e){return ct.apply(this,arguments)}function ct(){return(ct=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,tt(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new k(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function pt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function lt(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function dt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ft(t){return(new TextEncoder).encode(t)}var mt,gt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return ft("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),xt=(f(mt={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(mt,S.V2,{testnet:148607e3,mainnet:1002541853}),mt);function It(t,e){var n=xt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:It(a,s),note:gt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht,Tt,yt,vt;function _t(){return(_t=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:It(a,s),note:gt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(ht=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",ht.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(vt||(vt={}));var wt=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),bt=ft("swap"),Dt=(f(yt={},exports.SwapType.FixedInput,ft("fixed-input")),f(yt,exports.SwapType.FixedOutput,ft("fixed-output")),yt),St={ASSET_OPT_IN:ft("asset_opt_in")},Et={testnet:159521633,mainnet:0};function Nt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return rt(r.asset.decimals,Number(r.amount))/rt(n.asset.decimals,Number(n.amount))}function Ot(t){if(!Et[t])throw new Error("Unknown network or network not supported: ".concat(t));return Et[t]}function Pt(t){return kt.apply(this,arguments)}function kt(){return(kt=c(u().mark((function e(n){var a,s,o,i,c,p,l,d;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[St.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),d=r.default.assignGroupID([p]),e.abrupt("return",[{txn:d[0],signers:[i]}]);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(c[0].quote.amount_in.amount),Number(c[c.length-1].quote.amount_out.amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Ot(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[bt,Dt[i],r.default.encodeUint64(I)],foreignApps:[It(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p}),b=i===exports.SwapType.FixedInput?7:8,w.fee=t.ALGORAND_MIN_TX_FEE*(b+1),D=r.default.assignGroupID([_,w]),E=[{txn:D[vt.INPUT_TXN],signers:[a]},{txn:D[vt.APP_CALL_TXN],signers:[a]}],e.next=17,Rt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 17:if(!(e.sent.length>0)){e.next=25;break}return e.t0=q,e.next=22,Pt({client:s,accountAddress:a,assetIDs:[d,f,m],routerAppID:v});case 22:e.t1=e.sent,e.t2=E,E=(0,e.t0)(e.t1,e.t2);case 25:return e.abrupt("return",E);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Rt(t){return Bt.apply(this,arguments)}function Bt(){return(Bt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});case 5:return i=t.sent,t.abrupt("return",i.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Vt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Xt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},jt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Qt=new(function(n){m(a,jt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=It(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:Yt(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Jt(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?G({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Yt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Kt,Ht,Zt=ft("e");function $t(t){return te.apply(this,arguments)}function te(){return(te=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=G({stateArray:A}),T=e.fromByteArray(W([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(W([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(W([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ee(){return(ee=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=G({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mae)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ae,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ie(){return ie=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,zt(n,r);case 6:return o=t.sent,i=Jt(o,It(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[ne[S.V1_1].asset1],asset2ID:i[ne[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ie.apply(this,arguments)}var ce=Object.freeze({__proto__:null,getPoolInfo:se,getPoolReserves:function(t,e){return ue.apply(this,arguments)},getPoolAssets:function(t){return ie.apply(this,arguments)}});function pe(t){return le.apply(this,arguments)}function le(){return(le=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Wt(S.V2),i=o.generateLogicSigAccountForPool(e),c=It(r,S.V2),p=i.address(),l=M(a,s),t.next=8,zt(n,p);case 8:return d=t.sent,f=Jt(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[ne.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ne.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ne.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ne.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ne.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ne.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ne.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ne.v2.totalFeeShare]),m.poolTokenID=Number(f[ne.v2.poolTokenID]),m.asset1ID=Number(f[ne.v2.asset1]),m.asset2ID=Number(f[ne.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function de(){return(de=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,zt(e,n.account.address());case 2:return r=t.sent,a=Jt(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[ne.v2.asset1Reserves]),s.asset2=BigInt(a[ne.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[ne.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(){return(fe=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,zt(n,r);case 3:return s=t.sent,o=Jt(s,It(a,S.V2)),i=null,o&&(i={asset1ID:o[ne[S.V2].asset1],asset2ID:o[ne[S.V2].asset2],poolTokenID:o[ne[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var me=Object.freeze({__proto__:null,getPoolInfo:pe,getPoolReserves:function(t,e){return de.apply(this,arguments)},getPoolAssets:function(t){return fe.apply(this,arguments)}});function ge(t){return Boolean(t&&!(t.asset1+t.asset2))}var xe,Ie,Ae=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ge(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=rt(t.asset1,e.asset1)/rt(t.asset2,e.asset2)),r},isPoolEmpty:ge,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([se(t),pe(t)])}}),he=o((f(xe={},S.V1_1,o(o({},ce),Ae)),f(xe,S.V2,o(o({},me),Ae)),xe),Ae);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Ie||(Ie={}));var Te=96e4,ye=859e3;function ve(){return(ve=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=It(a,S.V1_1),T=Qt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:gt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[ft("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:F.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:_e(A),suggestedParams:c}),(E=[])[Ie.FUNDING_TXN]=D,E[Ie.VALIDATOR_APP_CALL]=_,E[Ie.POOL_TOKEN_CREATE]=w,E[Ie.ASSET1_OPT_IN]=b,A||(E[Ie.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),O=[{txn:N[Ie.FUNDING_TXN],signers:[i]},{txn:N[Ie.VALIDATOR_APP_CALL],signers:[y]},{txn:N[Ie.POOL_TOKEN_CREATE],signers:[y]},{txn:N[Ie.ASSET1_OPT_IN],signers:[y]}],N[Ie.ASSET2_OPT_IN]&&O.push({txn:N[Ie.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(t){return t?Te:ye}function we(){return(we=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Qt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ie.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function be(t){return De.apply(this,arguments)}function De(){return(De=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,tt(n,a[Ie.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Se(){return(Se=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,be({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",he.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ee,Ne={generateTxns:function(t){return ve.apply(this,arguments)},signTxns:function(t){return we.apply(this,arguments)},execute:function(t){return Se.apply(this,arguments)},getBootstrapFundingTxnAmount:_e};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ee||(Ee={}));var Oe=5,Pe=6;function ke(){return(ke=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=It(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,he.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Gt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[ft("bootstrap")],note:gt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Fe(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Le(h),suggestedParams:p}),(_=[])[Ee.FUNDING_TXN]=y,_[Ee.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Ee.FUNDING_TXN]={txn:w[Ee.FUNDING_TXN],signers:[c]},b[Ee.VALIDATOR_APP_CALL]={txn:w[Ee.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Le(t){return function(t,e){var n=Wt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Fe(t)+1e5}function Fe(e){return((e?Oe:Pe)+1)*t.ALGORAND_MIN_TX_FEE}function Re(){return(Re=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Gt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ee.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(){return(Be=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,X(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ne.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",he.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new k(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Me,Ce={generateTxns:function(t){return ke.apply(this,arguments)},signTxns:function(t){return Re.apply(this,arguments)},execute:function(t){return Be.apply(this,arguments)},getBootstrapFundingTxnAmount:Le,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Le(e)}};var Ve,Xe=(f(Me={},S.V1_1,Ne),f(Me,S.V2,Ce),f(Me,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Ne.generateTxns(t):Ce.generateTxns(t)})),f(Me,"signTxns",(function(t){return t.contractVersion===S.V1_1?Ne.signTxns(t):Ce.signTxns(t)})),f(Me,"execute",(function(t){return t.contractVersion===S.V1_1?Ne.execute(t):Ce.execute(t)})),f(Me,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Ne.getBootstrapFundingTxnAmount(n):Ce.getBootstrapFundingTxnAmount(n)})),Me);exports.V1_1AddLiquidityTxnIndices=void 0,(Ve=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ve.FEE_TXN=0]="FEE_TXN",Ve[Ve.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ve[Ve.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ve[Ve.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ve[Ve.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var je,Qe,Ue,qe,Ge=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(qe=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",qe.FLEXIBLE="flexible",qe.INITIAL="initial";var We,ze=(f(je={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(je,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(je,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),je),Je=(f(Qe={},exports.V2AddLiquidityType.INITIAL,1),f(Qe,exports.V2AddLiquidityType.SINGLE,2),f(Qe,exports.V2AddLiquidityType.FLEXIBLE,2),Qe),Ye=(f(Ue={},exports.V2AddLiquidityType.INITIAL,3),f(Ue,exports.V2AddLiquidityType.FLEXIBLE,3),f(Ue,exports.V2AddLiquidityType.SINGLE,2),Ue);function Ke(t){var e=t.assetIn,n=t.assetOut;return rt(n.decimals,Number(n.amount))/rt(e.decimals,Number(e.amount))}function He(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=Ke({assetIn:r,assetOut:a}),o=rt(a.decimals,Number(n))/rt(r.decimals,Number(e));return st({decimalPlaces:5},Math.abs(s/o-1))}function Ze(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,u=t.asset1Amount,i=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(u),d=s.asset2+BigInt(i),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(u)-x,h=BigInt(i)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=$e(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=$e(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:He({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function $e(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function tn(e){return(Je[e]+1)*t.ALGORAND_MIN_TX_FEE}var en=ft("add_liquidity"),nn=(f(We={},S.V1_1,[ft("mint")]),f(We,S.V2,{INITIAL_LIQUIDITY:[ft("add_initial_liquidity")],SINGLE_ASSET_MODE:[en,ft("single")],FLEXIBLE_MODE:[en,ft("flexible")]}),We);function rn(){return(rn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=nt("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:It(s,S.V1_1),appArgs:nn.v1_1,accounts:[d],note:gt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function an(){return(an=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sn(){return(sn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,$t({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,it(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=pt(a),x=lt(a),t.next=17,$t({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new k(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var on=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function On(){return(On=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!e.isUsingSwapRouter){t.next=2;break}return t.abrupt("return",Lt(e));case 2:if(n=e.assetIn,a=e.assetOut,s=e.client,o=e.initiatorAddr,i=e.pool,c=e.slippage,p=e.swapType,l=i.account.address(),(d=[i.asset1ID,i.asset2ID]).includes(n.id)&&d.includes(a.id)&&n.id!==a.id){t.next=7;break}throw new k({pool:i,assetIn:n,assetOut:a},"Input asset (#".concat(n.id,") and output asset (#").concat(a.id,") provided to generate transactions do not belong to the pool ").concat(l,"."));case 7:return t.next=9,s.getTransactionParams().do();case 9:return f=t.sent,m=C(n.id),g=p===exports.SwapType.FixedInput?n.amount:nt("positive",c,n.amount),x=p===exports.SwapType.FixedOutput?a.amount:nt("negative",c,a.amount),I=m?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,assetIndex:n.id,suggestedParams:f}),(A=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:i.validatorAppID,appArgs:[bt,Dt[p],r.default.encodeUint64(x)],note:gt.getAppCallTxnNoteWithClientName(S.V2),accounts:[l],foreignAssets:[i.asset1ID,i.asset2ID],suggestedParams:f})).fee=Pn(p),(h=[])[vt.INPUT_TXN]=I,h[vt.APP_CALL_TXN]=A,T=r.default.assignGroupID(h),t.abrupt("return",[{txn:T[vt.INPUT_TXN],signers:[o]},{txn:T[vt.APP_CALL_TXN],signers:[o]}]);case 21:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(e){return(wt[e]+1)*t.ALGORAND_MIN_TX_FEE}function kn(){return(kn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,c=e.assetIn,t.next=3,it(a,[i]);case 3:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=10,Q(a,o);case 10:return g=t.sent,x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.route[s.route.length-1].quote.amount_out.asset.id),I=null==g||null===(n=g.find((function(t){return t.txn.txn.xaid===c.id})))||void 0===n?void 0:n.txn.txn,A=null==g||null===(r=g.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:f,assetIn:I&&{amount:BigInt(c.amount)-BigInt(I.aamt||0),id:c.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:m});case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ln(){return(Ln=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.asset,r=e.decimals,a=e.pool,s=e.type,o=e.isSwapRouterEnabled,s!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,Fn({pool:a,assetIn:n,decimals:r,isSwapRouterEnabled:o});case 4:i=t.sent,t.next=10;break;case 7:return t.next=9,Bn({pool:a,assetOut:n,decimals:r,isSwapRouterEnabled:o});case 9:i=t.sent;case 10:return t.abrupt("return",i);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Fn(t){return Rn.apply(this,arguments)}function Rn(){return(Rn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetIn,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:n,assetIn:r},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(r.amount),c=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}p=n.asset2ID,l=n.asset1Reserves,d=n.asset2Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}p=n.asset1ID,l=n.asset2Reserves,d=n.asset1Reserves,t.next=18;break;case 17:throw new k({pool:n,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(f=Cn({inputSupply:l,outputSupply:d,swapInputAmount:i,totalFeeShare:c,decimals:a}),m=f.swapOutputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(m>d)){t.next=21;break}throw new Nn;case 21:if(I={assetInID:r.id,assetInAmount:i,assetOutID:p,assetOutAmount:m,swapFee:Number(g),rate:rt(a.assetOut,Number(m))/rt(a.assetIn,Number(i)),priceImpact:x},!s){t.next=28;break}return t.next=25,Mt({amount:r.amount,assetInID:Number(r.id),assetOutID:p,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_out.amount)>I.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},A),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:n,quote:I},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Bn(t){return Mn.apply(this,arguments)}function Mn(){return(Mn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetOut,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:n,assetOut:r},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(r.amount),c=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}p=n.asset2ID,l=n.asset2Reserves,d=n.asset1Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}p=n.asset1ID,l=n.asset1Reserves,d=n.asset2Reserves,t.next=18;break;case 17:throw new k({pool:n,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(f=Vn({inputSupply:l,outputSupply:d,swapOutputAmount:i,totalFeeShare:c,decimals:a}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(i>d)){t.next=21;break}throw new Nn;case 21:if(I={assetInID:p,assetInAmount:m,assetOutID:r.id,assetOutAmount:i,swapFee:Number(g),rate:rt(a.assetOut,Number(i))/rt(a.assetIn,Number(m)),priceImpact:x},!s){t.next=28;break}return t.next=25,Mt({amount:r.amount,assetInID:p,assetOutID:r.id,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Qn(){return(Qn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:gt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(nt("negative",l,c)),r.default.encodeUint64(nt("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[yn.ASSET_TRANSFER_TXN]=g,I[yn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[yn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[yn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Un(){return(Un=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=nt("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:gt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[yn.ASSET_TRANSFER_TXN]=T,v[yn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[yn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[yn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qn(){return(qn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,it(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Q(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Gn,Wn,zn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=jn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=jn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Xn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Xn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Qn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Un.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return qn.apply(this,arguments)}},Jn=(f(Gn={},S.V1_1,En),f(Gn,S.V2,zn),Gn),Yn=3n,Kn=1000n;function Hn(){return(Hn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===Wn.FEE_TXN_INDEX?c:e===Wn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.assetIn,c=e.assetOut,p=e.slippage,l=e.initiatorAddr,d=s.account.address(),(f=[s.asset1ID,s.asset2ID]).includes(i.id)&&f.includes(c.id)&&i.id!==c.id){t.next=5;break}throw new k({pool:s,assetIn:i,assetOut:c},"Input asset (#".concat(i.id,") and output asset (#").concat(c.id,") provided to generate transactions do not belong to the pool ").concat(d,"."));case 5:return t.next=7,n.getTransactionParams().do();case 7:return m=t.sent,g=[ft("swap"),o===exports.SwapType.FixedInput?ft("fi"):ft("fo")],x=r.default.makeApplicationNoOpTxnFromObject({from:d,appIndex:s.validatorAppID,appArgs:g,accounts:[l],note:gt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:m}),I=o===exports.SwapType.FixedOutput?nt("positive",p,i.amount):i.amount,A=0===i.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:i.id,amount:I,suggestedParams:m}),h=o===exports.SwapType.FixedInput?nt("negative",p,c.amount):c.amount,T=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:l,amount:h,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:l,assetIndex:c.id,amount:h,suggestedParams:m}),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.fee+T.fee,note:a,suggestedParams:m}),v=r.default.assignGroupID([y,x,A,T]),t.abrupt("return",[{txn:v[0],signers:[l]},{txn:v[1],signers:[d]},{txn:v[2],signers:[l]},{txn:v[3],signers:[d]}]);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new k({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new k({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*Yn/Kn,l=i-u*i/(u+(c-p));if(l>i)throw new Nn;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:Ke(d),priceImpact:He(o({inputSupply:u,outputSupply:i},d))}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,it(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new k({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new k({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new Nn;var c=o*u/(u-i)-o,p=c*Kn/(Kn-Yn),l=p-c,d=rt(a.assetOut,Number(i))/rt(a.assetIn,Number(p)),f=rt(a.assetOut,Number(u))/rt(a.assetIn,Number(o)),m=st({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m}}function rr(t){return ar.apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,it(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sr(){return(sr=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Wn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Wn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,tr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,rr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:lt(s),fees:pt(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new k(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Wn||(Wn={}));var or,ur={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?$n({pool:e,reserves:n,assetIn:r,decimals:a}):nr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:$n,getFixedOutputSwapQuote:nr,generateTxns:function(t){return Zn.apply(this,arguments)},signTxns:function(t){return Hn.apply(this,arguments)},execute:function(t){return sr.apply(this,arguments)},executeFixedOutputSwap:rr},ir=4*t.ALGORAND_MIN_TX_FEE;function cr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Nn})))throw new Nn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function pr(){return pr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedInputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Xn.getFixedInputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),pr.apply(this,arguments)}function lr(){return lr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedOutputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Xn.getFixedOutputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),lr.apply(this,arguments)}function dr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:Nt(t.route)}function fr(t){var e,n=t[0],r=dr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=dr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}var mr=(f(or={},S.V1_1,ur),f(or,S.V2,Xn),f(or,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ge(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return pr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return lr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(or,"generateTxns",(function(t){return t.isUsingSwapRouter||t.pool.contractVersion!==S.V1_1?Xn.generateTxns(t):ur.generateTxns(t)})),f(or,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&function(t){if(t.type===exports.SwapQuoteType.Direct)return t.quoteWithPool.pool.contractVersion;return S.V2}(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return ur.signTxns(o({pool:e},t))}return Xn.signTxns(t)})),f(or,"execute",(function(t){return t.contractVersion===S.V1_1?ur.execute(t):Xn.execute(t)})),or);function gr(){return(gr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,xr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,it(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:pt(a),confirmedRound:d,txnID:l,groupID:lt(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function xr(t){return Ir.apply(this,arguments)}function Ir(){return(Ir=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(){return Ar=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:pt(e),groupID:lt(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,it(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Ar.apply(this,arguments)}function hr(){return(hr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[ft("redeem")],note:gt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Tr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ft("commit"),i],note:W([ft("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function yr(){return(yr=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Tr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[ft("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=L,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Tn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Xe,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=F,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Jn,exports.Swap=mr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=vn,exports.V2AddLiquidityTxnIndices=ze,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=nt,exports.calculateAccountMinimumRequiredBalance=Yt,exports.combineAndRegroupSignerTxns=q,exports.convertFromBaseUnits=rt,exports.convertToBaseUnits=at,exports.generateOptIntoAssetTxns=function(t){return R.apply(this,arguments)},exports.generateOptIntoValidatorTxns=function(t){return At.apply(this,arguments)},exports.generateOptOutOfValidatorTxns=function(t){return _t.apply(this,arguments)},exports.generateRedeemTxns=function(t){return hr.apply(this,arguments)},exports.generateSwapRouterAssetOptInTransaction=Pt,exports.generateSwapRouterTxns=Lt,exports.getAccountExcess=function(t){return ee.apply(this,arguments)},exports.getAccountExcessWithinPool=$t,exports.getAccountInformation=zt,exports.getAddLiquidityTotalFee=function(e){switch(e.version){case S.V1_1:return Ge;case S.V2:return n=e.type,(Je[n]+Ye[n])*t.ALGORAND_MIN_TX_FEE;default:throw new Error("Provided contract version was not valid.")}var n},exports.getMinRequiredBalanceToOptIn=function(t){var e,n=t.currentMinumumBalanceForAccount,r=t.suggestedTransactionFee;if("app-opt-in"===t.type){var a=Wt(t.contractVersion);e=1e5+5e4*a.schema.numLocalByteSlices+28500*a.schema.numLocalInts}else e=1e5;return e+(n||0)+(r||0)},exports.getStakingAppID=function(t){return"testnet"===t?51948952:649588853},exports.getSwapFeesFromSwapRoute=function(t){return t.reduce((function(t,e){return t+Number(e.swap_fees.amount)}),0)},exports.getSwapRoute=Mt,exports.getSwapRouteRate=Nt,exports.getSwapRouterAppID=Ot,exports.getSwapRouterAppOptInRequiredAssetIDs=Rt,exports.getSwapTotalFee=function(e){switch(e.version){case S.V1_1:return ir;case S.V2:return n=e.type,(wt[n]+2)*t.ALGORAND_MIN_TX_FEE;default:throw new Error("Provided contract version was not valid.")}var n},exports.getTxnGroupID=lt,exports.getValidatorAppID=It,exports.hasSufficientMinimumBalance=function(t){return t.amount>=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=he,exports.prepareCommitTransactions=function(t){return yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Ar.apply(this,arguments)},exports.redeemExcessAsset=function(t){return gr.apply(this,arguments)},exports.sendAndWaitRawTransaction=it,exports.sumUpTxnFees=pt,exports.tinymanContract_v1_1=Qt,exports.tinymanContract_v2=Gt,exports.tinymanJSSDKConfig=gt; diff --git a/dist/swap/v2/router/swap-router.d.ts b/dist/swap/v2/router/swap-router.d.ts index 125e010f..efaecc07 100644 --- a/dist/swap/v2/router/swap-router.d.ts +++ b/dist/swap/v2/router/swap-router.d.ts @@ -1,4 +1,3 @@ -import algosdk from "algosdk"; import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; import { SignerTransaction } from "../../../util/commonTypes"; import { SwapType } from "../../constants"; @@ -9,10 +8,7 @@ export declare function generateSwapRouterAssetOptInTransaction({ client, router assetIDs: number[]; accountAddress: string; }): Promise; -export declare function generateSwapRouterTxns({ initiatorAddr, client, network, swapType, route }: GenerateSwapRouterTxnsParams): Promise<{ - txn: algosdk.Transaction; - signers: string[]; -}[]>; +export declare function generateSwapRouterTxns({ initiatorAddr, client, network, swapType, route }: GenerateSwapRouterTxnsParams): Promise; export declare function getSwapRouterAppOptInRequiredAssetIDs({ client, routerAppID, assetIDs }: { client: AlgodClient; routerAppID: number; diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index ae5cf6fb..70bf76e4 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -4,6 +4,7 @@ import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; import {CONTRACT_VERSION} from "../../../contract/constants"; import {getAssetId, isAlgo} from "../../../util/asset/assetUtils"; import {SignerTransaction} from "../../../util/commonTypes"; +import {combineAndRegroupSignerTxns} from "../../../util/transaction/transactionUtils"; import {getValidatorAppID} from "../../../validator"; import {SwapType} from "../../constants"; import { @@ -110,7 +111,7 @@ export async function generateSwapRouterTxns({ const txGroup = algosdk.assignGroupID([inputTxn, routerAppCallTxn]); - return [ + let txns: SignerTransaction[] = [ { txn: txGroup[V2SwapTxnGroupIndices.INPUT_TXN], signers: [initiatorAddr] @@ -120,6 +121,26 @@ export async function generateSwapRouterTxns({ signers: [initiatorAddr] } ]; + + const swapRouterAppOptInRequiredAssetIDs = await getSwapRouterAppOptInRequiredAssetIDs({ + client, + routerAppID, + assetIDs: [assetInID, intermediaryAssetID, assetOutID] + }); + + if (swapRouterAppOptInRequiredAssetIDs.length > 0) { + txns = combineAndRegroupSignerTxns( + await generateSwapRouterAssetOptInTransaction({ + client, + accountAddress: initiatorAddr, + assetIDs: [assetInID, intermediaryAssetID, assetOutID], + routerAppID + }), + txns + ); + } + + return txns; } export async function getSwapRouterAppOptInRequiredAssetIDs({ From d91dc2783fd85ef6079cd2389507e8410981ea8d Mon Sep 17 00:00:00 2001 From: anlerkan Date: Tue, 28 Feb 2023 16:40:30 +0300 Subject: [PATCH 08/39] chore(swap-router): update swap_fees type --- dist/swap/types.d.ts | 2 +- src/swap/types.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index eaa3eb43..5fb80c48 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -164,7 +164,7 @@ export interface SwapRouterQuote { }; swap_fees: { amount: string; - asset_id: string; + asset: SwapRouteAsset; }; price: number; price_impact: number; diff --git a/src/swap/types.ts b/src/swap/types.ts index 7a586976..2cca8472 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -189,7 +189,7 @@ export interface SwapRouterQuote { }; swap_fees: { amount: string; - asset_id: string; + asset: SwapRouteAsset; }; price: number; price_impact: number; From 94f77223a6288afe153b3decc90f8a166e1cdb41 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Wed, 1 Mar 2023 16:22:28 +0300 Subject: [PATCH 09/39] refactor(swap): remove unnecessary types --- dist/index.js | 2 +- dist/swap/index.d.ts | 25 ++++++++++++++++++++++--- dist/swap/types.d.ts | 17 ----------------- dist/swap/v2/index.d.ts | 29 +++++++++++++++++++++++++---- src/swap/types.ts | 23 ----------------------- src/swap/v2/index.ts | 30 ++++++++++++++++++++---------- 6 files changed, 68 insertions(+), 58 deletions(-) diff --git a/dist/index.js b/dist/index.js index 7e52cde1..1aefb1df 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),a=Uint8Array.from([1]);function s(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E="- would result negative",N="logic eval error:",O="exceeds schema integer count",P=/transaction \w+:/,k=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function X(t,e){return j.apply(this,arguments)}function j(){return(j=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Q(t,e){return U.apply(this,arguments)}function U(){return(U=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,X(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function q(){for(var e=arguments.length,n=new Array(e),r=0;r1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function rt(t,e){var n=Number(t);return st({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function at(t,e){return st({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function st(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(ut(e),2),s=a[0],o=a[1],u=v(ut(Math.round(Number(ot(s,o+r)))),2),i=u[0],c=u[1];return Number(ot(i,c-r))}return Math.round(e)}function ot(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ut(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function it(t,e){return ct.apply(this,arguments)}function ct(){return(ct=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,tt(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new k(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function pt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function lt(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function dt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ft(t){return(new TextEncoder).encode(t)}var mt,gt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return ft("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),xt=(f(mt={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(mt,S.V2,{testnet:148607e3,mainnet:1002541853}),mt);function It(t,e){var n=xt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:It(a,s),note:gt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht,Tt,yt,vt;function _t(){return(_t=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:It(a,s),note:gt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(ht=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",ht.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(vt||(vt={}));var wt=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),bt=ft("swap"),Dt=(f(yt={},exports.SwapType.FixedInput,ft("fixed-input")),f(yt,exports.SwapType.FixedOutput,ft("fixed-output")),yt),St={ASSET_OPT_IN:ft("asset_opt_in")},Et={testnet:159521633,mainnet:0};function Nt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return rt(r.asset.decimals,Number(r.amount))/rt(n.asset.decimals,Number(n.amount))}function Ot(t){if(!Et[t])throw new Error("Unknown network or network not supported: ".concat(t));return Et[t]}function Pt(t){return kt.apply(this,arguments)}function kt(){return(kt=c(u().mark((function e(n){var a,s,o,i,c,p,l,d;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[St.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),d=r.default.assignGroupID([p]),e.abrupt("return",[{txn:d[0],signers:[i]}]);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(c[0].quote.amount_in.amount),Number(c[c.length-1].quote.amount_out.amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Ot(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[bt,Dt[i],r.default.encodeUint64(I)],foreignApps:[It(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p}),b=i===exports.SwapType.FixedInput?7:8,w.fee=t.ALGORAND_MIN_TX_FEE*(b+1),D=r.default.assignGroupID([_,w]),E=[{txn:D[vt.INPUT_TXN],signers:[a]},{txn:D[vt.APP_CALL_TXN],signers:[a]}],e.next=17,Rt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 17:if(!(e.sent.length>0)){e.next=25;break}return e.t0=q,e.next=22,Pt({client:s,accountAddress:a,assetIDs:[d,f,m],routerAppID:v});case 22:e.t1=e.sent,e.t2=E,E=(0,e.t0)(e.t1,e.t2);case 25:return e.abrupt("return",E);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Rt(t){return Bt.apply(this,arguments)}function Bt(){return(Bt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});case 5:return i=t.sent,t.abrupt("return",i.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Vt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Xt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},jt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Qt=new(function(n){m(a,jt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=It(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:Yt(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Jt(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?G({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Yt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Kt,Ht,Zt=ft("e");function $t(t){return te.apply(this,arguments)}function te(){return(te=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=G({stateArray:A}),T=e.fromByteArray(W([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(W([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(W([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ee(){return(ee=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=G({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mae)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ae,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ie(){return ie=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,zt(n,r);case 6:return o=t.sent,i=Jt(o,It(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[ne[S.V1_1].asset1],asset2ID:i[ne[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ie.apply(this,arguments)}var ce=Object.freeze({__proto__:null,getPoolInfo:se,getPoolReserves:function(t,e){return ue.apply(this,arguments)},getPoolAssets:function(t){return ie.apply(this,arguments)}});function pe(t){return le.apply(this,arguments)}function le(){return(le=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Wt(S.V2),i=o.generateLogicSigAccountForPool(e),c=It(r,S.V2),p=i.address(),l=M(a,s),t.next=8,zt(n,p);case 8:return d=t.sent,f=Jt(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[ne.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ne.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ne.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ne.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ne.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ne.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ne.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ne.v2.totalFeeShare]),m.poolTokenID=Number(f[ne.v2.poolTokenID]),m.asset1ID=Number(f[ne.v2.asset1]),m.asset2ID=Number(f[ne.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function de(){return(de=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,zt(e,n.account.address());case 2:return r=t.sent,a=Jt(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[ne.v2.asset1Reserves]),s.asset2=BigInt(a[ne.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[ne.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(){return(fe=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,zt(n,r);case 3:return s=t.sent,o=Jt(s,It(a,S.V2)),i=null,o&&(i={asset1ID:o[ne[S.V2].asset1],asset2ID:o[ne[S.V2].asset2],poolTokenID:o[ne[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var me=Object.freeze({__proto__:null,getPoolInfo:pe,getPoolReserves:function(t,e){return de.apply(this,arguments)},getPoolAssets:function(t){return fe.apply(this,arguments)}});function ge(t){return Boolean(t&&!(t.asset1+t.asset2))}var xe,Ie,Ae=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ge(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=rt(t.asset1,e.asset1)/rt(t.asset2,e.asset2)),r},isPoolEmpty:ge,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([se(t),pe(t)])}}),he=o((f(xe={},S.V1_1,o(o({},ce),Ae)),f(xe,S.V2,o(o({},me),Ae)),xe),Ae);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Ie||(Ie={}));var Te=96e4,ye=859e3;function ve(){return(ve=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=It(a,S.V1_1),T=Qt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:gt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[ft("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:F.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:_e(A),suggestedParams:c}),(E=[])[Ie.FUNDING_TXN]=D,E[Ie.VALIDATOR_APP_CALL]=_,E[Ie.POOL_TOKEN_CREATE]=w,E[Ie.ASSET1_OPT_IN]=b,A||(E[Ie.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),O=[{txn:N[Ie.FUNDING_TXN],signers:[i]},{txn:N[Ie.VALIDATOR_APP_CALL],signers:[y]},{txn:N[Ie.POOL_TOKEN_CREATE],signers:[y]},{txn:N[Ie.ASSET1_OPT_IN],signers:[y]}],N[Ie.ASSET2_OPT_IN]&&O.push({txn:N[Ie.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(t){return t?Te:ye}function we(){return(we=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Qt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ie.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function be(t){return De.apply(this,arguments)}function De(){return(De=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,tt(n,a[Ie.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Se(){return(Se=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,be({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",he.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ee,Ne={generateTxns:function(t){return ve.apply(this,arguments)},signTxns:function(t){return we.apply(this,arguments)},execute:function(t){return Se.apply(this,arguments)},getBootstrapFundingTxnAmount:_e};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ee||(Ee={}));var Oe=5,Pe=6;function ke(){return(ke=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=It(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,he.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Gt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[ft("bootstrap")],note:gt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Fe(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Le(h),suggestedParams:p}),(_=[])[Ee.FUNDING_TXN]=y,_[Ee.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Ee.FUNDING_TXN]={txn:w[Ee.FUNDING_TXN],signers:[c]},b[Ee.VALIDATOR_APP_CALL]={txn:w[Ee.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Le(t){return function(t,e){var n=Wt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Fe(t)+1e5}function Fe(e){return((e?Oe:Pe)+1)*t.ALGORAND_MIN_TX_FEE}function Re(){return(Re=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Gt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ee.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(){return(Be=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,X(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ne.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",he.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new k(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Me,Ce={generateTxns:function(t){return ke.apply(this,arguments)},signTxns:function(t){return Re.apply(this,arguments)},execute:function(t){return Be.apply(this,arguments)},getBootstrapFundingTxnAmount:Le,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Le(e)}};var Ve,Xe=(f(Me={},S.V1_1,Ne),f(Me,S.V2,Ce),f(Me,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Ne.generateTxns(t):Ce.generateTxns(t)})),f(Me,"signTxns",(function(t){return t.contractVersion===S.V1_1?Ne.signTxns(t):Ce.signTxns(t)})),f(Me,"execute",(function(t){return t.contractVersion===S.V1_1?Ne.execute(t):Ce.execute(t)})),f(Me,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Ne.getBootstrapFundingTxnAmount(n):Ce.getBootstrapFundingTxnAmount(n)})),Me);exports.V1_1AddLiquidityTxnIndices=void 0,(Ve=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ve.FEE_TXN=0]="FEE_TXN",Ve[Ve.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ve[Ve.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ve[Ve.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ve[Ve.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var je,Qe,Ue,qe,Ge=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(qe=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",qe.FLEXIBLE="flexible",qe.INITIAL="initial";var We,ze=(f(je={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(je,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(je,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),je),Je=(f(Qe={},exports.V2AddLiquidityType.INITIAL,1),f(Qe,exports.V2AddLiquidityType.SINGLE,2),f(Qe,exports.V2AddLiquidityType.FLEXIBLE,2),Qe),Ye=(f(Ue={},exports.V2AddLiquidityType.INITIAL,3),f(Ue,exports.V2AddLiquidityType.FLEXIBLE,3),f(Ue,exports.V2AddLiquidityType.SINGLE,2),Ue);function Ke(t){var e=t.assetIn,n=t.assetOut;return rt(n.decimals,Number(n.amount))/rt(e.decimals,Number(e.amount))}function He(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=Ke({assetIn:r,assetOut:a}),o=rt(a.decimals,Number(n))/rt(r.decimals,Number(e));return st({decimalPlaces:5},Math.abs(s/o-1))}function Ze(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,u=t.asset1Amount,i=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(u),d=s.asset2+BigInt(i),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(u)-x,h=BigInt(i)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=$e(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=$e(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:He({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function $e(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function tn(e){return(Je[e]+1)*t.ALGORAND_MIN_TX_FEE}var en=ft("add_liquidity"),nn=(f(We={},S.V1_1,[ft("mint")]),f(We,S.V2,{INITIAL_LIQUIDITY:[ft("add_initial_liquidity")],SINGLE_ASSET_MODE:[en,ft("single")],FLEXIBLE_MODE:[en,ft("flexible")]}),We);function rn(){return(rn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=nt("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:It(s,S.V1_1),appArgs:nn.v1_1,accounts:[d],note:gt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function an(){return(an=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sn(){return(sn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,$t({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,it(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=pt(a),x=lt(a),t.next=17,$t({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new k(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var on=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function On(){return(On=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!e.isUsingSwapRouter){t.next=2;break}return t.abrupt("return",Lt(e));case 2:if(n=e.assetIn,a=e.assetOut,s=e.client,o=e.initiatorAddr,i=e.pool,c=e.slippage,p=e.swapType,l=i.account.address(),(d=[i.asset1ID,i.asset2ID]).includes(n.id)&&d.includes(a.id)&&n.id!==a.id){t.next=7;break}throw new k({pool:i,assetIn:n,assetOut:a},"Input asset (#".concat(n.id,") and output asset (#").concat(a.id,") provided to generate transactions do not belong to the pool ").concat(l,"."));case 7:return t.next=9,s.getTransactionParams().do();case 9:return f=t.sent,m=C(n.id),g=p===exports.SwapType.FixedInput?n.amount:nt("positive",c,n.amount),x=p===exports.SwapType.FixedOutput?a.amount:nt("negative",c,a.amount),I=m?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,assetIndex:n.id,suggestedParams:f}),(A=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:i.validatorAppID,appArgs:[bt,Dt[p],r.default.encodeUint64(x)],note:gt.getAppCallTxnNoteWithClientName(S.V2),accounts:[l],foreignAssets:[i.asset1ID,i.asset2ID],suggestedParams:f})).fee=Pn(p),(h=[])[vt.INPUT_TXN]=I,h[vt.APP_CALL_TXN]=A,T=r.default.assignGroupID(h),t.abrupt("return",[{txn:T[vt.INPUT_TXN],signers:[o]},{txn:T[vt.APP_CALL_TXN],signers:[o]}]);case 21:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(e){return(wt[e]+1)*t.ALGORAND_MIN_TX_FEE}function kn(){return(kn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,c=e.assetIn,t.next=3,it(a,[i]);case 3:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=10,Q(a,o);case 10:return g=t.sent,x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.route[s.route.length-1].quote.amount_out.asset.id),I=null==g||null===(n=g.find((function(t){return t.txn.txn.xaid===c.id})))||void 0===n?void 0:n.txn.txn,A=null==g||null===(r=g.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:f,assetIn:I&&{amount:BigInt(c.amount)-BigInt(I.aamt||0),id:c.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:m});case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ln(){return(Ln=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.asset,r=e.decimals,a=e.pool,s=e.type,o=e.isSwapRouterEnabled,s!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,Fn({pool:a,assetIn:n,decimals:r,isSwapRouterEnabled:o});case 4:i=t.sent,t.next=10;break;case 7:return t.next=9,Bn({pool:a,assetOut:n,decimals:r,isSwapRouterEnabled:o});case 9:i=t.sent;case 10:return t.abrupt("return",i);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Fn(t){return Rn.apply(this,arguments)}function Rn(){return(Rn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetIn,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:n,assetIn:r},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(r.amount),c=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}p=n.asset2ID,l=n.asset1Reserves,d=n.asset2Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}p=n.asset1ID,l=n.asset2Reserves,d=n.asset1Reserves,t.next=18;break;case 17:throw new k({pool:n,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(f=Cn({inputSupply:l,outputSupply:d,swapInputAmount:i,totalFeeShare:c,decimals:a}),m=f.swapOutputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(m>d)){t.next=21;break}throw new Nn;case 21:if(I={assetInID:r.id,assetInAmount:i,assetOutID:p,assetOutAmount:m,swapFee:Number(g),rate:rt(a.assetOut,Number(m))/rt(a.assetIn,Number(i)),priceImpact:x},!s){t.next=28;break}return t.next=25,Mt({amount:r.amount,assetInID:Number(r.id),assetOutID:p,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_out.amount)>I.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},A),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:n,quote:I},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Bn(t){return Mn.apply(this,arguments)}function Mn(){return(Mn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.pool,r=e.assetOut,a=e.decimals,s=e.isSwapRouterEnabled,n.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:n,assetOut:r},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(r.amount),c=n.totalFeeShare,r.id!==n.asset1ID){t.next=11;break}p=n.asset2ID,l=n.asset2Reserves,d=n.asset1Reserves,t.next=18;break;case 11:if(r.id!==n.asset2ID){t.next=17;break}p=n.asset1ID,l=n.asset1Reserves,d=n.asset2Reserves,t.next=18;break;case 17:throw new k({pool:n,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(n.account.address(),"."));case 18:if(f=Vn({inputSupply:l,outputSupply:d,swapOutputAmount:i,totalFeeShare:c,decimals:a}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(i>d)){t.next=21;break}throw new Nn;case 21:if(I={assetInID:p,assetInAmount:m,assetOutID:r.id,assetOutAmount:i,swapFee:Number(g),rate:rt(a.assetOut,Number(i))/rt(a.assetIn,Number(m)),priceImpact:x},!s){t.next=28;break}return t.next=25,Mt({amount:r.amount,assetInID:p,assetOutID:r.id,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Qn(){return(Qn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:gt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(nt("negative",l,c)),r.default.encodeUint64(nt("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[yn.ASSET_TRANSFER_TXN]=g,I[yn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[yn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[yn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Un(){return(Un=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=nt("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:gt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[yn.ASSET_TRANSFER_TXN]=T,v[yn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[yn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[yn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qn(){return(qn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,it(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Q(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Gn,Wn,zn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=jn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=jn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Xn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Xn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Qn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Un.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return qn.apply(this,arguments)}},Jn=(f(Gn={},S.V1_1,En),f(Gn,S.V2,zn),Gn),Yn=3n,Kn=1000n;function Hn(){return(Hn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===Wn.FEE_TXN_INDEX?c:e===Wn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.assetIn,c=e.assetOut,p=e.slippage,l=e.initiatorAddr,d=s.account.address(),(f=[s.asset1ID,s.asset2ID]).includes(i.id)&&f.includes(c.id)&&i.id!==c.id){t.next=5;break}throw new k({pool:s,assetIn:i,assetOut:c},"Input asset (#".concat(i.id,") and output asset (#").concat(c.id,") provided to generate transactions do not belong to the pool ").concat(d,"."));case 5:return t.next=7,n.getTransactionParams().do();case 7:return m=t.sent,g=[ft("swap"),o===exports.SwapType.FixedInput?ft("fi"):ft("fo")],x=r.default.makeApplicationNoOpTxnFromObject({from:d,appIndex:s.validatorAppID,appArgs:g,accounts:[l],note:gt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:m}),I=o===exports.SwapType.FixedOutput?nt("positive",p,i.amount):i.amount,A=0===i.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:i.id,amount:I,suggestedParams:m}),h=o===exports.SwapType.FixedInput?nt("negative",p,c.amount):c.amount,T=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:l,amount:h,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:l,assetIndex:c.id,amount:h,suggestedParams:m}),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.fee+T.fee,note:a,suggestedParams:m}),v=r.default.assignGroupID([y,x,A,T]),t.abrupt("return",[{txn:v[0],signers:[l]},{txn:v[1],signers:[d]},{txn:v[2],signers:[l]},{txn:v[3],signers:[d]}]);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new k({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new k({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*Yn/Kn,l=i-u*i/(u+(c-p));if(l>i)throw new Nn;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:Ke(d),priceImpact:He(o({inputSupply:u,outputSupply:i},d))}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,it(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new k({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new k({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new Nn;var c=o*u/(u-i)-o,p=c*Kn/(Kn-Yn),l=p-c,d=rt(a.assetOut,Number(i))/rt(a.assetIn,Number(p)),f=rt(a.assetOut,Number(u))/rt(a.assetIn,Number(o)),m=st({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m}}function rr(t){return ar.apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,it(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sr(){return(sr=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Wn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Wn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,tr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,rr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:lt(s),fees:pt(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new k(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Wn||(Wn={}));var or,ur={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?$n({pool:e,reserves:n,assetIn:r,decimals:a}):nr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:$n,getFixedOutputSwapQuote:nr,generateTxns:function(t){return Zn.apply(this,arguments)},signTxns:function(t){return Hn.apply(this,arguments)},execute:function(t){return sr.apply(this,arguments)},executeFixedOutputSwap:rr},ir=4*t.ALGORAND_MIN_TX_FEE;function cr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Nn})))throw new Nn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function pr(){return pr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedInputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Xn.getFixedInputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),pr.apply(this,arguments)}function lr(){return lr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedOutputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Xn.getFixedOutputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),lr.apply(this,arguments)}function dr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:Nt(t.route)}function fr(t){var e,n=t[0],r=dr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=dr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}var mr=(f(or={},S.V1_1,ur),f(or,S.V2,Xn),f(or,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ge(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return pr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return lr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(or,"generateTxns",(function(t){return t.isUsingSwapRouter||t.pool.contractVersion!==S.V1_1?Xn.generateTxns(t):ur.generateTxns(t)})),f(or,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&function(t){if(t.type===exports.SwapQuoteType.Direct)return t.quoteWithPool.pool.contractVersion;return S.V2}(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return ur.signTxns(o({pool:e},t))}return Xn.signTxns(t)})),f(or,"execute",(function(t){return t.contractVersion===S.V1_1?ur.execute(t):Xn.execute(t)})),or);function gr(){return(gr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,xr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,it(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:pt(a),confirmedRound:d,txnID:l,groupID:lt(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function xr(t){return Ir.apply(this,arguments)}function Ir(){return(Ir=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(){return Ar=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:pt(e),groupID:lt(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,it(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Ar.apply(this,arguments)}function hr(){return(hr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[ft("redeem")],note:gt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Tr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ft("commit"),i],note:W([ft("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function yr(){return(yr=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Tr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[ft("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=L,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Tn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Xe,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=F,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Jn,exports.Swap=mr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=vn,exports.V2AddLiquidityTxnIndices=ze,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=nt,exports.calculateAccountMinimumRequiredBalance=Yt,exports.combineAndRegroupSignerTxns=q,exports.convertFromBaseUnits=rt,exports.convertToBaseUnits=at,exports.generateOptIntoAssetTxns=function(t){return R.apply(this,arguments)},exports.generateOptIntoValidatorTxns=function(t){return At.apply(this,arguments)},exports.generateOptOutOfValidatorTxns=function(t){return _t.apply(this,arguments)},exports.generateRedeemTxns=function(t){return hr.apply(this,arguments)},exports.generateSwapRouterAssetOptInTransaction=Pt,exports.generateSwapRouterTxns=Lt,exports.getAccountExcess=function(t){return ee.apply(this,arguments)},exports.getAccountExcessWithinPool=$t,exports.getAccountInformation=zt,exports.getAddLiquidityTotalFee=function(e){switch(e.version){case S.V1_1:return Ge;case S.V2:return n=e.type,(Je[n]+Ye[n])*t.ALGORAND_MIN_TX_FEE;default:throw new Error("Provided contract version was not valid.")}var n},exports.getMinRequiredBalanceToOptIn=function(t){var e,n=t.currentMinumumBalanceForAccount,r=t.suggestedTransactionFee;if("app-opt-in"===t.type){var a=Wt(t.contractVersion);e=1e5+5e4*a.schema.numLocalByteSlices+28500*a.schema.numLocalInts}else e=1e5;return e+(n||0)+(r||0)},exports.getStakingAppID=function(t){return"testnet"===t?51948952:649588853},exports.getSwapFeesFromSwapRoute=function(t){return t.reduce((function(t,e){return t+Number(e.swap_fees.amount)}),0)},exports.getSwapRoute=Mt,exports.getSwapRouteRate=Nt,exports.getSwapRouterAppID=Ot,exports.getSwapRouterAppOptInRequiredAssetIDs=Rt,exports.getSwapTotalFee=function(e){switch(e.version){case S.V1_1:return ir;case S.V2:return n=e.type,(wt[n]+2)*t.ALGORAND_MIN_TX_FEE;default:throw new Error("Provided contract version was not valid.")}var n},exports.getTxnGroupID=lt,exports.getValidatorAppID=It,exports.hasSufficientMinimumBalance=function(t){return t.amount>=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=he,exports.prepareCommitTransactions=function(t){return yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Ar.apply(this,arguments)},exports.redeemExcessAsset=function(t){return gr.apply(this,arguments)},exports.sendAndWaitRawTransaction=it,exports.sumUpTxnFees=pt,exports.tinymanContract_v1_1=Qt,exports.tinymanContract_v2=Gt,exports.tinymanJSSDKConfig=gt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",s=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,s=Object.create(a.prototype),o=new w(r||[]);return s._invoke=function(t,e,n){var r="suspendedStart";return function(a,s){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw s;return D()}for(n.method=a,n.arg=s;;){var o=n.delegate;if(o){var u=y(o,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,o),s}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,a)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,s,o,u){var i=p(t[a],t,s);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,u)}),(function(t){r("throw",t,o,u)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function s(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(s,s):s()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function w(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,s=function e(){for(;++r=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E="- would result negative",N="logic eval error:",O="exceeds schema integer count",P=/transaction \w+:/,k=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function X(t,e){return j.apply(this,arguments)}function j(){return(j=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Q(t,e){return U.apply(this,arguments)}function U(){return(U=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,X(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function q(){for(var e=arguments.length,n=new Array(e),r=0;r1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function rt(t,e){var n=Number(t);return st({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function at(t,e){return st({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function st(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(ut(e),2),s=a[0],o=a[1],u=v(ut(Math.round(Number(ot(s,o+r)))),2),i=u[0],c=u[1];return Number(ot(i,c-r))}return Math.round(e)}function ot(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ut(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function it(t,e){return ct.apply(this,arguments)}function ct(){return(ct=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,tt(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new k(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function pt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function lt(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function dt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ft(t){return(new TextEncoder).encode(t)}var mt,gt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return ft("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),xt=(f(mt={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(mt,S.V2,{testnet:148607e3,mainnet:1002541853}),mt);function It(t,e){var n=xt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:It(a,s),note:gt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht,Tt,yt,vt;function _t(){return(_t=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:It(a,s),note:gt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(ht=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",ht.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(vt||(vt={}));var wt=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),bt=ft("swap"),Dt=(f(yt={},exports.SwapType.FixedInput,ft("fixed-input")),f(yt,exports.SwapType.FixedOutput,ft("fixed-output")),yt),St={ASSET_OPT_IN:ft("asset_opt_in")},Et={testnet:159521633,mainnet:0};function Nt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return rt(r.asset.decimals,Number(r.amount))/rt(n.asset.decimals,Number(n.amount))}function Ot(t){if(!Et[t])throw new Error("Unknown network or network not supported: ".concat(t));return Et[t]}function Pt(t){return kt.apply(this,arguments)}function kt(){return(kt=c(u().mark((function e(n){var a,s,o,i,c,p,l,d;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[St.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),d=r.default.assignGroupID([p]),e.abrupt("return",[{txn:d[0],signers:[i]}]);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(c[0].quote.amount_in.amount),Number(c[c.length-1].quote.amount_out.amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Ot(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[bt,Dt[i],r.default.encodeUint64(I)],foreignApps:[It(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p}),b=i===exports.SwapType.FixedInput?7:8,w.fee=t.ALGORAND_MIN_TX_FEE*(b+1),D=r.default.assignGroupID([_,w]),E=[{txn:D[vt.INPUT_TXN],signers:[a]},{txn:D[vt.APP_CALL_TXN],signers:[a]}],e.next=17,Rt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 17:if(!(e.sent.length>0)){e.next=25;break}return e.t0=q,e.next=22,Pt({client:s,accountAddress:a,assetIDs:[d,f,m],routerAppID:v});case 22:e.t1=e.sent,e.t2=E,E=(0,e.t0)(e.t1,e.t2);case 25:return e.abrupt("return",E);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Rt(t){return Bt.apply(this,arguments)}function Bt(){return(Bt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});case 5:return i=t.sent,t.abrupt("return",i.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Vt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Xt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},jt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Qt=new(function(n){m(a,jt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=It(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:Yt(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Jt(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?G({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Yt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Kt,Ht,Zt=ft("e");function $t(t){return te.apply(this,arguments)}function te(){return(te=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=G({stateArray:A}),T=e.fromByteArray(W([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(W([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(W([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ee(){return(ee=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=G({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mae)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ae,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ie(){return ie=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,zt(n,r);case 6:return o=t.sent,i=Jt(o,It(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[ne[S.V1_1].asset1],asset2ID:i[ne[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ie.apply(this,arguments)}var ce=Object.freeze({__proto__:null,getPoolInfo:se,getPoolReserves:function(t,e){return ue.apply(this,arguments)},getPoolAssets:function(t){return ie.apply(this,arguments)}});function pe(t){return le.apply(this,arguments)}function le(){return(le=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Wt(S.V2),i=o.generateLogicSigAccountForPool(e),c=It(r,S.V2),p=i.address(),l=M(a,s),t.next=8,zt(n,p);case 8:return d=t.sent,f=Jt(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[ne.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ne.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ne.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ne.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ne.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ne.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ne.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ne.v2.totalFeeShare]),m.poolTokenID=Number(f[ne.v2.poolTokenID]),m.asset1ID=Number(f[ne.v2.asset1]),m.asset2ID=Number(f[ne.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function de(){return(de=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,zt(e,n.account.address());case 2:return r=t.sent,a=Jt(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[ne.v2.asset1Reserves]),s.asset2=BigInt(a[ne.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[ne.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(){return(fe=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,zt(n,r);case 3:return s=t.sent,o=Jt(s,It(a,S.V2)),i=null,o&&(i={asset1ID:o[ne[S.V2].asset1],asset2ID:o[ne[S.V2].asset2],poolTokenID:o[ne[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var me=Object.freeze({__proto__:null,getPoolInfo:pe,getPoolReserves:function(t,e){return de.apply(this,arguments)},getPoolAssets:function(t){return fe.apply(this,arguments)}});function ge(t){return Boolean(t&&!(t.asset1+t.asset2))}var xe,Ie,Ae=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ge(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=rt(t.asset1,e.asset1)/rt(t.asset2,e.asset2)),r},isPoolEmpty:ge,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([se(t),pe(t)])}}),he=o((f(xe={},S.V1_1,o(o({},ce),Ae)),f(xe,S.V2,o(o({},me),Ae)),xe),Ae);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Ie||(Ie={}));var Te=96e4,ye=859e3;function ve(){return(ve=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=It(a,S.V1_1),T=Qt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:gt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[ft("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:F.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:_e(A),suggestedParams:c}),(E=[])[Ie.FUNDING_TXN]=D,E[Ie.VALIDATOR_APP_CALL]=_,E[Ie.POOL_TOKEN_CREATE]=w,E[Ie.ASSET1_OPT_IN]=b,A||(E[Ie.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),O=[{txn:N[Ie.FUNDING_TXN],signers:[i]},{txn:N[Ie.VALIDATOR_APP_CALL],signers:[y]},{txn:N[Ie.POOL_TOKEN_CREATE],signers:[y]},{txn:N[Ie.ASSET1_OPT_IN],signers:[y]}],N[Ie.ASSET2_OPT_IN]&&O.push({txn:N[Ie.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(t){return t?Te:ye}function we(){return(we=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Qt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ie.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function be(t){return De.apply(this,arguments)}function De(){return(De=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,tt(n,a[Ie.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Se(){return(Se=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,be({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",he.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ee,Ne={generateTxns:function(t){return ve.apply(this,arguments)},signTxns:function(t){return we.apply(this,arguments)},execute:function(t){return Se.apply(this,arguments)},getBootstrapFundingTxnAmount:_e};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ee||(Ee={}));var Oe=5,Pe=6;function ke(){return(ke=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=It(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,he.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Gt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[ft("bootstrap")],note:gt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Fe(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Le(h),suggestedParams:p}),(_=[])[Ee.FUNDING_TXN]=y,_[Ee.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Ee.FUNDING_TXN]={txn:w[Ee.FUNDING_TXN],signers:[c]},b[Ee.VALIDATOR_APP_CALL]={txn:w[Ee.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Le(t){return function(t,e){var n=Wt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Fe(t)+1e5}function Fe(e){return((e?Oe:Pe)+1)*t.ALGORAND_MIN_TX_FEE}function Re(){return(Re=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Gt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ee.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(){return(Be=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,X(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ne.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",he.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new k(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Me,Ce={generateTxns:function(t){return ke.apply(this,arguments)},signTxns:function(t){return Re.apply(this,arguments)},execute:function(t){return Be.apply(this,arguments)},getBootstrapFundingTxnAmount:Le,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Le(e)}};var Ve,Xe=(f(Me={},S.V1_1,Ne),f(Me,S.V2,Ce),f(Me,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Ne.generateTxns(t):Ce.generateTxns(t)})),f(Me,"signTxns",(function(t){return t.contractVersion===S.V1_1?Ne.signTxns(t):Ce.signTxns(t)})),f(Me,"execute",(function(t){return t.contractVersion===S.V1_1?Ne.execute(t):Ce.execute(t)})),f(Me,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Ne.getBootstrapFundingTxnAmount(n):Ce.getBootstrapFundingTxnAmount(n)})),Me);exports.V1_1AddLiquidityTxnIndices=void 0,(Ve=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ve.FEE_TXN=0]="FEE_TXN",Ve[Ve.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ve[Ve.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ve[Ve.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ve[Ve.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var je,Qe,Ue,qe,Ge=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(qe=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",qe.FLEXIBLE="flexible",qe.INITIAL="initial";var We,ze=(f(je={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(je,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(je,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),je),Je=(f(Qe={},exports.V2AddLiquidityType.INITIAL,1),f(Qe,exports.V2AddLiquidityType.SINGLE,2),f(Qe,exports.V2AddLiquidityType.FLEXIBLE,2),Qe),Ye=(f(Ue={},exports.V2AddLiquidityType.INITIAL,3),f(Ue,exports.V2AddLiquidityType.FLEXIBLE,3),f(Ue,exports.V2AddLiquidityType.SINGLE,2),Ue);function Ke(t){var e=t.assetIn,n=t.assetOut;return rt(n.decimals,Number(n.amount))/rt(e.decimals,Number(e.amount))}function He(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=Ke({assetIn:r,assetOut:a}),o=rt(a.decimals,Number(n))/rt(r.decimals,Number(e));return st({decimalPlaces:5},Math.abs(s/o-1))}function Ze(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,u=t.asset1Amount,i=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(u),d=s.asset2+BigInt(i),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(u)-x,h=BigInt(i)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=$e(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=$e(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:He({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function $e(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function tn(e){return(Je[e]+1)*t.ALGORAND_MIN_TX_FEE}var en=ft("add_liquidity"),nn=(f(We={},S.V1_1,[ft("mint")]),f(We,S.V2,{INITIAL_LIQUIDITY:[ft("add_initial_liquidity")],SINGLE_ASSET_MODE:[en,ft("single")],FLEXIBLE_MODE:[en,ft("flexible")]}),We);function rn(){return(rn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=nt("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:It(s,S.V1_1),appArgs:nn.v1_1,accounts:[d],note:gt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function an(){return(an=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sn(){return(sn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,$t({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,it(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=pt(a),x=lt(a),t.next=17,$t({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new k(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var on=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function On(){return(On=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!e.isUsingSwapRouter){t.next=2;break}return t.abrupt("return",Lt(e));case 2:if(n=e.assetIn,a=e.assetOut,s=e.client,o=e.initiatorAddr,i=e.pool,c=e.slippage,p=e.swapType,l=i.account.address(),(d=[i.asset1ID,i.asset2ID]).includes(n.id)&&d.includes(a.id)&&n.id!==a.id){t.next=7;break}throw new k({pool:i,assetIn:n,assetOut:a},"Input asset (#".concat(n.id,") and output asset (#").concat(a.id,") provided to generate transactions do not belong to the pool ").concat(l,"."));case 7:return t.next=9,s.getTransactionParams().do();case 9:return f=t.sent,m=C(n.id),g=p===exports.SwapType.FixedInput?n.amount:nt("positive",c,n.amount),x=p===exports.SwapType.FixedOutput?a.amount:nt("negative",c,a.amount),I=m?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,assetIndex:n.id,suggestedParams:f}),(A=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:i.validatorAppID,appArgs:[bt,Dt[p],r.default.encodeUint64(x)],note:gt.getAppCallTxnNoteWithClientName(S.V2),accounts:[l],foreignAssets:[i.asset1ID,i.asset2ID],suggestedParams:f})).fee=Pn(p),(h=[])[vt.INPUT_TXN]=I,h[vt.APP_CALL_TXN]=A,T=r.default.assignGroupID(h),t.abrupt("return",[{txn:T[vt.INPUT_TXN],signers:[o]},{txn:T[vt.APP_CALL_TXN],signers:[o]}]);case 21:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(e){return(wt[e]+1)*t.ALGORAND_MIN_TX_FEE}function kn(){return(kn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,c=e.assetIn,t.next=3,it(a,[i]);case 3:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=10,Q(a,o);case 10:return g=t.sent,x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.route[s.route.length-1].quote.amount_out.asset.id),I=null==g||null===(n=g.find((function(t){return t.txn.txn.xaid===c.id})))||void 0===n?void 0:n.txn.txn,A=null==g||null===(r=g.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:f,assetIn:I&&{amount:BigInt(c.amount)-BigInt(I.aamt||0),id:c.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:m});case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ln(){return(Ln=c(u().mark((function t(e,n,r,a,s){var o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e!==exports.SwapType.FixedInput){t.next=6;break}return t.next=3,Fn({pool:n,assetIn:r,decimals:a,isSwapRouterEnabled:s});case 3:o=t.sent,t.next=9;break;case 6:return t.next=8,Bn({pool:n,assetOut:r,decimals:a,isSwapRouterEnabled:s});case 8:o=t.sent;case 9:return t.abrupt("return",o);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Fn(t){return Rn.apply(this,arguments)}function Rn(){return(Rn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,a.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:a,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(n.amount),c=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}p=a.asset2ID,l=a.asset1Reserves,d=a.asset2Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}p=a.asset1ID,l=a.asset2Reserves,d=a.asset1Reserves,t.next=18;break;case 17:throw new k({pool:a,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(f=Cn({inputSupply:l,outputSupply:d,swapInputAmount:i,totalFeeShare:c,decimals:r}),m=f.swapOutputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(m>d)){t.next=21;break}throw new Nn;case 21:if(I={assetInID:n.id,assetInAmount:i,assetOutID:p,assetOutAmount:m,swapFee:Number(g),rate:rt(r.assetOut,Number(m))/rt(r.assetIn,Number(i)),priceImpact:x},!s){t.next=28;break}return t.next=25,Mt({amount:n.amount,assetInID:Number(n.id),assetOutID:p,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_out.amount)>I.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},A),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:a,quote:I},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Bn(t){return Mn.apply(this,arguments)}function Mn(){return(Mn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,a.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:a,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(n.amount),c=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}p=a.asset2ID,l=a.asset2Reserves,d=a.asset1Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}p=a.asset1ID,l=a.asset1Reserves,d=a.asset2Reserves,t.next=18;break;case 17:throw new k({pool:a,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(f=Vn({inputSupply:l,outputSupply:d,swapOutputAmount:i,totalFeeShare:c,decimals:r}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(i>d)){t.next=21;break}throw new Nn;case 21:if(I={assetInID:p,assetInAmount:m,assetOutID:n.id,assetOutAmount:i,swapFee:Number(g),rate:rt(r.assetOut,Number(i))/rt(r.assetIn,Number(m)),priceImpact:x},!s){t.next=28;break}return t.next=25,Mt({amount:n.amount,assetInID:p,assetOutID:n.id,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Qn(){return(Qn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:gt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(nt("negative",l,c)),r.default.encodeUint64(nt("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[yn.ASSET_TRANSFER_TXN]=g,I[yn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[yn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[yn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Un(){return(Un=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=nt("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:gt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[yn.ASSET_TRANSFER_TXN]=T,v[yn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[yn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[yn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qn(){return(qn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,it(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Q(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Gn,Wn,zn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=jn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=jn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Xn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Xn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Qn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Un.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return qn.apply(this,arguments)}},Jn=(f(Gn={},S.V1_1,En),f(Gn,S.V2,zn),Gn),Yn=3n,Kn=1000n;function Hn(){return(Hn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===Wn.FEE_TXN_INDEX?c:e===Wn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.assetIn,c=e.assetOut,p=e.slippage,l=e.initiatorAddr,d=s.account.address(),(f=[s.asset1ID,s.asset2ID]).includes(i.id)&&f.includes(c.id)&&i.id!==c.id){t.next=5;break}throw new k({pool:s,assetIn:i,assetOut:c},"Input asset (#".concat(i.id,") and output asset (#").concat(c.id,") provided to generate transactions do not belong to the pool ").concat(d,"."));case 5:return t.next=7,n.getTransactionParams().do();case 7:return m=t.sent,g=[ft("swap"),o===exports.SwapType.FixedInput?ft("fi"):ft("fo")],x=r.default.makeApplicationNoOpTxnFromObject({from:d,appIndex:s.validatorAppID,appArgs:g,accounts:[l],note:gt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:m}),I=o===exports.SwapType.FixedOutput?nt("positive",p,i.amount):i.amount,A=0===i.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:i.id,amount:I,suggestedParams:m}),h=o===exports.SwapType.FixedInput?nt("negative",p,c.amount):c.amount,T=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:l,amount:h,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:l,assetIndex:c.id,amount:h,suggestedParams:m}),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.fee+T.fee,note:a,suggestedParams:m}),v=r.default.assignGroupID([y,x,A,T]),t.abrupt("return",[{txn:v[0],signers:[l]},{txn:v[1],signers:[d]},{txn:v[2],signers:[l]},{txn:v[3],signers:[d]}]);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new k({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new k({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*Yn/Kn,l=i-u*i/(u+(c-p));if(l>i)throw new Nn;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:Ke(d),priceImpact:He(o({inputSupply:u,outputSupply:i},d))}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,it(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new k({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new k({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new Nn;var c=o*u/(u-i)-o,p=c*Kn/(Kn-Yn),l=p-c,d=rt(a.assetOut,Number(i))/rt(a.assetIn,Number(p)),f=rt(a.assetOut,Number(u))/rt(a.assetIn,Number(o)),m=st({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m}}function rr(t){return ar.apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,it(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sr(){return(sr=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Wn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Wn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,tr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,rr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:lt(s),fees:pt(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new k(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Wn||(Wn={}));var or,ur={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?$n({pool:e,reserves:n,assetIn:r,decimals:a}):nr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:$n,getFixedOutputSwapQuote:nr,generateTxns:function(t){return Zn.apply(this,arguments)},signTxns:function(t){return Hn.apply(this,arguments)},execute:function(t){return sr.apply(this,arguments)},executeFixedOutputSwap:rr},ir=4*t.ALGORAND_MIN_TX_FEE;function cr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Nn})))throw new Nn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function pr(){return pr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedInputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Xn.getFixedInputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),pr.apply(this,arguments)}function lr(){return lr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedOutputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Xn.getFixedOutputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),lr.apply(this,arguments)}function dr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:Nt(t.route)}function fr(t){var e,n=t[0],r=dr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=dr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}var mr=(f(or={},S.V1_1,ur),f(or,S.V2,Xn),f(or,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ge(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return pr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return lr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(or,"generateTxns",(function(t){return t.isUsingSwapRouter||t.pool.contractVersion!==S.V1_1?Xn.generateTxns(t):ur.generateTxns(t)})),f(or,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&function(t){if(t.type===exports.SwapQuoteType.Direct)return t.quoteWithPool.pool.contractVersion;return S.V2}(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return ur.signTxns(o({pool:e},t))}return Xn.signTxns(t)})),f(or,"execute",(function(t){return t.contractVersion===S.V1_1?ur.execute(t):Xn.execute(t)})),or);function gr(){return(gr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,xr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,it(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:pt(a),confirmedRound:d,txnID:l,groupID:lt(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function xr(t){return Ir.apply(this,arguments)}function Ir(){return(Ir=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(){return Ar=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:pt(e),groupID:lt(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,it(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Ar.apply(this,arguments)}function hr(){return(hr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[ft("redeem")],note:gt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Tr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ft("commit"),i],note:W([ft("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function yr(){return(yr=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Tr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[ft("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=L,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Tn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Xe,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=F,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Jn,exports.Swap=mr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=vn,exports.V2AddLiquidityTxnIndices=ze,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=nt,exports.calculateAccountMinimumRequiredBalance=Yt,exports.combineAndRegroupSignerTxns=q,exports.convertFromBaseUnits=rt,exports.convertToBaseUnits=at,exports.generateOptIntoAssetTxns=function(t){return R.apply(this,arguments)},exports.generateOptIntoValidatorTxns=function(t){return At.apply(this,arguments)},exports.generateOptOutOfValidatorTxns=function(t){return _t.apply(this,arguments)},exports.generateRedeemTxns=function(t){return hr.apply(this,arguments)},exports.generateSwapRouterAssetOptInTransaction=Pt,exports.generateSwapRouterTxns=Lt,exports.getAccountExcess=function(t){return ee.apply(this,arguments)},exports.getAccountExcessWithinPool=$t,exports.getAccountInformation=zt,exports.getAddLiquidityTotalFee=function(e){switch(e.version){case S.V1_1:return Ge;case S.V2:return n=e.type,(Je[n]+Ye[n])*t.ALGORAND_MIN_TX_FEE;default:throw new Error("Provided contract version was not valid.")}var n},exports.getMinRequiredBalanceToOptIn=function(t){var e,n=t.currentMinumumBalanceForAccount,r=t.suggestedTransactionFee;if("app-opt-in"===t.type){var a=Wt(t.contractVersion);e=1e5+5e4*a.schema.numLocalByteSlices+28500*a.schema.numLocalInts}else e=1e5;return e+(n||0)+(r||0)},exports.getStakingAppID=function(t){return"testnet"===t?51948952:649588853},exports.getSwapFeesFromSwapRoute=function(t){return t.reduce((function(t,e){return t+Number(e.swap_fees.amount)}),0)},exports.getSwapRoute=Mt,exports.getSwapRouteRate=Nt,exports.getSwapRouterAppID=Ot,exports.getSwapRouterAppOptInRequiredAssetIDs=Rt,exports.getSwapTotalFee=function(e){switch(e.version){case S.V1_1:return ir;case S.V2:return n=e.type,(wt[n]+2)*t.ALGORAND_MIN_TX_FEE;default:throw new Error("Provided contract version was not valid.")}var n},exports.getTxnGroupID=lt,exports.getValidatorAppID=It,exports.hasSufficientMinimumBalance=function(t){return t.amount>=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=he,exports.prepareCommitTransactions=function(t){return yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Ar.apply(this,arguments)},exports.redeemExcessAsset=function(t){return gr.apply(this,arguments)},exports.sendAndWaitRawTransaction=it,exports.sumUpTxnFees=pt,exports.tinymanContract_v1_1=Qt,exports.tinymanContract_v2=Gt,exports.tinymanJSSDKConfig=gt; diff --git a/dist/swap/index.d.ts b/dist/swap/index.d.ts index ec49c902..a7c96e08 100644 --- a/dist/swap/index.d.ts +++ b/dist/swap/index.d.ts @@ -47,9 +47,28 @@ export declare const Swap: { }) => Promise>; }; v2: { - getQuote: (params: import("./types").GetSwapQuoteWithContractVersionParams) => Promise; - getFixedInputSwapQuote: ({ pool, assetIn, decimals, isSwapRouterEnabled }: import("./types").GetFixedInputSwapQuoteByContractVersionParams) => Promise; - getFixedOutputSwapQuote: ({ pool, assetOut, decimals, isSwapRouterEnabled }: import("./types").GetFixedOutputSwapQuoteByContractVersionParams) => Promise; + getQuote: (type: import("./constants").SwapType, pool: import("..").V2PoolInfo, asset: import("../util/asset/assetModels").AssetWithIdAndAmount, decimals: { + assetIn: number; + assetOut: number; + }, isSwapRouterEnabled?: boolean | undefined) => Promise; + getFixedInputSwapQuote: ({ assetIn, decimals, pool, isSwapRouterEnabled }: { + pool: import("..").V2PoolInfo; + assetIn: import("../util/asset/assetModels").AssetWithIdAndAmount; + decimals: { + assetIn: number; + assetOut: number; + }; + isSwapRouterEnabled?: boolean | undefined; + }) => Promise; + getFixedOutputSwapQuote: ({ assetOut, decimals, pool, isSwapRouterEnabled }: { + pool: import("..").V2PoolInfo; + assetOut: import("../util/asset/assetModels").AssetWithIdAndAmount; + decimals: { + assetIn: number; + assetOut: number; + }; + isSwapRouterEnabled?: boolean | undefined; + }) => Promise; generateTxns: (params: import("./types").GenerateSwapTxnsParams) => Promise; signTxns: ({ txGroup, initiatorSigner }: { txGroup: import("..").SignerTransaction[]; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index 5fb80c48..127fec57 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -20,7 +20,6 @@ export interface DirectSwapQuote { priceImpact: number; /** The round that this quote is based on. */ round?: number; - price?: number; } /** An object containing information about a successfully executed swap. */ export interface V1SwapExecution { @@ -86,22 +85,6 @@ export declare type SwapQuote = { type: SwapQuoteType.Router; }); export declare type GetSwapQuoteBySwapTypeParams = Omit; -export declare type GetSwapQuoteWithContractVersionParams = { - type: SwapType; - pool: V2PoolInfo; - asset: AssetWithIdAndAmount; - decimals: { - assetIn: number; - assetOut: number; - }; - isSwapRouterEnabled?: boolean; -}; -export declare type GetFixedInputSwapQuoteByContractVersionParams = Omit & { - assetIn: AssetWithIdAndAmount; -}; -export declare type GetFixedOutputSwapQuoteByContractVersionParams = Omit & { - assetOut: AssetWithIdAndAmount; -}; export interface FetchSwapRouteQuotesPayload { asset_in_id: string; asset_out_id: string; diff --git a/dist/swap/v2/index.d.ts b/dist/swap/v2/index.d.ts index 62e5c352..75e7a288 100644 --- a/dist/swap/v2/index.d.ts +++ b/dist/swap/v2/index.d.ts @@ -1,6 +1,8 @@ import { Algodv2 } from "algosdk"; import { InitiatorSigner, SignerTransaction } from "../../util/commonTypes"; -import { GenerateSwapTxnsParams, GetFixedInputSwapQuoteByContractVersionParams, GetFixedOutputSwapQuoteByContractVersionParams, GetSwapQuoteWithContractVersionParams, SwapQuote, V2SwapExecution } from "../types"; +import { V2PoolInfo } from "../../util/pool/poolTypes"; +import { GenerateSwapTxnsParams, SwapQuote, V2SwapExecution } from "../types"; +import { SwapType } from "../constants"; import { AssetWithIdAndAmount } from "../../util/asset/assetModels"; declare function generateTxns(params: GenerateSwapTxnsParams): Promise; declare function signTxns({ txGroup, initiatorSigner }: { @@ -26,15 +28,34 @@ declare function execute({ client, quote, txGroup, signedTxns, assetIn }: { * @param decimals.assetOut - Decimals quantity for the output asset * @returns A promise for the Swap quote */ -declare function getQuote(params: GetSwapQuoteWithContractVersionParams): Promise; +declare function getQuote(type: SwapType, pool: V2PoolInfo, asset: AssetWithIdAndAmount, decimals: { + assetIn: number; + assetOut: number; +}, isSwapRouterEnabled?: boolean): Promise; /** * @returns A quote for a fixed input swap. Does NOT execute any transactions. */ -declare function getFixedInputSwapQuote({ pool, assetIn, decimals, isSwapRouterEnabled }: GetFixedInputSwapQuoteByContractVersionParams): Promise; +declare function getFixedInputSwapQuote({ assetIn, decimals, pool, isSwapRouterEnabled }: { + pool: V2PoolInfo; + assetIn: AssetWithIdAndAmount; + decimals: { + assetIn: number; + assetOut: number; + }; + isSwapRouterEnabled?: boolean; +}): Promise; /** * @returns A quote for a fixed output swap. Does NOT execute any transactions. */ -declare function getFixedOutputSwapQuote({ pool, assetOut, decimals, isSwapRouterEnabled }: GetFixedOutputSwapQuoteByContractVersionParams): Promise; +declare function getFixedOutputSwapQuote({ assetOut, decimals, pool, isSwapRouterEnabled }: { + pool: V2PoolInfo; + assetOut: AssetWithIdAndAmount; + decimals: { + assetIn: number; + assetOut: number; + }; + isSwapRouterEnabled?: boolean; +}): Promise; declare function calculateFixedInputSwap({ inputSupply, outputSupply, swapInputAmount, totalFeeShare, decimals }: { inputSupply: bigint; outputSupply: bigint; diff --git a/src/swap/types.ts b/src/swap/types.ts index 2cca8472..00a345a7 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -22,7 +22,6 @@ export interface DirectSwapQuote { priceImpact: number; /** The round that this quote is based on. */ round?: number; - price?: number; } /** An object containing information about a successfully executed swap. */ @@ -94,28 +93,6 @@ export type SwapQuote = export type GetSwapQuoteBySwapTypeParams = Omit; -export type GetSwapQuoteWithContractVersionParams = { - type: SwapType; - pool: V2PoolInfo; - asset: AssetWithIdAndAmount; - decimals: {assetIn: number; assetOut: number}; - isSwapRouterEnabled?: boolean; -}; - -export type GetFixedInputSwapQuoteByContractVersionParams = Omit< - GetSwapQuoteWithContractVersionParams, - "type" | "asset" -> & { - assetIn: AssetWithIdAndAmount; -}; - -export type GetFixedOutputSwapQuoteByContractVersionParams = Omit< - GetSwapQuoteWithContractVersionParams, - "type" | "asset" -> & { - assetOut: AssetWithIdAndAmount; -}; - export interface FetchSwapRouteQuotesPayload { asset_in_id: string; asset_out_id: string; diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index c77a197b..b5b4b19f 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -7,13 +7,10 @@ import { } from "../../util/util"; import {InitiatorSigner, SignerTransaction} from "../../util/commonTypes"; import TinymanError from "../../util/error/TinymanError"; -import {PoolStatus} from "../../util/pool/poolTypes"; +import {PoolStatus, V2PoolInfo} from "../../util/pool/poolTypes"; import { DirectSwapQuote, GenerateSwapTxnsParams, - GetFixedInputSwapQuoteByContractVersionParams, - GetFixedOutputSwapQuoteByContractVersionParams, - GetSwapQuoteWithContractVersionParams, SwapQuote, SwapQuoteType, V2SwapExecution @@ -199,9 +196,12 @@ async function execute({ * @returns A promise for the Swap quote */ async function getQuote( - params: GetSwapQuoteWithContractVersionParams + type: SwapType, + pool: V2PoolInfo, + asset: AssetWithIdAndAmount, + decimals: {assetIn: number; assetOut: number}, + isSwapRouterEnabled?: boolean ): Promise { - const {asset, decimals, pool, type, isSwapRouterEnabled} = params; let quote: SwapQuote; if (type === SwapType.FixedInput) { @@ -227,11 +227,16 @@ async function getQuote( * @returns A quote for a fixed input swap. Does NOT execute any transactions. */ async function getFixedInputSwapQuote({ - pool, assetIn, decimals, + pool, isSwapRouterEnabled -}: GetFixedInputSwapQuoteByContractVersionParams): Promise { +}: { + pool: V2PoolInfo; + assetIn: AssetWithIdAndAmount; + decimals: {assetIn: number; assetOut: number}; + isSwapRouterEnabled?: boolean; +}): Promise { if (pool.status !== PoolStatus.READY) { throw new TinymanError({pool, assetIn}, "Trying to swap on a non-existent pool"); } @@ -315,11 +320,16 @@ async function getFixedInputSwapQuote({ * @returns A quote for a fixed output swap. Does NOT execute any transactions. */ async function getFixedOutputSwapQuote({ - pool, assetOut, decimals, + pool, isSwapRouterEnabled -}: GetFixedOutputSwapQuoteByContractVersionParams): Promise { +}: { + pool: V2PoolInfo; + assetOut: AssetWithIdAndAmount; + decimals: {assetIn: number; assetOut: number}; + isSwapRouterEnabled?: boolean; +}): Promise { if (pool.status !== PoolStatus.READY) { throw new TinymanError({pool, assetOut}, "Trying to swap on a non-existent pool"); } From 5390599a905d06542c66382320e725c2255ba8ac Mon Sep 17 00:00:00 2001 From: anlerkan Date: Fri, 3 Mar 2023 14:35:48 +0300 Subject: [PATCH 10/39] feat(swap): generate txns from quote, swap router improvements --- dist/index.js | 2 +- dist/swap/index.d.ts | 5 ++- dist/swap/types.d.ts | 14 +++----- dist/swap/v1_1/index.d.ts | 4 +-- dist/swap/v2/index.d.ts | 3 +- dist/swap/v2/router/swap-router.d.ts | 9 +++-- src/swap/types.ts | 17 +++------ src/swap/utils.ts | 6 ++-- src/swap/v1_1/index.ts | 42 +++++++++++----------- src/swap/v2/index.ts | 49 ++++++++++++++++---------- src/swap/v2/router/swap-router.ts | 52 +++++++++++----------------- 11 files changed, 95 insertions(+), 108 deletions(-) diff --git a/dist/index.js b/dist/index.js index 1aefb1df..a557d497 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),a=Uint8Array.from([1]);function s(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E="- would result negative",N="logic eval error:",O="exceeds schema integer count",P=/transaction \w+:/,k=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function X(t,e){return j.apply(this,arguments)}function j(){return(j=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Q(t,e){return U.apply(this,arguments)}function U(){return(U=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,X(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function q(){for(var e=arguments.length,n=new Array(e),r=0;r1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function rt(t,e){var n=Number(t);return st({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function at(t,e){return st({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function st(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(ut(e),2),s=a[0],o=a[1],u=v(ut(Math.round(Number(ot(s,o+r)))),2),i=u[0],c=u[1];return Number(ot(i,c-r))}return Math.round(e)}function ot(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ut(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function it(t,e){return ct.apply(this,arguments)}function ct(){return(ct=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,tt(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new k(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function pt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function lt(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function dt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ft(t){return(new TextEncoder).encode(t)}var mt,gt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return ft("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),xt=(f(mt={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(mt,S.V2,{testnet:148607e3,mainnet:1002541853}),mt);function It(t,e){var n=xt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:It(a,s),note:gt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht,Tt,yt,vt;function _t(){return(_t=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:It(a,s),note:gt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(ht=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",ht.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(vt||(vt={}));var wt=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),bt=ft("swap"),Dt=(f(yt={},exports.SwapType.FixedInput,ft("fixed-input")),f(yt,exports.SwapType.FixedOutput,ft("fixed-output")),yt),St={ASSET_OPT_IN:ft("asset_opt_in")},Et={testnet:159521633,mainnet:0};function Nt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return rt(r.asset.decimals,Number(r.amount))/rt(n.asset.decimals,Number(n.amount))}function Ot(t){if(!Et[t])throw new Error("Unknown network or network not supported: ".concat(t));return Et[t]}function Pt(t){return kt.apply(this,arguments)}function kt(){return(kt=c(u().mark((function e(n){var a,s,o,i,c,p,l,d;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[St.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),d=r.default.assignGroupID([p]),e.abrupt("return",[{txn:d[0],signers:[i]}]);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(c[0].quote.amount_in.amount),Number(c[c.length-1].quote.amount_out.amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Ot(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[bt,Dt[i],r.default.encodeUint64(I)],foreignApps:[It(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p}),b=i===exports.SwapType.FixedInput?7:8,w.fee=t.ALGORAND_MIN_TX_FEE*(b+1),D=r.default.assignGroupID([_,w]),E=[{txn:D[vt.INPUT_TXN],signers:[a]},{txn:D[vt.APP_CALL_TXN],signers:[a]}],e.next=17,Rt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 17:if(!(e.sent.length>0)){e.next=25;break}return e.t0=q,e.next=22,Pt({client:s,accountAddress:a,assetIDs:[d,f,m],routerAppID:v});case 22:e.t1=e.sent,e.t2=E,E=(0,e.t0)(e.t1,e.t2);case 25:return e.abrupt("return",E);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Rt(t){return Bt.apply(this,arguments)}function Bt(){return(Bt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});case 5:return i=t.sent,t.abrupt("return",i.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Vt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Xt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},jt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Qt=new(function(n){m(a,jt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=It(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:Yt(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Jt(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?G({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Yt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Kt,Ht,Zt=ft("e");function $t(t){return te.apply(this,arguments)}function te(){return(te=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=G({stateArray:A}),T=e.fromByteArray(W([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(W([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(W([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ee(){return(ee=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=G({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mae)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ae,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ie(){return ie=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,zt(n,r);case 6:return o=t.sent,i=Jt(o,It(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[ne[S.V1_1].asset1],asset2ID:i[ne[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ie.apply(this,arguments)}var ce=Object.freeze({__proto__:null,getPoolInfo:se,getPoolReserves:function(t,e){return ue.apply(this,arguments)},getPoolAssets:function(t){return ie.apply(this,arguments)}});function pe(t){return le.apply(this,arguments)}function le(){return(le=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Wt(S.V2),i=o.generateLogicSigAccountForPool(e),c=It(r,S.V2),p=i.address(),l=M(a,s),t.next=8,zt(n,p);case 8:return d=t.sent,f=Jt(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[ne.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ne.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ne.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ne.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ne.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ne.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ne.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ne.v2.totalFeeShare]),m.poolTokenID=Number(f[ne.v2.poolTokenID]),m.asset1ID=Number(f[ne.v2.asset1]),m.asset2ID=Number(f[ne.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function de(){return(de=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,zt(e,n.account.address());case 2:return r=t.sent,a=Jt(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[ne.v2.asset1Reserves]),s.asset2=BigInt(a[ne.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[ne.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(){return(fe=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,zt(n,r);case 3:return s=t.sent,o=Jt(s,It(a,S.V2)),i=null,o&&(i={asset1ID:o[ne[S.V2].asset1],asset2ID:o[ne[S.V2].asset2],poolTokenID:o[ne[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var me=Object.freeze({__proto__:null,getPoolInfo:pe,getPoolReserves:function(t,e){return de.apply(this,arguments)},getPoolAssets:function(t){return fe.apply(this,arguments)}});function ge(t){return Boolean(t&&!(t.asset1+t.asset2))}var xe,Ie,Ae=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ge(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=rt(t.asset1,e.asset1)/rt(t.asset2,e.asset2)),r},isPoolEmpty:ge,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([se(t),pe(t)])}}),he=o((f(xe={},S.V1_1,o(o({},ce),Ae)),f(xe,S.V2,o(o({},me),Ae)),xe),Ae);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Ie||(Ie={}));var Te=96e4,ye=859e3;function ve(){return(ve=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=It(a,S.V1_1),T=Qt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:gt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[ft("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:F.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:_e(A),suggestedParams:c}),(E=[])[Ie.FUNDING_TXN]=D,E[Ie.VALIDATOR_APP_CALL]=_,E[Ie.POOL_TOKEN_CREATE]=w,E[Ie.ASSET1_OPT_IN]=b,A||(E[Ie.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),O=[{txn:N[Ie.FUNDING_TXN],signers:[i]},{txn:N[Ie.VALIDATOR_APP_CALL],signers:[y]},{txn:N[Ie.POOL_TOKEN_CREATE],signers:[y]},{txn:N[Ie.ASSET1_OPT_IN],signers:[y]}],N[Ie.ASSET2_OPT_IN]&&O.push({txn:N[Ie.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(t){return t?Te:ye}function we(){return(we=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Qt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ie.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function be(t){return De.apply(this,arguments)}function De(){return(De=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,tt(n,a[Ie.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Se(){return(Se=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,be({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",he.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ee,Ne={generateTxns:function(t){return ve.apply(this,arguments)},signTxns:function(t){return we.apply(this,arguments)},execute:function(t){return Se.apply(this,arguments)},getBootstrapFundingTxnAmount:_e};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ee||(Ee={}));var Oe=5,Pe=6;function ke(){return(ke=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=It(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,he.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Gt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[ft("bootstrap")],note:gt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Fe(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Le(h),suggestedParams:p}),(_=[])[Ee.FUNDING_TXN]=y,_[Ee.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Ee.FUNDING_TXN]={txn:w[Ee.FUNDING_TXN],signers:[c]},b[Ee.VALIDATOR_APP_CALL]={txn:w[Ee.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Le(t){return function(t,e){var n=Wt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Fe(t)+1e5}function Fe(e){return((e?Oe:Pe)+1)*t.ALGORAND_MIN_TX_FEE}function Re(){return(Re=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Gt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ee.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(){return(Be=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,X(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ne.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",he.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new k(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Me,Ce={generateTxns:function(t){return ke.apply(this,arguments)},signTxns:function(t){return Re.apply(this,arguments)},execute:function(t){return Be.apply(this,arguments)},getBootstrapFundingTxnAmount:Le,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Le(e)}};var Ve,Xe=(f(Me={},S.V1_1,Ne),f(Me,S.V2,Ce),f(Me,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Ne.generateTxns(t):Ce.generateTxns(t)})),f(Me,"signTxns",(function(t){return t.contractVersion===S.V1_1?Ne.signTxns(t):Ce.signTxns(t)})),f(Me,"execute",(function(t){return t.contractVersion===S.V1_1?Ne.execute(t):Ce.execute(t)})),f(Me,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Ne.getBootstrapFundingTxnAmount(n):Ce.getBootstrapFundingTxnAmount(n)})),Me);exports.V1_1AddLiquidityTxnIndices=void 0,(Ve=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ve.FEE_TXN=0]="FEE_TXN",Ve[Ve.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ve[Ve.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ve[Ve.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ve[Ve.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var je,Qe,Ue,qe,Ge=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(qe=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",qe.FLEXIBLE="flexible",qe.INITIAL="initial";var We,ze=(f(je={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(je,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(je,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),je),Je=(f(Qe={},exports.V2AddLiquidityType.INITIAL,1),f(Qe,exports.V2AddLiquidityType.SINGLE,2),f(Qe,exports.V2AddLiquidityType.FLEXIBLE,2),Qe),Ye=(f(Ue={},exports.V2AddLiquidityType.INITIAL,3),f(Ue,exports.V2AddLiquidityType.FLEXIBLE,3),f(Ue,exports.V2AddLiquidityType.SINGLE,2),Ue);function Ke(t){var e=t.assetIn,n=t.assetOut;return rt(n.decimals,Number(n.amount))/rt(e.decimals,Number(e.amount))}function He(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=Ke({assetIn:r,assetOut:a}),o=rt(a.decimals,Number(n))/rt(r.decimals,Number(e));return st({decimalPlaces:5},Math.abs(s/o-1))}function Ze(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,u=t.asset1Amount,i=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(u),d=s.asset2+BigInt(i),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(u)-x,h=BigInt(i)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=$e(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=$e(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:He({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function $e(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function tn(e){return(Je[e]+1)*t.ALGORAND_MIN_TX_FEE}var en=ft("add_liquidity"),nn=(f(We={},S.V1_1,[ft("mint")]),f(We,S.V2,{INITIAL_LIQUIDITY:[ft("add_initial_liquidity")],SINGLE_ASSET_MODE:[en,ft("single")],FLEXIBLE_MODE:[en,ft("flexible")]}),We);function rn(){return(rn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=nt("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:It(s,S.V1_1),appArgs:nn.v1_1,accounts:[d],note:gt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function an(){return(an=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sn(){return(sn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,$t({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,it(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=pt(a),x=lt(a),t.next=17,$t({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new k(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var on=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function On(){return(On=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!e.isUsingSwapRouter){t.next=2;break}return t.abrupt("return",Lt(e));case 2:if(n=e.assetIn,a=e.assetOut,s=e.client,o=e.initiatorAddr,i=e.pool,c=e.slippage,p=e.swapType,l=i.account.address(),(d=[i.asset1ID,i.asset2ID]).includes(n.id)&&d.includes(a.id)&&n.id!==a.id){t.next=7;break}throw new k({pool:i,assetIn:n,assetOut:a},"Input asset (#".concat(n.id,") and output asset (#").concat(a.id,") provided to generate transactions do not belong to the pool ").concat(l,"."));case 7:return t.next=9,s.getTransactionParams().do();case 9:return f=t.sent,m=C(n.id),g=p===exports.SwapType.FixedInput?n.amount:nt("positive",c,n.amount),x=p===exports.SwapType.FixedOutput?a.amount:nt("negative",c,a.amount),I=m?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,amount:g,assetIndex:n.id,suggestedParams:f}),(A=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:i.validatorAppID,appArgs:[bt,Dt[p],r.default.encodeUint64(x)],note:gt.getAppCallTxnNoteWithClientName(S.V2),accounts:[l],foreignAssets:[i.asset1ID,i.asset2ID],suggestedParams:f})).fee=Pn(p),(h=[])[vt.INPUT_TXN]=I,h[vt.APP_CALL_TXN]=A,T=r.default.assignGroupID(h),t.abrupt("return",[{txn:T[vt.INPUT_TXN],signers:[o]},{txn:T[vt.APP_CALL_TXN],signers:[o]}]);case 21:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(e){return(wt[e]+1)*t.ALGORAND_MIN_TX_FEE}function kn(){return(kn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,c=e.assetIn,t.next=3,it(a,[i]);case 3:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=10,Q(a,o);case 10:return g=t.sent,x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.route[s.route.length-1].quote.amount_out.asset.id),I=null==g||null===(n=g.find((function(t){return t.txn.txn.xaid===c.id})))||void 0===n?void 0:n.txn.txn,A=null==g||null===(r=g.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:f,assetIn:I&&{amount:BigInt(c.amount)-BigInt(I.aamt||0),id:c.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:m});case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ln(){return(Ln=c(u().mark((function t(e,n,r,a,s){var o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e!==exports.SwapType.FixedInput){t.next=6;break}return t.next=3,Fn({pool:n,assetIn:r,decimals:a,isSwapRouterEnabled:s});case 3:o=t.sent,t.next=9;break;case 6:return t.next=8,Bn({pool:n,assetOut:r,decimals:a,isSwapRouterEnabled:s});case 8:o=t.sent;case 9:return t.abrupt("return",o);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Fn(t){return Rn.apply(this,arguments)}function Rn(){return(Rn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,a.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:a,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(n.amount),c=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}p=a.asset2ID,l=a.asset1Reserves,d=a.asset2Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}p=a.asset1ID,l=a.asset2Reserves,d=a.asset1Reserves,t.next=18;break;case 17:throw new k({pool:a,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(f=Cn({inputSupply:l,outputSupply:d,swapInputAmount:i,totalFeeShare:c,decimals:r}),m=f.swapOutputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(m>d)){t.next=21;break}throw new Nn;case 21:if(I={assetInID:n.id,assetInAmount:i,assetOutID:p,assetOutAmount:m,swapFee:Number(g),rate:rt(r.assetOut,Number(m))/rt(r.assetIn,Number(i)),priceImpact:x},!s){t.next=28;break}return t.next=25,Mt({amount:n.amount,assetInID:Number(n.id),assetOutID:p,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_out.amount)>I.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},A),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:a,quote:I},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Bn(t){return Mn.apply(this,arguments)}function Mn(){return(Mn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,a.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:a,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(n.amount),c=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}p=a.asset2ID,l=a.asset2Reserves,d=a.asset1Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}p=a.asset1ID,l=a.asset1Reserves,d=a.asset2Reserves,t.next=18;break;case 17:throw new k({pool:a,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(f=Vn({inputSupply:l,outputSupply:d,swapOutputAmount:i,totalFeeShare:c,decimals:r}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(i>d)){t.next=21;break}throw new Nn;case 21:if(I={assetInID:p,assetInAmount:m,assetOutID:n.id,assetOutAmount:i,swapFee:Number(g),rate:rt(r.assetOut,Number(i))/rt(r.assetIn,Number(m)),priceImpact:x},!s){t.next=28;break}return t.next=25,Mt({amount:n.amount,assetInID:p,assetOutID:n.id,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Qn(){return(Qn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:gt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(nt("negative",l,c)),r.default.encodeUint64(nt("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[yn.ASSET_TRANSFER_TXN]=g,I[yn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[yn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[yn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Un(){return(Un=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=nt("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:gt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[yn.ASSET_TRANSFER_TXN]=T,v[yn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[yn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[yn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qn(){return(qn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,it(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Q(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Gn,Wn,zn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=jn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=jn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Xn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Xn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Qn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Un.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return qn.apply(this,arguments)}},Jn=(f(Gn={},S.V1_1,En),f(Gn,S.V2,zn),Gn),Yn=3n,Kn=1000n;function Hn(){return(Hn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===Wn.FEE_TXN_INDEX?c:e===Wn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.assetIn,c=e.assetOut,p=e.slippage,l=e.initiatorAddr,d=s.account.address(),(f=[s.asset1ID,s.asset2ID]).includes(i.id)&&f.includes(c.id)&&i.id!==c.id){t.next=5;break}throw new k({pool:s,assetIn:i,assetOut:c},"Input asset (#".concat(i.id,") and output asset (#").concat(c.id,") provided to generate transactions do not belong to the pool ").concat(d,"."));case 5:return t.next=7,n.getTransactionParams().do();case 7:return m=t.sent,g=[ft("swap"),o===exports.SwapType.FixedInput?ft("fi"):ft("fo")],x=r.default.makeApplicationNoOpTxnFromObject({from:d,appIndex:s.validatorAppID,appArgs:g,accounts:[l],note:gt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:m}),I=o===exports.SwapType.FixedOutput?nt("positive",p,i.amount):i.amount,A=0===i.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:i.id,amount:I,suggestedParams:m}),h=o===exports.SwapType.FixedInput?nt("negative",p,c.amount):c.amount,T=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:l,amount:h,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:l,assetIndex:c.id,amount:h,suggestedParams:m}),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.fee+T.fee,note:a,suggestedParams:m}),v=r.default.assignGroupID([y,x,A,T]),t.abrupt("return",[{txn:v[0],signers:[l]},{txn:v[1],signers:[d]},{txn:v[2],signers:[l]},{txn:v[3],signers:[d]}]);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new k({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new k({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*Yn/Kn,l=i-u*i/(u+(c-p));if(l>i)throw new Nn;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:Ke(d),priceImpact:He(o({inputSupply:u,outputSupply:i},d))}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,it(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new k({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new k({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new Nn;var c=o*u/(u-i)-o,p=c*Kn/(Kn-Yn),l=p-c,d=rt(a.assetOut,Number(i))/rt(a.assetIn,Number(p)),f=rt(a.assetOut,Number(u))/rt(a.assetIn,Number(o)),m=st({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m}}function rr(t){return ar.apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,it(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sr(){return(sr=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new k({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Wn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Wn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,tr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,rr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:lt(s),fees:pt(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new k(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Wn||(Wn={}));var or,ur={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?$n({pool:e,reserves:n,assetIn:r,decimals:a}):nr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:$n,getFixedOutputSwapQuote:nr,generateTxns:function(t){return Zn.apply(this,arguments)},signTxns:function(t){return Hn.apply(this,arguments)},execute:function(t){return sr.apply(this,arguments)},executeFixedOutputSwap:rr},ir=4*t.ALGORAND_MIN_TX_FEE;function cr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Nn})))throw new Nn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function pr(){return pr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedInputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Xn.getFixedInputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),pr.apply(this,arguments)}function lr(){return lr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedOutputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Xn.getFixedOutputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),lr.apply(this,arguments)}function dr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:Nt(t.route)}function fr(t){var e,n=t[0],r=dr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=dr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}var mr=(f(or={},S.V1_1,ur),f(or,S.V2,Xn),f(or,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ge(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return pr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return lr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(or,"generateTxns",(function(t){return t.isUsingSwapRouter||t.pool.contractVersion!==S.V1_1?Xn.generateTxns(t):ur.generateTxns(t)})),f(or,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&function(t){if(t.type===exports.SwapQuoteType.Direct)return t.quoteWithPool.pool.contractVersion;return S.V2}(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return ur.signTxns(o({pool:e},t))}return Xn.signTxns(t)})),f(or,"execute",(function(t){return t.contractVersion===S.V1_1?ur.execute(t):Xn.execute(t)})),or);function gr(){return(gr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,xr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,it(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:pt(a),confirmedRound:d,txnID:l,groupID:lt(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function xr(t){return Ir.apply(this,arguments)}function Ir(){return(Ir=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(){return Ar=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:pt(e),groupID:lt(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,it(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new k(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Ar.apply(this,arguments)}function hr(){return(hr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[ft("redeem")],note:gt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Tr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ft("commit"),i],note:W([ft("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function yr(){return(yr=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Tr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[ft("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=L,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Tn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Xe,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=F,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Jn,exports.Swap=mr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=vn,exports.V2AddLiquidityTxnIndices=ze,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=nt,exports.calculateAccountMinimumRequiredBalance=Yt,exports.combineAndRegroupSignerTxns=q,exports.convertFromBaseUnits=rt,exports.convertToBaseUnits=at,exports.generateOptIntoAssetTxns=function(t){return R.apply(this,arguments)},exports.generateOptIntoValidatorTxns=function(t){return At.apply(this,arguments)},exports.generateOptOutOfValidatorTxns=function(t){return _t.apply(this,arguments)},exports.generateRedeemTxns=function(t){return hr.apply(this,arguments)},exports.generateSwapRouterAssetOptInTransaction=Pt,exports.generateSwapRouterTxns=Lt,exports.getAccountExcess=function(t){return ee.apply(this,arguments)},exports.getAccountExcessWithinPool=$t,exports.getAccountInformation=zt,exports.getAddLiquidityTotalFee=function(e){switch(e.version){case S.V1_1:return Ge;case S.V2:return n=e.type,(Je[n]+Ye[n])*t.ALGORAND_MIN_TX_FEE;default:throw new Error("Provided contract version was not valid.")}var n},exports.getMinRequiredBalanceToOptIn=function(t){var e,n=t.currentMinumumBalanceForAccount,r=t.suggestedTransactionFee;if("app-opt-in"===t.type){var a=Wt(t.contractVersion);e=1e5+5e4*a.schema.numLocalByteSlices+28500*a.schema.numLocalInts}else e=1e5;return e+(n||0)+(r||0)},exports.getStakingAppID=function(t){return"testnet"===t?51948952:649588853},exports.getSwapFeesFromSwapRoute=function(t){return t.reduce((function(t,e){return t+Number(e.swap_fees.amount)}),0)},exports.getSwapRoute=Mt,exports.getSwapRouteRate=Nt,exports.getSwapRouterAppID=Ot,exports.getSwapRouterAppOptInRequiredAssetIDs=Rt,exports.getSwapTotalFee=function(e){switch(e.version){case S.V1_1:return ir;case S.V2:return n=e.type,(wt[n]+2)*t.ALGORAND_MIN_TX_FEE;default:throw new Error("Provided contract version was not valid.")}var n},exports.getTxnGroupID=lt,exports.getValidatorAppID=It,exports.hasSufficientMinimumBalance=function(t){return t.amount>=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=he,exports.prepareCommitTransactions=function(t){return yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Ar.apply(this,arguments)},exports.redeemExcessAsset=function(t){return gr.apply(this,arguments)},exports.sendAndWaitRawTransaction=it,exports.sumUpTxnFees=pt,exports.tinymanContract_v1_1=Qt,exports.tinymanContract_v2=Gt,exports.tinymanJSSDKConfig=gt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",s=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,s=Object.create(a.prototype),o=new w(r||[]);return s._invoke=function(t,e,n){var r="suspendedStart";return function(a,s){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw s;return D()}for(n.method=a,n.arg=s;;){var o=n.delegate;if(o){var u=y(o,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,o),s}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,a)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,s,o,u){var i=p(t[a],t,s);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,u)}),(function(t){r("throw",t,o,u)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function s(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(s,s):s()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function w(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,s=function e(){for(;++r=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function X(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[s?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function q(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=D(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var j=100000n,Q=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return j+Q*BigInt(o.length)+U*BigInt(s.length+a.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(nt(e),2),s=a[0],o=a[1],u=v(nt(Math.round(Number(et(s,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return at.apply(this,arguments)}function at(){return(at=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function st(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),Tt=it("swap"),yt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),vt={ASSET_OPT_IN:it("asset_opt_in")},_t={testnet:159521633,mainnet:0};function wt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function bt(t){if(!_t[t])throw new Error("Unknown network or network not supported: ".concat(t));return _t[t]}function Dt(t){return St.apply(this,arguments)}function St(){return(St=c(u().mark((function e(n){var a,s,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[vt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Et(t){return Ot.apply(this,arguments)}function Ot(){return(Ot=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E,O,N;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(c[0].quote.amount_in.amount),Number(c[c.length-1].quote.amount_out.amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=bt(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[Tt,yt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p}),b=i===exports.SwapType.FixedInput?7:8,w.fee=t.ALGORAND_MIN_TX_FEE*(b+1),D=[_,w],e.next=16,Nt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 16:if(!((E=e.sent).length>0)){e.next=22;break}return e.next=20,Dt({client:s,accountAddress:a,assetIDs:E,routerAppID:v});case 20:O=e.sent,D.unshift(O);case 22:return N=r.default.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[a]}})));case 24:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nt(t){return Pt.apply(this,arguments)}function Pt(){return(Pt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function kt(t){return Lt.apply(this,arguments)}function Lt(){return(Lt=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});case 5:return i=t.sent,t.abrupt("return",i.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Ft={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Rt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Bt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Mt=new(function(n){m(a,Bt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:Ut(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Qt(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?X({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Ut(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Gt,Wt,zt=it("e");function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=X({stateArray:A}),T=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,zt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,zt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,zt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Kt(){return(Kt=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=X({stateArray:l["key-value"]}),m=0,g=Object.entries(f);m$t)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat($t,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function re(){return re=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,jt(n,r);case 6:return o=t.sent,i=Qt(o,dt(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[Ht[S.V1_1].asset1],asset2ID:i[Ht[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),re.apply(this,arguments)}var ae=Object.freeze({__proto__:null,getPoolInfo:te,getPoolReserves:function(t,e){return ne.apply(this,arguments)},getPoolAssets:function(t){return re.apply(this,arguments)}});function se(t){return oe.apply(this,arguments)}function oe(){return(oe=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=qt(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(a,s),t.next=8,jt(n,p);case 8:return d=t.sent,f=Qt(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ht.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ht.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ht.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ht.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ht.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ht.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ht.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ht.v2.totalFeeShare]),m.poolTokenID=Number(f[Ht.v2.poolTokenID]),m.asset1ID=Number(f[Ht.v2.asset1]),m.asset2ID=Number(f[Ht.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ue(){return(ue=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,jt(e,n.account.address());case 2:return r=t.sent,a=Qt(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[Ht.v2.asset1Reserves]),s.asset2=BigInt(a[Ht.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[Ht.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ie(){return(ie=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,jt(n,r);case 3:return s=t.sent,o=Qt(s,dt(a,S.V2)),i=null,o&&(i={asset1ID:o[Ht[S.V2].asset1],asset2ID:o[Ht[S.V2].asset2],poolTokenID:o[Ht[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ce=Object.freeze({__proto__:null,getPoolInfo:se,getPoolReserves:function(t,e){return ue.apply(this,arguments)},getPoolAssets:function(t){return ie.apply(this,arguments)}});function pe(t){return Boolean(t&&!(t.asset1+t.asset2))}var le,de,fe=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=pe(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:pe,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([te(t),se(t)])}}),me=o((f(le={},S.V1_1,o(o({},ae),fe)),f(le,S.V2,o(o({},ce),fe)),le),fe);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(de||(de={}));var ge=96e4,xe=859e3;function Ie(){return(Ie=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,P;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(a,S.V1_1),T=Mt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Ae(A),suggestedParams:c}),(E=[])[de.FUNDING_TXN]=D,E[de.VALIDATOR_APP_CALL]=_,E[de.POOL_TOKEN_CREATE]=w,E[de.ASSET1_OPT_IN]=b,A||(E[de.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),P=[{txn:N[de.FUNDING_TXN],signers:[i]},{txn:N[de.VALIDATOR_APP_CALL],signers:[y]},{txn:N[de.POOL_TOKEN_CREATE],signers:[y]},{txn:N[de.ASSET1_OPT_IN],signers:[y]}],N[de.ASSET2_OPT_IN]&&P.push({txn:N[de.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",P);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ae(t){return t?ge:xe}function he(){return(he=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Mt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===de.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(t){return ye.apply(this,arguments)}function ye(){return(ye=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,a[de.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function ve(){return(ve=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Te({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",me.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var _e,we={generateTxns:function(t){return Ie.apply(this,arguments)},signTxns:function(t){return he.apply(this,arguments)},execute:function(t){return ve.apply(this,arguments)},getBootstrapFundingTxnAmount:Ae};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(_e||(_e={}));var be=5,De=6;function Se(t,e){return Ee.apply(this,arguments)}function Ee(){return(Ee=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Oe(t,e){return Ne.apply(this,arguments)}function Ne(){return(Ne=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Se(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pe(){return(Pe=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=dt(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,me.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Xt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Le(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:ke(h),suggestedParams:p}),(_=[])[_e.FUNDING_TXN]=y,_[_e.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[_e.FUNDING_TXN]={txn:w[_e.FUNDING_TXN],signers:[c]},b[_e.VALIDATOR_APP_CALL]={txn:w[_e.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ke(t){return function(t,e){var n=qt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Le(t)+1e5}function Le(e){return((e?be:De)+1)*t.ALGORAND_MIN_TX_FEE}function Fe(){return(Fe=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===_e.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Re(){return(Re=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Se(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Ht.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",me.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Be,Me={generateTxns:function(t){return Pe.apply(this,arguments)},signTxns:function(t){return Fe.apply(this,arguments)},execute:function(t){return Re.apply(this,arguments)},getBootstrapFundingTxnAmount:ke,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+ke(e)}};var Ce,Ve=(f(Be={},S.V1_1,we),f(Be,S.V2,Me),f(Be,"generateTxns",(function(t){return t.contractVersion===S.V1_1?we.generateTxns(t):Me.generateTxns(t)})),f(Be,"signTxns",(function(t){return t.contractVersion===S.V1_1?we.signTxns(t):Me.signTxns(t)})),f(Be,"execute",(function(t){return t.contractVersion===S.V1_1?we.execute(t):Me.execute(t)})),f(Be,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?we.getBootstrapFundingTxnAmount(n):Me.getBootstrapFundingTxnAmount(n)})),Be);exports.V1_1AddLiquidityTxnIndices=void 0,(Ce=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ce.FEE_TXN=0]="FEE_TXN",Ce[Ce.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ce[Ce.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ce[Ce.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ce[Ce.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Xe,qe,je,Qe,Ue=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Qe=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Qe.FLEXIBLE="flexible",Qe.INITIAL="initial";var Ge,We=(f(Xe={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Xe,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Xe,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Xe),ze=(f(qe={},exports.V2AddLiquidityType.INITIAL,1),f(qe,exports.V2AddLiquidityType.SINGLE,2),f(qe,exports.V2AddLiquidityType.FLEXIBLE,2),qe),Je=(f(je={},exports.V2AddLiquidityType.INITIAL,3),f(je,exports.V2AddLiquidityType.FLEXIBLE,3),f(je,exports.V2AddLiquidityType.SINGLE,2),je);function Ye(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function Ke(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=Ye({assetIn:r,assetOut:a}),o=Z(a.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(s/o-1))}function He(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,u=t.asset1Amount,i=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(u),d=s.asset2+BigInt(i),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(u)-x,h=BigInt(i)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=Ze(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=Ze(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:Ke({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function Ze(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function $e(e){return(ze[e]+1)*t.ALGORAND_MIN_TX_FEE}var tn=it("add_liquidity"),en=(f(Ge={},S.V1_1,[it("mint")]),f(Ge,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[tn,it("single")],FLEXIBLE_MODE:[tn,it("flexible")]}),Ge);function nn(){return(nn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(s,S.V1_1),appArgs:en.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rn(){return(rn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function an(){return(an=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Jt({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=st(a),x=ot(a),t.next=17,Jt({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var sn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function On(){return(On=c(u().mark((function t(e){var n,a,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Et(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,a=e.initiatorAddr,s=e.slippage,i=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=C(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:H("positive",s,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",s,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:l.validatorAppID,appArgs:[Tt,yt[i],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Nn(i),(_=[])[It.INPUT_TXN]=y,_[It.APP_CALL_TXN]=v,w=r.default.assignGroupID(_),t.abrupt("return",[{txn:w[It.INPUT_TXN],signers:[a]},{txn:w[It.APP_CALL_TXN],signers:[a]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nn(e){return(ht[e]+1)*t.ALGORAND_MIN_TX_FEE}function Pn(){return(Pn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,rt(a,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Oe(a,o);case 10:return m=t.sent,g=s.type===exports.SwapQuoteType.Direct?{id:s.quoteWithPool.quote.assetInID,amount:s.quoteWithPool.quote.assetInAmount}:{id:Number(s.asset_in_id),amount:Number(s.route[0].quote.amount_in.amount)},x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function kn(){return(kn=c(u().mark((function t(e,n,r,a,s){var o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e!==exports.SwapType.FixedInput){t.next=6;break}return t.next=3,Ln({pool:n,assetIn:r,decimals:a,isSwapRouterEnabled:s});case 3:o=t.sent,t.next=9;break;case 6:return t.next=8,Rn({pool:n,assetOut:r,decimals:a,isSwapRouterEnabled:s});case 8:o=t.sent;case 9:return t.abrupt("return",o);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ln(t){return Fn.apply(this,arguments)}function Fn(){return(Fn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(n.amount),c=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}p=a.asset2ID,l=a.asset1Reserves,d=a.asset2Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}p=a.asset1ID,l=a.asset2Reserves,d=a.asset1Reserves,t.next=18;break;case 17:throw new F({pool:a,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(f=Mn({inputSupply:l,outputSupply:d,swapInputAmount:i,totalFeeShare:c,decimals:r}),m=f.swapOutputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(m>d)){t.next=21;break}throw new En;case 21:if(I={assetInID:n.id,assetInAmount:i,assetOutID:p,assetOutAmount:m,swapFee:Number(g),rate:Z(r.assetOut,Number(m))/Z(r.assetIn,Number(i)),priceImpact:x},!s){t.next=28;break}return t.next=25,kt({amount:n.amount,assetInID:Number(n.id),assetOutID:p,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_out.amount)>I.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},A),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:a,quote:I},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rn(t){return Bn.apply(this,arguments)}function Bn(){return(Bn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(n.amount),c=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}p=a.asset2ID,l=a.asset2Reserves,d=a.asset1Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}p=a.asset1ID,l=a.asset1Reserves,d=a.asset2Reserves,t.next=18;break;case 17:throw new F({pool:a,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(f=Cn({inputSupply:l,outputSupply:d,swapOutputAmount:i,totalFeeShare:c,decimals:r}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(i>d)){t.next=21;break}throw new En;case 21:if(I={assetInID:p,assetInAmount:m,assetOutID:n.id,assetOutAmount:i,swapFee:Number(g),rate:Z(r.assetOut,Number(i))/Z(r.assetIn,Number(m)),priceImpact:x},!s){t.next=28;break}return t.next=25,kt({amount:n.amount,assetInID:p,assetOutID:n.id,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function qn(){return(qn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[vn,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[Tn.ASSET_TRANSFER_TXN]=g,I[Tn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[Tn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[Tn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jn(){return(jn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[vn,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[Tn.ASSET_TRANSFER_TXN]=T,v[Tn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[Tn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[Tn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Qn(){return(Qn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,rt(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Oe(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Un,Gn,Wn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Xn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=Xn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Vn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Vn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return qn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return jn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Qn.apply(this,arguments)}},zn=(f(Un={},S.V1_1,Sn),f(Un,S.V2,Wn),Un),Jn=3n,Yn=1000n;function Kn(){return(Kn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===Gn.FEE_TXN_INDEX?c:e===Gn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.quote,o=e.swapType,i=e.slippage,c=e.initiatorAddr,p=s.pool,l=s.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:s},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",i,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",i,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),_=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:a,suggestedParams:x}),w=r.default.assignGroupID([_,A,T,v]),t.abrupt("return",[{txn:w[0],signers:[c]},{txn:w[1],signers:[m]},{txn:w[2],signers:[c]},{txn:w[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*Jn/Yn,l=i-u*i/(u+(c-p));if(l>i)throw new En;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:Ye(d),priceImpact:Ke(o({inputSupply:u,outputSupply:i},d))}}function $n(t){return tr.apply(this,arguments)}function tr(){return(tr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Jt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Jt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function er(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new En;var c=o*u/(u-i)-o,p=c*Yn/(Yn-Jn),l=p-c,d=Z(a.assetOut,Number(i))/Z(a.assetIn,Number(p)),f=Z(a.assetOut,Number(u))/Z(a.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m}}function nr(t){return rr.apply(this,arguments)}function rr(){return(rr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Jt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Jt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Gn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Gn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Gn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Gn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,$n({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,nr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(s),fees:st(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Gn||(Gn={}));var sr,or={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Zn({pool:e,reserves:n,assetIn:r,decimals:a}):er({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Zn,getFixedOutputSwapQuote:er,generateTxns:function(t){return Hn.apply(this,arguments)},signTxns:function(t){return Kn.apply(this,arguments)},execute:function(t){return ar.apply(this,arguments)},executeFixedOutputSwap:nr},ur=4*t.ALGORAND_MIN_TX_FEE;function ir(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof En})))throw new En;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function cr(){return cr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:or.getFixedInputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Vn.getFixedInputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,ir(i);case 4:return p=t.sent,t.abrupt("return",dr(p));case 6:case"end":return t.stop()}}),t)}))),cr.apply(this,arguments)}function pr(){return pr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:or.getFixedOutputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Vn.getFixedOutputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,ir(i);case 4:return p=t.sent,t.abrupt("return",dr(p));case 6:case"end":return t.stop()}}),t)}))),pr.apply(this,arguments)}function lr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:wt(t.route)}function dr(t){var e,n=t[0],r=lr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=lr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}function fr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var mr=(f(sr={},S.V1_1,or),f(sr,S.V2,Vn),f(sr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return pe(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return cr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return pr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(sr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&fr(t.quote)===S.V1_1?or.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):Vn.generateTxns(t)})),f(sr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&fr(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return or.signTxns(o({pool:e},t))}return Vn.signTxns(t)})),f(sr,"execute",(function(t){return t.contractVersion===S.V1_1?or.execute(t):Vn.execute(t)})),sr);function gr(){return(gr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,xr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:st(a),confirmedRound:d,txnID:l,groupID:ot(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function xr(t){return Ir.apply(this,arguments)}function Ir(){return(Ir=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(){return Ar=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:st(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,rt(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Ar.apply(this,arguments)}function hr(){return(hr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Tr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:q([it("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function yr(){return(yr=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Tr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=hn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Ve,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=zn,exports.Swap=mr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=yn,exports.V2AddLiquidityTxnIndices=We,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=Ut,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=me,exports.prepareCommitTransactions=function(t){return yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Ar.apply(this,arguments)},exports.redeemExcessAsset=function(t){return gr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=st,exports.tinymanContract_v1_1=Mt,exports.tinymanContract_v2=Xt,exports.tinymanJSSDKConfig=pt; diff --git a/dist/swap/index.d.ts b/dist/swap/index.d.ts index a7c96e08..a3e8a2d7 100644 --- a/dist/swap/index.d.ts +++ b/dist/swap/index.d.ts @@ -23,7 +23,7 @@ export declare const Swap: { assetOut: number; }; }) => import("./types").DirectSwapQuote; - generateTxns: ({ client, pool, swapType, assetIn, assetOut, slippage, initiatorAddr }: import("./types").GenerateSwapTxnsWithoutRouterParams) => Promise; + generateTxns: ({ client, quote, swapType, slippage, initiatorAddr }: import("./types").GenerateV1_1SwapTxnsParams) => Promise; signTxns: ({ pool, txGroup, initiatorSigner }: { pool: import("..").V1PoolInfo; txGroup: import("..").SignerTransaction[]; @@ -74,12 +74,11 @@ export declare const Swap: { txGroup: import("..").SignerTransaction[]; initiatorSigner: import("..").InitiatorSigner; }) => Promise; - execute: ({ client, quote, txGroup, signedTxns, assetIn }: { + execute: ({ client, quote, txGroup, signedTxns }: { client: import("algosdk").Algodv2; quote: import("./types").SwapQuote; txGroup: import("..").SignerTransaction[]; signedTxns: Uint8Array[]; - assetIn: import("../util/asset/assetModels").AssetWithIdAndAmount; }) => Promise; calculateFixedInputSwap: ({ inputSupply, outputSupply, swapInputAmount, totalFeeShare, decimals }: { inputSupply: bigint; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index 127fec57..9cbcecb5 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -111,12 +111,11 @@ export declare enum SwapQuoteType { Direct = "direct", Router = "router" } -export interface GenerateSwapTxnsWithoutRouterParams { +export interface GenerateSwapTxnsParams { client: Algodv2; - pool: V1PoolInfo | V2PoolInfo; + network: SupportedNetwork; + quote: SwapQuote; swapType: SwapType; - assetIn: AssetWithIdAndAmount; - assetOut: AssetWithIdAndAmount; slippage: number; initiatorAddr: string; } @@ -127,13 +126,8 @@ export interface GenerateSwapRouterTxnsParams { route: SwapRoute; network: SupportedNetwork; } -export declare type GenerateSwapTxnsParams = (GenerateSwapTxnsWithoutRouterParams & { - isUsingSwapRouter: false; -}) | (GenerateSwapRouterTxnsParams & { - isUsingSwapRouter: true; -}); export declare type GenerateV1_1SwapTxnsParams = Omit & { - quote: DirectSwapQuote; + quote: SwapQuoteWithPool; }; export interface SwapRouterQuote { swap_type: SwapType; diff --git a/dist/swap/v1_1/index.d.ts b/dist/swap/v1_1/index.d.ts index e1d26409..00140977 100644 --- a/dist/swap/v1_1/index.d.ts +++ b/dist/swap/v1_1/index.d.ts @@ -1,7 +1,7 @@ import { Algodv2 } from "algosdk"; import { InitiatorSigner, SignerTransaction } from "../../util/commonTypes"; import { PoolReserves, V1PoolInfo } from "../../util/pool/poolTypes"; -import { DirectSwapQuote, GenerateSwapTxnsWithoutRouterParams, V1SwapExecution } from "../types"; +import { DirectSwapQuote, GenerateV1_1SwapTxnsParams, V1SwapExecution } from "../types"; import { SwapType } from "../constants"; import { AssetWithIdAndAmount } from "../../util/asset/assetModels"; declare function signTxns({ pool, txGroup, initiatorSigner }: { @@ -9,7 +9,7 @@ declare function signTxns({ pool, txGroup, initiatorSigner }: { txGroup: SignerTransaction[]; initiatorSigner: InitiatorSigner; }): Promise; -declare function generateTxns({ client, pool, swapType, assetIn, assetOut, slippage, initiatorAddr }: GenerateSwapTxnsWithoutRouterParams): Promise; +declare function generateTxns({ client, quote, swapType, slippage, initiatorAddr }: GenerateV1_1SwapTxnsParams): Promise; /** * * @param type - Type of the swap diff --git a/dist/swap/v2/index.d.ts b/dist/swap/v2/index.d.ts index 75e7a288..03cecd17 100644 --- a/dist/swap/v2/index.d.ts +++ b/dist/swap/v2/index.d.ts @@ -12,12 +12,11 @@ declare function signTxns({ txGroup, initiatorSigner }: { /** * Executes a swap with the desired quantities. */ -declare function execute({ client, quote, txGroup, signedTxns, assetIn }: { +declare function execute({ client, quote, txGroup, signedTxns }: { client: Algodv2; quote: SwapQuote; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; - assetIn: AssetWithIdAndAmount; }): Promise; /** * @param type - Type of the swap diff --git a/dist/swap/v2/router/swap-router.d.ts b/dist/swap/v2/router/swap-router.d.ts index efaecc07..bba15c66 100644 --- a/dist/swap/v2/router/swap-router.d.ts +++ b/dist/swap/v2/router/swap-router.d.ts @@ -1,5 +1,5 @@ +import algosdk, { Transaction } from "algosdk"; import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; -import { SignerTransaction } from "../../../util/commonTypes"; import { SwapType } from "../../constants"; import { FetchSwapRouteQuotesResponse, GenerateSwapRouterTxnsParams } from "../../types"; export declare function generateSwapRouterAssetOptInTransaction({ client, routerAppID, assetIDs, accountAddress }: { @@ -7,8 +7,11 @@ export declare function generateSwapRouterAssetOptInTransaction({ client, router routerAppID: number; assetIDs: number[]; accountAddress: string; -}): Promise; -export declare function generateSwapRouterTxns({ initiatorAddr, client, network, swapType, route }: GenerateSwapRouterTxnsParams): Promise; +}): Promise; +export declare function generateSwapRouterTxns({ initiatorAddr, client, network, swapType, route }: GenerateSwapRouterTxnsParams): Promise<{ + txn: algosdk.Transaction; + signers: string[]; +}[]>; export declare function getSwapRouterAppOptInRequiredAssetIDs({ client, routerAppID, assetIDs }: { client: AlgodClient; routerAppID: number; diff --git a/src/swap/types.ts b/src/swap/types.ts index 00a345a7..732f49fb 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -124,12 +124,11 @@ export enum SwapQuoteType { Router = "router" } -export interface GenerateSwapTxnsWithoutRouterParams { +export interface GenerateSwapTxnsParams { client: Algodv2; - pool: V1PoolInfo | V2PoolInfo; + network: SupportedNetwork; + quote: SwapQuote; swapType: SwapType; - assetIn: AssetWithIdAndAmount; - assetOut: AssetWithIdAndAmount; slippage: number; initiatorAddr: string; } @@ -142,16 +141,8 @@ export interface GenerateSwapRouterTxnsParams { network: SupportedNetwork; } -export type GenerateSwapTxnsParams = - | (GenerateSwapTxnsWithoutRouterParams & { - isUsingSwapRouter: false; - }) - | (GenerateSwapRouterTxnsParams & { - isUsingSwapRouter: true; - }); - export type GenerateV1_1SwapTxnsParams = Omit & { - quote: DirectSwapQuote; + quote: SwapQuoteWithPool; }; export interface SwapRouterQuote { diff --git a/src/swap/utils.ts b/src/swap/utils.ts index e07b07d4..b64f25dd 100644 --- a/src/swap/utils.ts +++ b/src/swap/utils.ts @@ -193,10 +193,10 @@ export function generateTxns( params: GenerateSwapTxnsParams ): Promise { if ( - !params.isUsingSwapRouter && - params.pool.contractVersion === CONTRACT_VERSION.V1_1 + params.quote.type === SwapQuoteType.Direct && + getContractVersionFromSwapQuote(params.quote) === CONTRACT_VERSION.V1_1 ) { - return SwapV1_1.generateTxns(params); + return SwapV1_1.generateTxns({...params, quote: params.quote.quoteWithPool}); } return SwapV2.generateTxns(params); diff --git a/src/swap/v1_1/index.ts b/src/swap/v1_1/index.ts index 99581f92..79fa1624 100644 --- a/src/swap/v1_1/index.ts +++ b/src/swap/v1_1/index.ts @@ -15,11 +15,7 @@ import {DEFAULT_FEE_TXN_NOTE} from "../../util/constant"; import {ALGO_ASSET_ID} from "../../util/asset/assetConstants"; import {PoolReserves, PoolStatus, V1PoolInfo} from "../../util/pool/poolTypes"; import {getAccountExcessWithinPool} from "../../util/account/accountUtils"; -import { - DirectSwapQuote, - GenerateSwapTxnsWithoutRouterParams, - V1SwapExecution -} from "../types"; +import {DirectSwapQuote, GenerateV1_1SwapTxnsParams, V1SwapExecution} from "../types"; import {SwapType} from "../constants"; import {calculatePriceImpact, calculateSwapRate} from "../common/utils"; import OutputAmountExceedsAvailableLiquidityError from "../../util/error/OutputAmountExceedsAvailableLiquidityError"; @@ -66,24 +62,26 @@ async function signTxns({ async function generateTxns({ client, - pool, + quote, swapType, - assetIn, - assetOut, slippage, initiatorAddr -}: GenerateSwapTxnsWithoutRouterParams): Promise { +}: GenerateV1_1SwapTxnsParams): Promise { + const {pool, quote: swapQuote} = quote; + + const {assetInID, assetOutID} = swapQuote; + const poolAddress = pool.account.address(); const poolAssets = [pool.asset1ID, pool.asset2ID]; if ( - !poolAssets.includes(assetIn.id) || - !poolAssets.includes(assetOut.id) || - assetIn.id === assetOut.id + !poolAssets.includes(assetInID) || + !poolAssets.includes(assetOutID) || + assetInID === assetOutID ) { throw new TinymanError( - {pool, assetIn, assetOut}, - `Input asset (#${assetIn.id}) and output asset (#${assetOut.id}) provided to generate transactions do not belong to the pool ${poolAddress}.` + {pool, quote}, + `Input asset (#${assetInID}) and output asset (#${assetOutID}) provided to generate transactions do not belong to the pool ${poolAddress}.` ); } @@ -108,11 +106,11 @@ async function generateTxns({ const assetInAmount = swapType === SwapType.FixedOutput - ? applySlippageToAmount("positive", slippage, assetIn.amount) - : assetIn.amount; + ? applySlippageToAmount("positive", slippage, swapQuote.assetInAmount) + : swapQuote.assetInAmount; let assetInTxn: algosdk.Transaction; - if (assetIn.id === ALGO_ASSET_ID) { + if (assetInID === ALGO_ASSET_ID) { assetInTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({ from: initiatorAddr, to: poolAddress, @@ -123,7 +121,7 @@ async function generateTxns({ assetInTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ from: initiatorAddr, to: poolAddress, - assetIndex: assetIn.id, + assetIndex: assetInID, amount: assetInAmount, suggestedParams }); @@ -131,11 +129,11 @@ async function generateTxns({ const assetOutAmount = swapType === SwapType.FixedInput - ? applySlippageToAmount("negative", slippage, assetOut.amount) - : assetOut.amount; + ? applySlippageToAmount("negative", slippage, swapQuote.assetOutAmount) + : swapQuote.assetOutAmount; let assetOutTxn: algosdk.Transaction; - if (assetOut.id === ALGO_ASSET_ID) { + if (assetOutID === ALGO_ASSET_ID) { assetOutTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({ from: poolAddress, to: initiatorAddr, @@ -146,7 +144,7 @@ async function generateTxns({ assetOutTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ from: poolAddress, to: initiatorAddr, - assetIndex: assetOut.id, + assetIndex: assetOutID, amount: assetOutAmount, suggestedParams }); diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index b5b4b19f..b855c57f 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -34,36 +34,41 @@ import {generateSwapRouterTxns, getSwapRoute} from "./router/swap-router"; async function generateTxns( params: GenerateSwapTxnsParams ): Promise { - if (params.isUsingSwapRouter) { - return generateSwapRouterTxns(params); + if (params.quote.type === SwapQuoteType.Router) { + return generateSwapRouterTxns({...params, route: params.quote.route}); } - const {assetIn, assetOut, client, initiatorAddr, pool, slippage, swapType} = params; + const {client, initiatorAddr, slippage, swapType, quote} = params; + + const { + quoteWithPool: {pool, quote: swapQuote} + } = quote; + const {assetInID, assetOutID} = swapQuote; const poolAddress = pool.account.address(); const poolAssets = [pool.asset1ID, pool.asset2ID]; if ( - !poolAssets.includes(assetIn.id) || - !poolAssets.includes(assetOut.id) || - assetIn.id === assetOut.id + !poolAssets.includes(assetInID) || + !poolAssets.includes(assetOutID) || + assetInID === assetOutID ) { throw new TinymanError( - {pool, assetIn, assetOut}, - `Input asset (#${assetIn.id}) and output asset (#${assetOut.id}) provided to generate transactions do not belong to the pool ${poolAddress}.` + {pool, quote}, + `Input asset (#${assetInID}) and output asset (#${assetOutID}) provided to generate transactions do not belong to the pool ${poolAddress}.` ); } const suggestedParams = await client.getTransactionParams().do(); - const isAssetInAlgo = isAlgo(assetIn.id); + const isAssetInAlgo = isAlgo(assetInID); const assetInAmount = swapType === SwapType.FixedInput - ? assetIn.amount - : applySlippageToAmount("positive", slippage, assetIn.amount); + ? swapQuote.assetInAmount + : applySlippageToAmount("positive", slippage, swapQuote.assetInAmount); const assetOutAmount = swapType === SwapType.FixedOutput - ? assetOut.amount - : applySlippageToAmount("negative", slippage, assetOut.amount); + ? swapQuote.assetOutAmount + : applySlippageToAmount("negative", slippage, swapQuote.assetOutAmount); /** * If the input asset is Algo, a payment txn, otherwise an asset transfer txn is required @@ -79,7 +84,7 @@ async function generateTxns( from: initiatorAddr, to: poolAddress, amount: assetInAmount, - assetIndex: assetIn.id, + assetIndex: assetInID, suggestedParams }); @@ -142,21 +147,29 @@ async function execute({ client, quote, txGroup, - signedTxns, - assetIn + signedTxns }: { client: Algodv2; quote: SwapQuote; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; - assetIn: AssetWithIdAndAmount; }): Promise { let [{confirmedRound, txnID}] = await sendAndWaitRawTransaction(client, [signedTxns]); const innerTxns = await getAppCallInnerTxns(client, txGroup); + const assetIn: AssetWithIdAndAmount = + quote.type === SwapQuoteType.Direct + ? { + id: quote.quoteWithPool.quote.assetInID, + amount: quote.quoteWithPool.quote.assetInAmount + } + : { + id: Number(quote.asset_in_id), + amount: Number(quote.route[0].quote.amount_in.amount) + }; const assetOutId = quote.type === SwapQuoteType.Direct ? quote.quoteWithPool.quote.assetOutID - : Number(quote.route[quote.route.length - 1].quote.amount_out.asset.id); + : Number(quote.asset_out_id); /** * If the swap type if Fixed Output, usually there will be a difference between * input amount and the actual used input amount. The change will be returned to the user diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index 70bf76e4..9904f142 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -1,10 +1,9 @@ -import algosdk, {ALGORAND_MIN_TX_FEE, getApplicationAddress} from "algosdk"; +import algosdk, {ALGORAND_MIN_TX_FEE, getApplicationAddress, Transaction} from "algosdk"; import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; import {CONTRACT_VERSION} from "../../../contract/constants"; +import {ALGO_ASSET_ID} from "../../../util/asset/assetConstants"; import {getAssetId, isAlgo} from "../../../util/asset/assetUtils"; -import {SignerTransaction} from "../../../util/commonTypes"; -import {combineAndRegroupSignerTxns} from "../../../util/transaction/transactionUtils"; import {getValidatorAppID} from "../../../validator"; import {SwapType} from "../../constants"; import { @@ -13,7 +12,6 @@ import { GenerateSwapRouterTxnsParams } from "../../types"; import { - V2SwapTxnGroupIndices, V2_SWAP_APP_CALL_ARG_ENCODED, V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED, V2_SWAP_ROUTER_APP_ARGS_ENCODED @@ -30,7 +28,7 @@ export async function generateSwapRouterAssetOptInTransaction({ routerAppID: number; assetIDs: number[]; accountAddress: string; -}): Promise { +}): Promise { const suggestedParams = await client.getTransactionParams().do(); // We need to create a NoOp transaction for each asset we want to opt-in to const assetOptInTxn = algosdk.makeApplicationNoOpTxnFromObject({ @@ -47,9 +45,7 @@ export async function generateSwapRouterAssetOptInTransaction({ // multiplied by the number of inner transactions assetOptInTxn.fee = ALGORAND_MIN_TX_FEE * (innerTransactionCount + 1); - const txGroup = algosdk.assignGroupID([assetOptInTxn]); - - return [{txn: txGroup[0], signers: [accountAddress]}]; + return assetOptInTxn; } export async function generateSwapRouterTxns({ @@ -109,18 +105,7 @@ export async function generateSwapRouterTxns({ routerAppCallTxn.fee = ALGORAND_MIN_TX_FEE * (innerTransactionCount + 1); - const txGroup = algosdk.assignGroupID([inputTxn, routerAppCallTxn]); - - let txns: SignerTransaction[] = [ - { - txn: txGroup[V2SwapTxnGroupIndices.INPUT_TXN], - signers: [initiatorAddr] - }, - { - txn: txGroup[V2SwapTxnGroupIndices.APP_CALL_TXN], - signers: [initiatorAddr] - } - ]; + const txnList = [inputTxn, routerAppCallTxn]; const swapRouterAppOptInRequiredAssetIDs = await getSwapRouterAppOptInRequiredAssetIDs({ client, @@ -129,18 +114,22 @@ export async function generateSwapRouterTxns({ }); if (swapRouterAppOptInRequiredAssetIDs.length > 0) { - txns = combineAndRegroupSignerTxns( - await generateSwapRouterAssetOptInTransaction({ - client, - accountAddress: initiatorAddr, - assetIDs: [assetInID, intermediaryAssetID, assetOutID], - routerAppID - }), - txns - ); + const routerAppOptInTxn = await generateSwapRouterAssetOptInTransaction({ + client, + accountAddress: initiatorAddr, + assetIDs: swapRouterAppOptInRequiredAssetIDs, + routerAppID + }); + + txnList.unshift(routerAppOptInTxn); } - return txns; + const txGroup = algosdk.assignGroupID(txnList); + + return txGroup.map((txn: Transaction) => ({ + txn, + signers: [initiatorAddr] + })); } export async function getSwapRouterAppOptInRequiredAssetIDs({ @@ -158,7 +147,8 @@ export async function getSwapRouterAppOptInRequiredAssetIDs({ (asset: {"asset-id": number}) => asset["asset-id"] ); const requiredAssetIDs = assetIDs.filter( - (assetID: number) => !appOptedInAssetIDs.includes(assetID) + (assetID: number) => + assetID !== ALGO_ASSET_ID && !appOptedInAssetIDs.includes(assetID) ); return requiredAssetIDs; From b689505f16c1bb7078280f20cf2ccfea9f0f50b4 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Fri, 3 Mar 2023 17:33:20 +0300 Subject: [PATCH 11/39] fix(swap): fix generate swap txns params --- dist/swap/types.d.ts | 2 +- src/swap/types.ts | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index 9cbcecb5..9c799463 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -126,7 +126,7 @@ export interface GenerateSwapRouterTxnsParams { route: SwapRoute; network: SupportedNetwork; } -export declare type GenerateV1_1SwapTxnsParams = Omit & { +export declare type GenerateV1_1SwapTxnsParams = Omit & { quote: SwapQuoteWithPool; }; export interface SwapRouterQuote { diff --git a/src/swap/types.ts b/src/swap/types.ts index 732f49fb..ef56a491 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -141,7 +141,10 @@ export interface GenerateSwapRouterTxnsParams { network: SupportedNetwork; } -export type GenerateV1_1SwapTxnsParams = Omit & { +export type GenerateV1_1SwapTxnsParams = Omit< + GenerateSwapTxnsParams, + "quote" | "network" +> & { quote: SwapQuoteWithPool; }; From 431ae5939d62a3b41abdf08384c7a729c20dff23 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Mon, 6 Mar 2023 15:30:07 +0300 Subject: [PATCH 12/39] fix(swap-router): support different networks while getting route --- dist/index.js | 2 +- dist/swap/index.d.ts | 8 +++++--- dist/swap/v2/index.d.ts | 10 ++++++---- dist/swap/v2/router/constants.d.ts | 5 +++++ dist/swap/v2/router/swap-router.d.ts | 4 +++- src/swap/utils.ts | 6 ++++-- src/swap/v2/index.ts | 28 +++++++++++++++++++++------- src/swap/v2/router/constants.ts | 16 ++++++++++++++++ src/swap/v2/router/swap-router.ts | 8 ++++++-- 9 files changed, 67 insertions(+), 20 deletions(-) diff --git a/dist/index.js b/dist/index.js index a557d497..dc7cb197 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),a=Uint8Array.from([1]);function s(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function X(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[s?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function q(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=D(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var j=100000n,Q=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return j+Q*BigInt(o.length)+U*BigInt(s.length+a.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(nt(e),2),s=a[0],o=a[1],u=v(nt(Math.round(Number(et(s,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return at.apply(this,arguments)}function at(){return(at=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function st(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),Tt=it("swap"),yt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),vt={ASSET_OPT_IN:it("asset_opt_in")},_t={testnet:159521633,mainnet:0};function wt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function bt(t){if(!_t[t])throw new Error("Unknown network or network not supported: ".concat(t));return _t[t]}function Dt(t){return St.apply(this,arguments)}function St(){return(St=c(u().mark((function e(n){var a,s,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[vt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Et(t){return Ot.apply(this,arguments)}function Ot(){return(Ot=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E,O,N;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(c[0].quote.amount_in.amount),Number(c[c.length-1].quote.amount_out.amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=bt(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[Tt,yt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p}),b=i===exports.SwapType.FixedInput?7:8,w.fee=t.ALGORAND_MIN_TX_FEE*(b+1),D=[_,w],e.next=16,Nt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 16:if(!((E=e.sent).length>0)){e.next=22;break}return e.next=20,Dt({client:s,accountAddress:a,assetIDs:E,routerAppID:v});case 20:O=e.sent,D.unshift(O);case 22:return N=r.default.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[a]}})));case 24:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nt(t){return Pt.apply(this,arguments)}function Pt(){return(Pt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function kt(t){return Lt.apply(this,arguments)}function Lt(){return(Lt=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});case 5:return i=t.sent,t.abrupt("return",i.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Ft={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Rt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Bt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Mt=new(function(n){m(a,Bt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:Ut(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Qt(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?X({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Ut(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Gt,Wt,zt=it("e");function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=X({stateArray:A}),T=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,zt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,zt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,zt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Kt(){return(Kt=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=X({stateArray:l["key-value"]}),m=0,g=Object.entries(f);m$t)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat($t,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function re(){return re=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,jt(n,r);case 6:return o=t.sent,i=Qt(o,dt(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[Ht[S.V1_1].asset1],asset2ID:i[Ht[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),re.apply(this,arguments)}var ae=Object.freeze({__proto__:null,getPoolInfo:te,getPoolReserves:function(t,e){return ne.apply(this,arguments)},getPoolAssets:function(t){return re.apply(this,arguments)}});function se(t){return oe.apply(this,arguments)}function oe(){return(oe=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=qt(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(a,s),t.next=8,jt(n,p);case 8:return d=t.sent,f=Qt(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ht.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ht.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ht.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ht.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ht.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ht.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ht.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ht.v2.totalFeeShare]),m.poolTokenID=Number(f[Ht.v2.poolTokenID]),m.asset1ID=Number(f[Ht.v2.asset1]),m.asset2ID=Number(f[Ht.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ue(){return(ue=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,jt(e,n.account.address());case 2:return r=t.sent,a=Qt(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[Ht.v2.asset1Reserves]),s.asset2=BigInt(a[Ht.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[Ht.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ie(){return(ie=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,jt(n,r);case 3:return s=t.sent,o=Qt(s,dt(a,S.V2)),i=null,o&&(i={asset1ID:o[Ht[S.V2].asset1],asset2ID:o[Ht[S.V2].asset2],poolTokenID:o[Ht[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ce=Object.freeze({__proto__:null,getPoolInfo:se,getPoolReserves:function(t,e){return ue.apply(this,arguments)},getPoolAssets:function(t){return ie.apply(this,arguments)}});function pe(t){return Boolean(t&&!(t.asset1+t.asset2))}var le,de,fe=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=pe(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:pe,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([te(t),se(t)])}}),me=o((f(le={},S.V1_1,o(o({},ae),fe)),f(le,S.V2,o(o({},ce),fe)),le),fe);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(de||(de={}));var ge=96e4,xe=859e3;function Ie(){return(Ie=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,P;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(a,S.V1_1),T=Mt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Ae(A),suggestedParams:c}),(E=[])[de.FUNDING_TXN]=D,E[de.VALIDATOR_APP_CALL]=_,E[de.POOL_TOKEN_CREATE]=w,E[de.ASSET1_OPT_IN]=b,A||(E[de.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),P=[{txn:N[de.FUNDING_TXN],signers:[i]},{txn:N[de.VALIDATOR_APP_CALL],signers:[y]},{txn:N[de.POOL_TOKEN_CREATE],signers:[y]},{txn:N[de.ASSET1_OPT_IN],signers:[y]}],N[de.ASSET2_OPT_IN]&&P.push({txn:N[de.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",P);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ae(t){return t?ge:xe}function he(){return(he=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Mt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===de.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(t){return ye.apply(this,arguments)}function ye(){return(ye=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,a[de.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function ve(){return(ve=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Te({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",me.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var _e,we={generateTxns:function(t){return Ie.apply(this,arguments)},signTxns:function(t){return he.apply(this,arguments)},execute:function(t){return ve.apply(this,arguments)},getBootstrapFundingTxnAmount:Ae};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(_e||(_e={}));var be=5,De=6;function Se(t,e){return Ee.apply(this,arguments)}function Ee(){return(Ee=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Oe(t,e){return Ne.apply(this,arguments)}function Ne(){return(Ne=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Se(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pe(){return(Pe=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=dt(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,me.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Xt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Le(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:ke(h),suggestedParams:p}),(_=[])[_e.FUNDING_TXN]=y,_[_e.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[_e.FUNDING_TXN]={txn:w[_e.FUNDING_TXN],signers:[c]},b[_e.VALIDATOR_APP_CALL]={txn:w[_e.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ke(t){return function(t,e){var n=qt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Le(t)+1e5}function Le(e){return((e?be:De)+1)*t.ALGORAND_MIN_TX_FEE}function Fe(){return(Fe=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===_e.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Re(){return(Re=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Se(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Ht.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",me.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Be,Me={generateTxns:function(t){return Pe.apply(this,arguments)},signTxns:function(t){return Fe.apply(this,arguments)},execute:function(t){return Re.apply(this,arguments)},getBootstrapFundingTxnAmount:ke,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+ke(e)}};var Ce,Ve=(f(Be={},S.V1_1,we),f(Be,S.V2,Me),f(Be,"generateTxns",(function(t){return t.contractVersion===S.V1_1?we.generateTxns(t):Me.generateTxns(t)})),f(Be,"signTxns",(function(t){return t.contractVersion===S.V1_1?we.signTxns(t):Me.signTxns(t)})),f(Be,"execute",(function(t){return t.contractVersion===S.V1_1?we.execute(t):Me.execute(t)})),f(Be,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?we.getBootstrapFundingTxnAmount(n):Me.getBootstrapFundingTxnAmount(n)})),Be);exports.V1_1AddLiquidityTxnIndices=void 0,(Ce=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ce.FEE_TXN=0]="FEE_TXN",Ce[Ce.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ce[Ce.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ce[Ce.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ce[Ce.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Xe,qe,je,Qe,Ue=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Qe=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Qe.FLEXIBLE="flexible",Qe.INITIAL="initial";var Ge,We=(f(Xe={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Xe,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Xe,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Xe),ze=(f(qe={},exports.V2AddLiquidityType.INITIAL,1),f(qe,exports.V2AddLiquidityType.SINGLE,2),f(qe,exports.V2AddLiquidityType.FLEXIBLE,2),qe),Je=(f(je={},exports.V2AddLiquidityType.INITIAL,3),f(je,exports.V2AddLiquidityType.FLEXIBLE,3),f(je,exports.V2AddLiquidityType.SINGLE,2),je);function Ye(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function Ke(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=Ye({assetIn:r,assetOut:a}),o=Z(a.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(s/o-1))}function He(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,u=t.asset1Amount,i=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(u),d=s.asset2+BigInt(i),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(u)-x,h=BigInt(i)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=Ze(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=Ze(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:Ke({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function Ze(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function $e(e){return(ze[e]+1)*t.ALGORAND_MIN_TX_FEE}var tn=it("add_liquidity"),en=(f(Ge={},S.V1_1,[it("mint")]),f(Ge,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[tn,it("single")],FLEXIBLE_MODE:[tn,it("flexible")]}),Ge);function nn(){return(nn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(s,S.V1_1),appArgs:en.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rn(){return(rn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function an(){return(an=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Jt({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=st(a),x=ot(a),t.next=17,Jt({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var sn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function On(){return(On=c(u().mark((function t(e){var n,a,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Et(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,a=e.initiatorAddr,s=e.slippage,i=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=C(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:H("positive",s,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",s,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:l.validatorAppID,appArgs:[Tt,yt[i],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Nn(i),(_=[])[It.INPUT_TXN]=y,_[It.APP_CALL_TXN]=v,w=r.default.assignGroupID(_),t.abrupt("return",[{txn:w[It.INPUT_TXN],signers:[a]},{txn:w[It.APP_CALL_TXN],signers:[a]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nn(e){return(ht[e]+1)*t.ALGORAND_MIN_TX_FEE}function Pn(){return(Pn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,rt(a,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Oe(a,o);case 10:return m=t.sent,g=s.type===exports.SwapQuoteType.Direct?{id:s.quoteWithPool.quote.assetInID,amount:s.quoteWithPool.quote.assetInAmount}:{id:Number(s.asset_in_id),amount:Number(s.route[0].quote.amount_in.amount)},x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function kn(){return(kn=c(u().mark((function t(e,n,r,a,s){var o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e!==exports.SwapType.FixedInput){t.next=6;break}return t.next=3,Ln({pool:n,assetIn:r,decimals:a,isSwapRouterEnabled:s});case 3:o=t.sent,t.next=9;break;case 6:return t.next=8,Rn({pool:n,assetOut:r,decimals:a,isSwapRouterEnabled:s});case 8:o=t.sent;case 9:return t.abrupt("return",o);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ln(t){return Fn.apply(this,arguments)}function Fn(){return(Fn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(n.amount),c=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}p=a.asset2ID,l=a.asset1Reserves,d=a.asset2Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}p=a.asset1ID,l=a.asset2Reserves,d=a.asset1Reserves,t.next=18;break;case 17:throw new F({pool:a,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(f=Mn({inputSupply:l,outputSupply:d,swapInputAmount:i,totalFeeShare:c,decimals:r}),m=f.swapOutputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(m>d)){t.next=21;break}throw new En;case 21:if(I={assetInID:n.id,assetInAmount:i,assetOutID:p,assetOutAmount:m,swapFee:Number(g),rate:Z(r.assetOut,Number(m))/Z(r.assetIn,Number(i)),priceImpact:x},!s){t.next=28;break}return t.next=25,kt({amount:n.amount,assetInID:Number(n.id),assetOutID:p,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_out.amount)>I.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},A),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:a,quote:I},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rn(t){return Bn.apply(this,arguments)}function Bn(){return(Bn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(n.amount),c=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}p=a.asset2ID,l=a.asset2Reserves,d=a.asset1Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}p=a.asset1ID,l=a.asset1Reserves,d=a.asset2Reserves,t.next=18;break;case 17:throw new F({pool:a,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(f=Cn({inputSupply:l,outputSupply:d,swapOutputAmount:i,totalFeeShare:c,decimals:r}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact,!(i>d)){t.next=21;break}throw new En;case 21:if(I={assetInID:p,assetInAmount:m,assetOutID:n.id,assetOutAmount:i,swapFee:Number(g),rate:Z(r.assetOut,Number(i))/Z(r.assetIn,Number(m)),priceImpact:x},!s){t.next=28;break}return t.next=25,kt({amount:n.amount,assetInID:p,assetOutID:n.id,swapType:exports.SwapType.FixedInput});case 25:if(!((A=t.sent).route.length>1&&BigInt(A.route[A.route.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function qn(){return(qn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[vn,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[Tn.ASSET_TRANSFER_TXN]=g,I[Tn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[Tn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[Tn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jn(){return(jn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[vn,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[Tn.ASSET_TRANSFER_TXN]=T,v[Tn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[Tn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[Tn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Qn(){return(Qn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,rt(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Oe(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Un,Gn,Wn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Xn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=Xn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Vn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Vn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return qn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return jn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Qn.apply(this,arguments)}},zn=(f(Un={},S.V1_1,Sn),f(Un,S.V2,Wn),Un),Jn=3n,Yn=1000n;function Kn(){return(Kn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===Gn.FEE_TXN_INDEX?c:e===Gn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.quote,o=e.swapType,i=e.slippage,c=e.initiatorAddr,p=s.pool,l=s.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:s},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",i,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",i,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),_=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:a,suggestedParams:x}),w=r.default.assignGroupID([_,A,T,v]),t.abrupt("return",[{txn:w[0],signers:[c]},{txn:w[1],signers:[m]},{txn:w[2],signers:[c]},{txn:w[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*Jn/Yn,l=i-u*i/(u+(c-p));if(l>i)throw new En;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:Ye(d),priceImpact:Ke(o({inputSupply:u,outputSupply:i},d))}}function $n(t){return tr.apply(this,arguments)}function tr(){return(tr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Jt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Jt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function er(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new En;var c=o*u/(u-i)-o,p=c*Yn/(Yn-Jn),l=p-c,d=Z(a.assetOut,Number(i))/Z(a.assetIn,Number(p)),f=Z(a.assetOut,Number(u))/Z(a.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m}}function nr(t){return rr.apply(this,arguments)}function rr(){return(rr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Jt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Jt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Gn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Gn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Gn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Gn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,$n({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,nr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(s),fees:st(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Gn||(Gn={}));var sr,or={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Zn({pool:e,reserves:n,assetIn:r,decimals:a}):er({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Zn,getFixedOutputSwapQuote:er,generateTxns:function(t){return Hn.apply(this,arguments)},signTxns:function(t){return Kn.apply(this,arguments)},execute:function(t){return ar.apply(this,arguments)},executeFixedOutputSwap:nr},ur=4*t.ALGORAND_MIN_TX_FEE;function ir(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof En})))throw new En;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function cr(){return cr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:or.getFixedInputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Vn.getFixedInputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,ir(i);case 4:return p=t.sent,t.abrupt("return",dr(p));case 6:case"end":return t.stop()}}),t)}))),cr.apply(this,arguments)}function pr(){return pr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var e=c(u().mark((function e(s,i){var c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(p={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o},e.prev=1,t.info.contractVersion!==S.V1_1){e.next=6;break}c={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:or.getFixedOutputSwapQuote(p),pool:t.info}},e.next=9;break;case 6:return e.next=8,Vn.getFixedOutputSwapQuote(p);case 8:c=e.sent;case 9:s(c),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(1),i(e.t0);case 15:case"end":return e.stop()}}),e,null,[[1,12]])})));return function(t,n){return e.apply(this,arguments)}}())})),t.next=4,ir(i);case 4:return p=t.sent,t.abrupt("return",dr(p));case 6:case"end":return t.stop()}}),t)}))),pr.apply(this,arguments)}function lr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:wt(t.route)}function dr(t){var e,n=t[0],r=lr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=lr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}function fr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var mr=(f(sr={},S.V1_1,or),f(sr,S.V2,Vn),f(sr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return pe(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return cr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return pr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(sr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&fr(t.quote)===S.V1_1?or.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):Vn.generateTxns(t)})),f(sr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&fr(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return or.signTxns(o({pool:e},t))}return Vn.signTxns(t)})),f(sr,"execute",(function(t){return t.contractVersion===S.V1_1?or.execute(t):Vn.execute(t)})),sr);function gr(){return(gr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,xr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:st(a),confirmedRound:d,txnID:l,groupID:ot(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function xr(t){return Ir.apply(this,arguments)}function Ir(){return(Ir=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(){return Ar=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:st(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,rt(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Ar.apply(this,arguments)}function hr(){return(hr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Tr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:q([it("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function yr(){return(yr=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Tr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=hn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Ve,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=zn,exports.Swap=mr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=yn,exports.V2AddLiquidityTxnIndices=We,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=Ut,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=me,exports.prepareCommitTransactions=function(t){return yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Ar.apply(this,arguments)},exports.redeemExcessAsset=function(t){return gr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=st,exports.tinymanContract_v1_1=Mt,exports.tinymanContract_v2=Xt,exports.tinymanJSSDKConfig=pt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",s=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,s=Object.create(a.prototype),o=new w(r||[]);return s._invoke=function(t,e,n){var r="suspendedStart";return function(a,s){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw s;return D()}for(n.method=a,n.arg=s;;){var o=n.delegate;if(o){var u=y(o,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,o),s}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,a)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,s,o,u){var i=p(t[a],t,s);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,u)}),(function(t){r("throw",t,o,u)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function s(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(s,s):s()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function w(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,s=function e(){for(;++r=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function X(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[s?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function q(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=D(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var j=100000n,Q=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return j+Q*BigInt(o.length)+U*BigInt(s.length+a.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(nt(e),2),s=a[0],o=a[1],u=v(nt(Math.round(Number(et(s,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return at.apply(this,arguments)}function at(){return(at=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function st(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),Tt=it("swap"),yt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),vt={ASSET_OPT_IN:it("asset_opt_in")},_t={testnet:159521633,mainnet:0},wt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function bt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Dt(t){if(!_t[t])throw new Error("Unknown network or network not supported: ".concat(t));return _t[t]}function St(t){return Et.apply(this,arguments)}function Et(){return(Et=c(u().mark((function e(n){var a,s,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[vt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ot(t){return Nt.apply(this,arguments)}function Nt(){return(Nt=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E,O,N;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(c[0].quote.amount_in.amount),Number(c[c.length-1].quote.amount_out.amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Dt(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[Tt,yt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p}),b=i===exports.SwapType.FixedInput?7:8,w.fee=t.ALGORAND_MIN_TX_FEE*(b+1),D=[_,w],e.next=16,Pt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 16:if(!((E=e.sent).length>0)){e.next=22;break}return e.next=20,St({client:s,accountAddress:a,assetIDs:E,routerAppID:v});case 20:O=e.sent,D.unshift(O);case 22:return N=r.default.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[a]}})));case 24:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Pt(t){return kt.apply(this,arguments)}function kt(){return(kt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(wt[o],"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Rt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Bt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Mt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Ct=new(function(n){m(a,Mt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:Gt(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Ut(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?X({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Gt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Wt,zt,Jt=it("e");function Yt(t){return Kt.apply(this,arguments)}function Kt(){return(Kt=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=X({stateArray:A}),T=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,Jt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,Jt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,Jt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ht(){return(Ht=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=X({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mte)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(te,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ae(){return ae=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,Qt(n,r);case 6:return o=t.sent,i=Ut(o,dt(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[Zt[S.V1_1].asset1],asset2ID:i[Zt[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ae.apply(this,arguments)}var se=Object.freeze({__proto__:null,getPoolInfo:ee,getPoolReserves:function(t,e){return re.apply(this,arguments)},getPoolAssets:function(t){return ae.apply(this,arguments)}});function oe(t){return ue.apply(this,arguments)}function ue(){return(ue=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=jt(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(a,s),t.next=8,Qt(n,p);case 8:return d=t.sent,f=Ut(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[Zt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Zt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Zt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Zt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Zt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Zt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Zt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Zt.v2.totalFeeShare]),m.poolTokenID=Number(f[Zt.v2.poolTokenID]),m.asset1ID=Number(f[Zt.v2.asset1]),m.asset2ID=Number(f[Zt.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ie(){return(ie=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Qt(e,n.account.address());case 2:return r=t.sent,a=Ut(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[Zt.v2.asset1Reserves]),s.asset2=BigInt(a[Zt.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[Zt.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ce(){return(ce=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,Qt(n,r);case 3:return s=t.sent,o=Ut(s,dt(a,S.V2)),i=null,o&&(i={asset1ID:o[Zt[S.V2].asset1],asset2ID:o[Zt[S.V2].asset2],poolTokenID:o[Zt[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pe=Object.freeze({__proto__:null,getPoolInfo:oe,getPoolReserves:function(t,e){return ie.apply(this,arguments)},getPoolAssets:function(t){return ce.apply(this,arguments)}});function le(t){return Boolean(t&&!(t.asset1+t.asset2))}var de,fe,me=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=le(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:le,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([ee(t),oe(t)])}}),ge=o((f(de={},S.V1_1,o(o({},se),me)),f(de,S.V2,o(o({},pe),me)),de),me);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(fe||(fe={}));var xe=96e4,Ie=859e3;function Ae(){return(Ae=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,P;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(a,S.V1_1),T=Ct.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:he(A),suggestedParams:c}),(E=[])[fe.FUNDING_TXN]=D,E[fe.VALIDATOR_APP_CALL]=_,E[fe.POOL_TOKEN_CREATE]=w,E[fe.ASSET1_OPT_IN]=b,A||(E[fe.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),P=[{txn:N[fe.FUNDING_TXN],signers:[i]},{txn:N[fe.VALIDATOR_APP_CALL],signers:[y]},{txn:N[fe.POOL_TOKEN_CREATE],signers:[y]},{txn:N[fe.ASSET1_OPT_IN],signers:[y]}],N[fe.ASSET2_OPT_IN]&&P.push({txn:N[fe.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",P);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function he(t){return t?xe:Ie}function Te(){return(Te=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Ct.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===fe.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ye(t){return ve.apply(this,arguments)}function ve(){return(ve=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,a[fe.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function _e(){return(_e=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,ye({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",ge.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var we,be={generateTxns:function(t){return Ae.apply(this,arguments)},signTxns:function(t){return Te.apply(this,arguments)},execute:function(t){return _e.apply(this,arguments)},getBootstrapFundingTxnAmount:he};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(we||(we={}));var De=5,Se=6;function Ee(t,e){return Oe.apply(this,arguments)}function Oe(){return(Oe=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ne(t,e){return Pe.apply(this,arguments)}function Pe(){return(Pe=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Ee(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ke(){return(ke=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=dt(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,ge.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=qt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Fe(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Le(h),suggestedParams:p}),(_=[])[we.FUNDING_TXN]=y,_[we.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[we.FUNDING_TXN]={txn:w[we.FUNDING_TXN],signers:[c]},b[we.VALIDATOR_APP_CALL]={txn:w[we.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Le(t){return function(t,e){var n=jt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Fe(t)+1e5}function Fe(e){return((e?De:Se)+1)*t.ALGORAND_MIN_TX_FEE}function Re(){return(Re=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=qt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===we.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(){return(Be=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Ee(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Zt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",ge.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Me,Ce={generateTxns:function(t){return ke.apply(this,arguments)},signTxns:function(t){return Re.apply(this,arguments)},execute:function(t){return Be.apply(this,arguments)},getBootstrapFundingTxnAmount:Le,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Le(e)}};var Ve,Xe=(f(Me={},S.V1_1,be),f(Me,S.V2,Ce),f(Me,"generateTxns",(function(t){return t.contractVersion===S.V1_1?be.generateTxns(t):Ce.generateTxns(t)})),f(Me,"signTxns",(function(t){return t.contractVersion===S.V1_1?be.signTxns(t):Ce.signTxns(t)})),f(Me,"execute",(function(t){return t.contractVersion===S.V1_1?be.execute(t):Ce.execute(t)})),f(Me,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?be.getBootstrapFundingTxnAmount(n):Ce.getBootstrapFundingTxnAmount(n)})),Me);exports.V1_1AddLiquidityTxnIndices=void 0,(Ve=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ve.FEE_TXN=0]="FEE_TXN",Ve[Ve.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ve[Ve.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ve[Ve.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ve[Ve.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var qe,je,Qe,Ue,Ge=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Ue=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Ue.FLEXIBLE="flexible",Ue.INITIAL="initial";var We,ze=(f(qe={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(qe,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(qe,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),qe),Je=(f(je={},exports.V2AddLiquidityType.INITIAL,1),f(je,exports.V2AddLiquidityType.SINGLE,2),f(je,exports.V2AddLiquidityType.FLEXIBLE,2),je),Ye=(f(Qe={},exports.V2AddLiquidityType.INITIAL,3),f(Qe,exports.V2AddLiquidityType.FLEXIBLE,3),f(Qe,exports.V2AddLiquidityType.SINGLE,2),Qe);function Ke(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function He(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=Ke({assetIn:r,assetOut:a}),o=Z(a.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(s/o-1))}function Ze(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,u=t.asset1Amount,i=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(u),d=s.asset2+BigInt(i),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(u)-x,h=BigInt(i)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=$e(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=$e(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:He({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function $e(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function tn(e){return(Je[e]+1)*t.ALGORAND_MIN_TX_FEE}var en=it("add_liquidity"),nn=(f(We={},S.V1_1,[it("mint")]),f(We,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[en,it("single")],FLEXIBLE_MODE:[en,it("flexible")]}),We);function rn(){return(rn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(s,S.V1_1),appArgs:nn.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function an(){return(an=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sn(){return(sn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Yt({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=st(a),x=ot(a),t.next=17,Yt({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var on=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Nn(){return(Nn=c(u().mark((function t(e){var n,a,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Ot(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,a=e.initiatorAddr,s=e.slippage,i=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=C(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:H("positive",s,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",s,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:l.validatorAppID,appArgs:[Tt,yt[i],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Pn(i),(_=[])[It.INPUT_TXN]=y,_[It.APP_CALL_TXN]=v,w=r.default.assignGroupID(_),t.abrupt("return",[{txn:w[It.INPUT_TXN],signers:[a]},{txn:w[It.APP_CALL_TXN],signers:[a]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(e){return(ht[e]+1)*t.ALGORAND_MIN_TX_FEE}function kn(){return(kn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,rt(a,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Ne(a,o);case 10:return m=t.sent,g=s.type===exports.SwapQuoteType.Direct?{id:s.quoteWithPool.quote.assetInID,amount:s.quoteWithPool.quote.assetInAmount}:{id:Number(s.asset_in_id),amount:Number(s.route[0].quote.amount_in.amount)},x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ln(){return(Ln=c(u().mark((function t(e,n,r,a,s,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e!==exports.SwapType.FixedInput){t.next=6;break}return t.next=3,Fn({pool:n,assetIn:r,decimals:a,isSwapRouterEnabled:o,network:s});case 3:i=t.sent,t.next=9;break;case 6:return t.next=8,Bn({pool:n,assetOut:r,decimals:a,isSwapRouterEnabled:o,network:s});case 8:i=t.sent;case 9:return t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Fn(t){return Rn.apply(this,arguments)}function Rn(){return(Rn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,i=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 17:throw new F({pool:a,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=Cn({inputSupply:d,outputSupply:f,swapInputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapOutputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(g>f)){t.next=21;break}throw new On;case 21:if(A={assetInID:n.id,assetInAmount:c,assetOutID:l,assetOutAmount:g,swapFee:Number(x),rate:Z(r.assetOut,Number(g))/Z(r.assetIn,Number(c)),priceImpact:I},!s){t.next=28;break}return t.next=25,Lt({amount:n.amount,assetInID:Number(n.id),assetOutID:l,swapType:exports.SwapType.FixedInput,network:i});case 25:if(!((h=t.sent).route.length>1&&BigInt(h.route[h.route.length-1].quote.amount_out.amount)>A.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},h),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:a,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Bn(t){return Mn.apply(this,arguments)}function Mn(){return(Mn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,i=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 17:throw new F({pool:a,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=Vn({inputSupply:d,outputSupply:f,swapOutputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapInputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(c>f)){t.next=21;break}throw new On;case 21:if(A={assetInID:l,assetInAmount:g,assetOutID:n.id,assetOutAmount:c,swapFee:Number(x),rate:Z(r.assetOut,Number(c))/Z(r.assetIn,Number(g)),priceImpact:I},!s){t.next=28;break}return t.next=25,Lt({amount:n.amount,assetInID:l,assetOutID:n.id,swapType:exports.SwapType.FixedInput,network:i});case 25:if(!((h=t.sent).route.length>1&&BigInt(h.route[h.route.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function jn(){return(jn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[yn.ASSET_TRANSFER_TXN]=g,I[yn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[yn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[yn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Qn(){return(Qn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[yn.ASSET_TRANSFER_TXN]=T,v[yn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[yn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[yn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Un(){return(Un=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,rt(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Ne(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Gn,Wn,zn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=qn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=qn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Xn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Xn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return jn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Qn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Un.apply(this,arguments)}},Jn=(f(Gn={},S.V1_1,En),f(Gn,S.V2,zn),Gn),Yn=3n,Kn=1000n;function Hn(){return(Hn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===Wn.FEE_TXN_INDEX?c:e===Wn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.quote,o=e.swapType,i=e.slippage,c=e.initiatorAddr,p=s.pool,l=s.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:s},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",i,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",i,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),_=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:a,suggestedParams:x}),w=r.default.assignGroupID([_,A,T,v]),t.abrupt("return",[{txn:w[0],signers:[c]},{txn:w[1],signers:[m]},{txn:w[2],signers:[c]},{txn:w[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*Yn/Kn,l=i-u*i/(u+(c-p));if(l>i)throw new On;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:Ke(d),priceImpact:He(o({inputSupply:u,outputSupply:i},d))}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Yt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Yt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new On;var c=o*u/(u-i)-o,p=c*Kn/(Kn-Yn),l=p-c,d=Z(a.assetOut,Number(i))/Z(a.assetIn,Number(p)),f=Z(a.assetOut,Number(u))/Z(a.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m}}function rr(t){return ar.apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Yt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Yt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sr(){return(sr=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Wn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Wn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,tr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,rr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(s),fees:st(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Wn||(Wn={}));var or,ur={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?$n({pool:e,reserves:n,assetIn:r,decimals:a}):nr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:$n,getFixedOutputSwapQuote:nr,generateTxns:function(t){return Zn.apply(this,arguments)},signTxns:function(t){return Hn.apply(this,arguments)},execute:function(t){return sr.apply(this,arguments)},executeFixedOutputSwap:rr},ir=4*t.ALGORAND_MIN_TX_FEE;function cr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof On})))throw new On;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function pr(){return pr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var s=c(u().mark((function s(i,c){var p,l;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedInputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Xn.getFixedInputSwapQuote(l);case 8:p=s.sent;case 9:i(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),pr.apply(this,arguments)}function lr(){return lr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var s=c(u().mark((function s(i,c){var p,l;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedOutputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Xn.getFixedOutputSwapQuote(l);case 8:p=s.sent;case 9:i(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),lr.apply(this,arguments)}function dr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:bt(t.route)}function fr(t){var e,n=t[0],r=dr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=dr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}function mr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var gr=(f(or={},S.V1_1,ur),f(or,S.V2,Xn),f(or,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return le(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return pr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return lr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(or,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&mr(t.quote)===S.V1_1?ur.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):Xn.generateTxns(t)})),f(or,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&mr(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return ur.signTxns(o({pool:e},t))}return Xn.signTxns(t)})),f(or,"execute",(function(t){return t.contractVersion===S.V1_1?ur.execute(t):Xn.execute(t)})),or);function xr(){return(xr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,Ir({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:st(a),confirmedRound:d,txnID:l,groupID:ot(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function Ir(t){return Ar.apply(this,arguments)}function Ar(){return(Ar=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hr(){return hr=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:st(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,rt(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),hr.apply(this,arguments)}function Tr(){return(Tr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:q([it("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function vr(){return(vr=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=yr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Tn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Xe,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Jn,exports.Swap=gr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=vn,exports.V2AddLiquidityTxnIndices=ze,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=Gt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=ge,exports.prepareCommitTransactions=function(t){return vr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return hr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return xr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=st,exports.tinymanContract_v1_1=Ct,exports.tinymanContract_v2=qt,exports.tinymanJSSDKConfig=pt; diff --git a/dist/swap/index.d.ts b/dist/swap/index.d.ts index a3e8a2d7..7cb00bf3 100644 --- a/dist/swap/index.d.ts +++ b/dist/swap/index.d.ts @@ -50,23 +50,25 @@ export declare const Swap: { getQuote: (type: import("./constants").SwapType, pool: import("..").V2PoolInfo, asset: import("../util/asset/assetModels").AssetWithIdAndAmount, decimals: { assetIn: number; assetOut: number; - }, isSwapRouterEnabled?: boolean | undefined) => Promise; - getFixedInputSwapQuote: ({ assetIn, decimals, pool, isSwapRouterEnabled }: { + }, network: import("..").SupportedNetwork, isSwapRouterEnabled?: boolean | undefined) => Promise; + getFixedInputSwapQuote: ({ assetIn, decimals, pool, isSwapRouterEnabled, network }: { pool: import("..").V2PoolInfo; assetIn: import("../util/asset/assetModels").AssetWithIdAndAmount; decimals: { assetIn: number; assetOut: number; }; + network: import("..").SupportedNetwork; isSwapRouterEnabled?: boolean | undefined; }) => Promise; - getFixedOutputSwapQuote: ({ assetOut, decimals, pool, isSwapRouterEnabled }: { + getFixedOutputSwapQuote: ({ assetOut, decimals, pool, isSwapRouterEnabled, network }: { pool: import("..").V2PoolInfo; assetOut: import("../util/asset/assetModels").AssetWithIdAndAmount; decimals: { assetIn: number; assetOut: number; }; + network: import("..").SupportedNetwork; isSwapRouterEnabled?: boolean | undefined; }) => Promise; generateTxns: (params: import("./types").GenerateSwapTxnsParams) => Promise; diff --git a/dist/swap/v2/index.d.ts b/dist/swap/v2/index.d.ts index 03cecd17..43f3f2e3 100644 --- a/dist/swap/v2/index.d.ts +++ b/dist/swap/v2/index.d.ts @@ -1,5 +1,5 @@ import { Algodv2 } from "algosdk"; -import { InitiatorSigner, SignerTransaction } from "../../util/commonTypes"; +import { InitiatorSigner, SignerTransaction, SupportedNetwork } from "../../util/commonTypes"; import { V2PoolInfo } from "../../util/pool/poolTypes"; import { GenerateSwapTxnsParams, SwapQuote, V2SwapExecution } from "../types"; import { SwapType } from "../constants"; @@ -30,29 +30,31 @@ declare function execute({ client, quote, txGroup, signedTxns }: { declare function getQuote(type: SwapType, pool: V2PoolInfo, asset: AssetWithIdAndAmount, decimals: { assetIn: number; assetOut: number; -}, isSwapRouterEnabled?: boolean): Promise; +}, network: SupportedNetwork, isSwapRouterEnabled?: boolean): Promise; /** * @returns A quote for a fixed input swap. Does NOT execute any transactions. */ -declare function getFixedInputSwapQuote({ assetIn, decimals, pool, isSwapRouterEnabled }: { +declare function getFixedInputSwapQuote({ assetIn, decimals, pool, isSwapRouterEnabled, network }: { pool: V2PoolInfo; assetIn: AssetWithIdAndAmount; decimals: { assetIn: number; assetOut: number; }; + network: SupportedNetwork; isSwapRouterEnabled?: boolean; }): Promise; /** * @returns A quote for a fixed output swap. Does NOT execute any transactions. */ -declare function getFixedOutputSwapQuote({ assetOut, decimals, pool, isSwapRouterEnabled }: { +declare function getFixedOutputSwapQuote({ assetOut, decimals, pool, isSwapRouterEnabled, network }: { pool: V2PoolInfo; assetOut: AssetWithIdAndAmount; decimals: { assetIn: number; assetOut: number; }; + network: SupportedNetwork; isSwapRouterEnabled?: boolean; }): Promise; declare function calculateFixedInputSwap({ inputSupply, outputSupply, swapInputAmount, totalFeeShare, decimals }: { diff --git a/dist/swap/v2/router/constants.d.ts b/dist/swap/v2/router/constants.d.ts index f4fa2ddf..e17160a1 100644 --- a/dist/swap/v2/router/constants.d.ts +++ b/dist/swap/v2/router/constants.d.ts @@ -1,2 +1,7 @@ import { SupportedNetwork } from "../../../util/commonTypes"; export declare const SWAP_ROUTER_APP_ID: Record; +declare const TINYMAN_ANALYTICS_API_BASE_URLS: Record; +export { TINYMAN_ANALYTICS_API_BASE_URLS }; diff --git a/dist/swap/v2/router/swap-router.d.ts b/dist/swap/v2/router/swap-router.d.ts index bba15c66..cdbc9ec2 100644 --- a/dist/swap/v2/router/swap-router.d.ts +++ b/dist/swap/v2/router/swap-router.d.ts @@ -1,5 +1,6 @@ import algosdk, { Transaction } from "algosdk"; import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; +import { SupportedNetwork } from "../../../util/commonTypes"; import { SwapType } from "../../constants"; import { FetchSwapRouteQuotesResponse, GenerateSwapRouterTxnsParams } from "../../types"; export declare function generateSwapRouterAssetOptInTransaction({ client, routerAppID, assetIDs, accountAddress }: { @@ -17,9 +18,10 @@ export declare function getSwapRouterAppOptInRequiredAssetIDs({ client, routerAp routerAppID: number; assetIDs: number[]; }): Promise; -export declare function getSwapRoute({ amount, assetInID, assetOutID, swapType }: { +export declare function getSwapRoute({ amount, assetInID, assetOutID, swapType, network }: { assetInID: number; assetOutID: number; swapType: SwapType; amount: number | bigint; + network: SupportedNetwork; }): Promise; diff --git a/src/swap/utils.ts b/src/swap/utils.ts index b64f25dd..319b1f45 100644 --- a/src/swap/utils.ts +++ b/src/swap/utils.ts @@ -83,7 +83,8 @@ export async function getFixedInputSwapQuote( assetIn: {amount, id: Number(assetIn.id)}, decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, reserves: pool.reserves, - isSwapRouterEnabled + isSwapRouterEnabled, + network: params.network }; try { @@ -128,7 +129,8 @@ export async function getFixedOutputSwapQuote( assetOut: {amount, id: Number(assetOut.id)}, decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, reserves: pool.reserves, - isSwapRouterEnabled + isSwapRouterEnabled, + network: params.network }; try { diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index b855c57f..29faf70f 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -5,7 +5,11 @@ import { convertFromBaseUnits, sendAndWaitRawTransaction } from "../../util/util"; -import {InitiatorSigner, SignerTransaction} from "../../util/commonTypes"; +import { + InitiatorSigner, + SignerTransaction, + SupportedNetwork +} from "../../util/commonTypes"; import TinymanError from "../../util/error/TinymanError"; import {PoolStatus, V2PoolInfo} from "../../util/pool/poolTypes"; import { @@ -208,11 +212,13 @@ async function execute({ * @param decimals.assetOut - Decimals quantity for the output asset * @returns A promise for the Swap quote */ +// eslint-disable-next-line max-params async function getQuote( type: SwapType, pool: V2PoolInfo, asset: AssetWithIdAndAmount, decimals: {assetIn: number; assetOut: number}, + network: SupportedNetwork, isSwapRouterEnabled?: boolean ): Promise { let quote: SwapQuote; @@ -222,14 +228,16 @@ async function getQuote( pool, assetIn: asset, decimals, - isSwapRouterEnabled + isSwapRouterEnabled, + network }); } else { quote = await getFixedOutputSwapQuote({ pool, assetOut: asset, decimals, - isSwapRouterEnabled + isSwapRouterEnabled, + network }); } @@ -243,11 +251,13 @@ async function getFixedInputSwapQuote({ assetIn, decimals, pool, - isSwapRouterEnabled + isSwapRouterEnabled, + network }: { pool: V2PoolInfo; assetIn: AssetWithIdAndAmount; decimals: {assetIn: number; assetOut: number}; + network: SupportedNetwork; isSwapRouterEnabled?: boolean; }): Promise { if (pool.status !== PoolStatus.READY) { @@ -305,7 +315,8 @@ async function getFixedInputSwapQuote({ amount: assetIn.amount, assetInID: Number(assetIn.id), assetOutID, - swapType: SwapType.FixedInput + swapType: SwapType.FixedInput, + network }); if ( @@ -336,11 +347,13 @@ async function getFixedOutputSwapQuote({ assetOut, decimals, pool, - isSwapRouterEnabled + isSwapRouterEnabled, + network }: { pool: V2PoolInfo; assetOut: AssetWithIdAndAmount; decimals: {assetIn: number; assetOut: number}; + network: SupportedNetwork; isSwapRouterEnabled?: boolean; }): Promise { if (pool.status !== PoolStatus.READY) { @@ -399,7 +412,8 @@ async function getFixedOutputSwapQuote({ amount: assetOut.amount, assetInID, assetOutID: assetOut.id, - swapType: SwapType.FixedInput + swapType: SwapType.FixedInput, + network }); if ( diff --git a/src/swap/v2/router/constants.ts b/src/swap/v2/router/constants.ts index 3c654d79..9904f2c6 100644 --- a/src/swap/v2/router/constants.ts +++ b/src/swap/v2/router/constants.ts @@ -5,3 +5,19 @@ export const SWAP_ROUTER_APP_ID: Record = { // TODO: Update this when the mainnet app is deployed mainnet: 0 }; + +const TINYMAN_ANALYTICS_API_BASE_URLS: Record< + SupportedNetwork, + {base: string; v1: string} +> = { + mainnet: { + base: "https://mainnet.analytics.tinyman.org/api", + v1: "https://mainnet.analytics.tinyman.org/api/v1" + }, + testnet: { + base: "https://testnet.analytics.tinyman.org/api", + v1: "https://testnet.analytics.tinyman.org/api/v1" + } +}; + +export {TINYMAN_ANALYTICS_API_BASE_URLS}; diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index 9904f142..1f03ca50 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -4,6 +4,7 @@ import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; import {CONTRACT_VERSION} from "../../../contract/constants"; import {ALGO_ASSET_ID} from "../../../util/asset/assetConstants"; import {getAssetId, isAlgo} from "../../../util/asset/assetUtils"; +import {SupportedNetwork} from "../../../util/commonTypes"; import {getValidatorAppID} from "../../../validator"; import {SwapType} from "../../constants"; import { @@ -16,6 +17,7 @@ import { V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED, V2_SWAP_ROUTER_APP_ARGS_ENCODED } from "../constants"; +import {TINYMAN_ANALYTICS_API_BASE_URLS} from "./constants"; import {getSwapRouterAppID} from "./util"; export async function generateSwapRouterAssetOptInTransaction({ @@ -158,12 +160,14 @@ export async function getSwapRoute({ amount, assetInID, assetOutID, - swapType + swapType, + network }: { assetInID: number; assetOutID: number; swapType: SwapType; amount: number | bigint; + network: SupportedNetwork; }): Promise { const payload: FetchSwapRouteQuotesPayload = { asset_in_id: String(assetInID), @@ -174,7 +178,7 @@ export async function getSwapRoute({ try { const response = await fetch( - "https://testnet.analytics.tinyman.org/api/v1/swap-router/quotes/", + `${TINYMAN_ANALYTICS_API_BASE_URLS[network]}/swap-router/quotes/`, { method: "POST", headers: { From 14230c08a42efc3d76be543e9982583be6bc47ac Mon Sep 17 00:00:00 2001 From: anlerkan Date: Mon, 6 Mar 2023 15:31:54 +0300 Subject: [PATCH 13/39] fix(swap-router): use v1 endpoint --- src/swap/v2/router/swap-router.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index 1f03ca50..44cc1646 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -178,7 +178,7 @@ export async function getSwapRoute({ try { const response = await fetch( - `${TINYMAN_ANALYTICS_API_BASE_URLS[network]}/swap-router/quotes/`, + `${TINYMAN_ANALYTICS_API_BASE_URLS[network].v1}/swap-router/quotes/`, { method: "POST", headers: { From 0e540c8ae081e4e67b29b48db80f4f12f793c9b4 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Mon, 6 Mar 2023 15:32:20 +0300 Subject: [PATCH 14/39] chore: get build --- dist/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/index.js b/dist/index.js index dc7cb197..2fef0f23 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),a=Uint8Array.from([1]);function s(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function X(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[s?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function q(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=D(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var j=100000n,Q=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return j+Q*BigInt(o.length)+U*BigInt(s.length+a.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(nt(e),2),s=a[0],o=a[1],u=v(nt(Math.round(Number(et(s,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return at.apply(this,arguments)}function at(){return(at=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function st(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),Tt=it("swap"),yt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),vt={ASSET_OPT_IN:it("asset_opt_in")},_t={testnet:159521633,mainnet:0},wt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function bt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Dt(t){if(!_t[t])throw new Error("Unknown network or network not supported: ".concat(t));return _t[t]}function St(t){return Et.apply(this,arguments)}function Et(){return(Et=c(u().mark((function e(n){var a,s,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[vt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ot(t){return Nt.apply(this,arguments)}function Nt(){return(Nt=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E,O,N;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(c[0].quote.amount_in.amount),Number(c[c.length-1].quote.amount_out.amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Dt(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[Tt,yt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p}),b=i===exports.SwapType.FixedInput?7:8,w.fee=t.ALGORAND_MIN_TX_FEE*(b+1),D=[_,w],e.next=16,Pt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 16:if(!((E=e.sent).length>0)){e.next=22;break}return e.next=20,St({client:s,accountAddress:a,assetIDs:E,routerAppID:v});case 20:O=e.sent,D.unshift(O);case 22:return N=r.default.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[a]}})));case 24:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Pt(t){return kt.apply(this,arguments)}function kt(){return(kt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(wt[o],"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Rt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Bt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Mt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Ct=new(function(n){m(a,Mt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:Gt(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Ut(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?X({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Gt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Wt,zt,Jt=it("e");function Yt(t){return Kt.apply(this,arguments)}function Kt(){return(Kt=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=X({stateArray:A}),T=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,Jt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,Jt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,Jt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ht(){return(Ht=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=X({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mte)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(te,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ae(){return ae=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,Qt(n,r);case 6:return o=t.sent,i=Ut(o,dt(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[Zt[S.V1_1].asset1],asset2ID:i[Zt[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ae.apply(this,arguments)}var se=Object.freeze({__proto__:null,getPoolInfo:ee,getPoolReserves:function(t,e){return re.apply(this,arguments)},getPoolAssets:function(t){return ae.apply(this,arguments)}});function oe(t){return ue.apply(this,arguments)}function ue(){return(ue=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=jt(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(a,s),t.next=8,Qt(n,p);case 8:return d=t.sent,f=Ut(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[Zt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Zt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Zt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Zt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Zt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Zt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Zt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Zt.v2.totalFeeShare]),m.poolTokenID=Number(f[Zt.v2.poolTokenID]),m.asset1ID=Number(f[Zt.v2.asset1]),m.asset2ID=Number(f[Zt.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ie(){return(ie=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Qt(e,n.account.address());case 2:return r=t.sent,a=Ut(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[Zt.v2.asset1Reserves]),s.asset2=BigInt(a[Zt.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[Zt.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ce(){return(ce=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,Qt(n,r);case 3:return s=t.sent,o=Ut(s,dt(a,S.V2)),i=null,o&&(i={asset1ID:o[Zt[S.V2].asset1],asset2ID:o[Zt[S.V2].asset2],poolTokenID:o[Zt[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pe=Object.freeze({__proto__:null,getPoolInfo:oe,getPoolReserves:function(t,e){return ie.apply(this,arguments)},getPoolAssets:function(t){return ce.apply(this,arguments)}});function le(t){return Boolean(t&&!(t.asset1+t.asset2))}var de,fe,me=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=le(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:le,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([ee(t),oe(t)])}}),ge=o((f(de={},S.V1_1,o(o({},se),me)),f(de,S.V2,o(o({},pe),me)),de),me);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(fe||(fe={}));var xe=96e4,Ie=859e3;function Ae(){return(Ae=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,P;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(a,S.V1_1),T=Ct.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:he(A),suggestedParams:c}),(E=[])[fe.FUNDING_TXN]=D,E[fe.VALIDATOR_APP_CALL]=_,E[fe.POOL_TOKEN_CREATE]=w,E[fe.ASSET1_OPT_IN]=b,A||(E[fe.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),P=[{txn:N[fe.FUNDING_TXN],signers:[i]},{txn:N[fe.VALIDATOR_APP_CALL],signers:[y]},{txn:N[fe.POOL_TOKEN_CREATE],signers:[y]},{txn:N[fe.ASSET1_OPT_IN],signers:[y]}],N[fe.ASSET2_OPT_IN]&&P.push({txn:N[fe.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",P);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function he(t){return t?xe:Ie}function Te(){return(Te=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Ct.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===fe.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ye(t){return ve.apply(this,arguments)}function ve(){return(ve=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,a[fe.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function _e(){return(_e=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,ye({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",ge.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var we,be={generateTxns:function(t){return Ae.apply(this,arguments)},signTxns:function(t){return Te.apply(this,arguments)},execute:function(t){return _e.apply(this,arguments)},getBootstrapFundingTxnAmount:he};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(we||(we={}));var De=5,Se=6;function Ee(t,e){return Oe.apply(this,arguments)}function Oe(){return(Oe=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ne(t,e){return Pe.apply(this,arguments)}function Pe(){return(Pe=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Ee(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ke(){return(ke=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=dt(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,ge.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=qt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Fe(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Le(h),suggestedParams:p}),(_=[])[we.FUNDING_TXN]=y,_[we.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[we.FUNDING_TXN]={txn:w[we.FUNDING_TXN],signers:[c]},b[we.VALIDATOR_APP_CALL]={txn:w[we.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Le(t){return function(t,e){var n=jt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Fe(t)+1e5}function Fe(e){return((e?De:Se)+1)*t.ALGORAND_MIN_TX_FEE}function Re(){return(Re=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=qt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===we.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(){return(Be=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Ee(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Zt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",ge.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Me,Ce={generateTxns:function(t){return ke.apply(this,arguments)},signTxns:function(t){return Re.apply(this,arguments)},execute:function(t){return Be.apply(this,arguments)},getBootstrapFundingTxnAmount:Le,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Le(e)}};var Ve,Xe=(f(Me={},S.V1_1,be),f(Me,S.V2,Ce),f(Me,"generateTxns",(function(t){return t.contractVersion===S.V1_1?be.generateTxns(t):Ce.generateTxns(t)})),f(Me,"signTxns",(function(t){return t.contractVersion===S.V1_1?be.signTxns(t):Ce.signTxns(t)})),f(Me,"execute",(function(t){return t.contractVersion===S.V1_1?be.execute(t):Ce.execute(t)})),f(Me,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?be.getBootstrapFundingTxnAmount(n):Ce.getBootstrapFundingTxnAmount(n)})),Me);exports.V1_1AddLiquidityTxnIndices=void 0,(Ve=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ve.FEE_TXN=0]="FEE_TXN",Ve[Ve.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ve[Ve.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ve[Ve.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ve[Ve.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var qe,je,Qe,Ue,Ge=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Ue=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Ue.FLEXIBLE="flexible",Ue.INITIAL="initial";var We,ze=(f(qe={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(qe,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(qe,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),qe),Je=(f(je={},exports.V2AddLiquidityType.INITIAL,1),f(je,exports.V2AddLiquidityType.SINGLE,2),f(je,exports.V2AddLiquidityType.FLEXIBLE,2),je),Ye=(f(Qe={},exports.V2AddLiquidityType.INITIAL,3),f(Qe,exports.V2AddLiquidityType.FLEXIBLE,3),f(Qe,exports.V2AddLiquidityType.SINGLE,2),Qe);function Ke(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function He(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=Ke({assetIn:r,assetOut:a}),o=Z(a.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(s/o-1))}function Ze(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,u=t.asset1Amount,i=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(u),d=s.asset2+BigInt(i),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(u)-x,h=BigInt(i)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=$e(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=$e(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:He({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function $e(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function tn(e){return(Je[e]+1)*t.ALGORAND_MIN_TX_FEE}var en=it("add_liquidity"),nn=(f(We={},S.V1_1,[it("mint")]),f(We,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[en,it("single")],FLEXIBLE_MODE:[en,it("flexible")]}),We);function rn(){return(rn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(s,S.V1_1),appArgs:nn.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function an(){return(an=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sn(){return(sn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Yt({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=st(a),x=ot(a),t.next=17,Yt({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var on=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Nn(){return(Nn=c(u().mark((function t(e){var n,a,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Ot(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,a=e.initiatorAddr,s=e.slippage,i=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=C(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:H("positive",s,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",s,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:l.validatorAppID,appArgs:[Tt,yt[i],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Pn(i),(_=[])[It.INPUT_TXN]=y,_[It.APP_CALL_TXN]=v,w=r.default.assignGroupID(_),t.abrupt("return",[{txn:w[It.INPUT_TXN],signers:[a]},{txn:w[It.APP_CALL_TXN],signers:[a]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(e){return(ht[e]+1)*t.ALGORAND_MIN_TX_FEE}function kn(){return(kn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,rt(a,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Ne(a,o);case 10:return m=t.sent,g=s.type===exports.SwapQuoteType.Direct?{id:s.quoteWithPool.quote.assetInID,amount:s.quoteWithPool.quote.assetInAmount}:{id:Number(s.asset_in_id),amount:Number(s.route[0].quote.amount_in.amount)},x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ln(){return(Ln=c(u().mark((function t(e,n,r,a,s,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e!==exports.SwapType.FixedInput){t.next=6;break}return t.next=3,Fn({pool:n,assetIn:r,decimals:a,isSwapRouterEnabled:o,network:s});case 3:i=t.sent,t.next=9;break;case 6:return t.next=8,Bn({pool:n,assetOut:r,decimals:a,isSwapRouterEnabled:o,network:s});case 8:i=t.sent;case 9:return t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Fn(t){return Rn.apply(this,arguments)}function Rn(){return(Rn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,i=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 17:throw new F({pool:a,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=Cn({inputSupply:d,outputSupply:f,swapInputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapOutputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(g>f)){t.next=21;break}throw new On;case 21:if(A={assetInID:n.id,assetInAmount:c,assetOutID:l,assetOutAmount:g,swapFee:Number(x),rate:Z(r.assetOut,Number(g))/Z(r.assetIn,Number(c)),priceImpact:I},!s){t.next=28;break}return t.next=25,Lt({amount:n.amount,assetInID:Number(n.id),assetOutID:l,swapType:exports.SwapType.FixedInput,network:i});case 25:if(!((h=t.sent).route.length>1&&BigInt(h.route[h.route.length-1].quote.amount_out.amount)>A.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},h),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:a,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Bn(t){return Mn.apply(this,arguments)}function Mn(){return(Mn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,i=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 17:throw new F({pool:a,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=Vn({inputSupply:d,outputSupply:f,swapOutputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapInputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(c>f)){t.next=21;break}throw new On;case 21:if(A={assetInID:l,assetInAmount:g,assetOutID:n.id,assetOutAmount:c,swapFee:Number(x),rate:Z(r.assetOut,Number(c))/Z(r.assetIn,Number(g)),priceImpact:I},!s){t.next=28;break}return t.next=25,Lt({amount:n.amount,assetInID:l,assetOutID:n.id,swapType:exports.SwapType.FixedInput,network:i});case 25:if(!((h=t.sent).route.length>1&&BigInt(h.route[h.route.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function jn(){return(jn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[yn.ASSET_TRANSFER_TXN]=g,I[yn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[yn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[yn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Qn(){return(Qn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[yn.ASSET_TRANSFER_TXN]=T,v[yn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[yn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[yn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Un(){return(Un=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,rt(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Ne(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Gn,Wn,zn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=qn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=qn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Xn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Xn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return jn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Qn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Un.apply(this,arguments)}},Jn=(f(Gn={},S.V1_1,En),f(Gn,S.V2,zn),Gn),Yn=3n,Kn=1000n;function Hn(){return(Hn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===Wn.FEE_TXN_INDEX?c:e===Wn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.quote,o=e.swapType,i=e.slippage,c=e.initiatorAddr,p=s.pool,l=s.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:s},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",i,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",i,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),_=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:a,suggestedParams:x}),w=r.default.assignGroupID([_,A,T,v]),t.abrupt("return",[{txn:w[0],signers:[c]},{txn:w[1],signers:[m]},{txn:w[2],signers:[c]},{txn:w[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*Yn/Kn,l=i-u*i/(u+(c-p));if(l>i)throw new On;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:Ke(d),priceImpact:He(o({inputSupply:u,outputSupply:i},d))}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Yt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Yt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new On;var c=o*u/(u-i)-o,p=c*Kn/(Kn-Yn),l=p-c,d=Z(a.assetOut,Number(i))/Z(a.assetIn,Number(p)),f=Z(a.assetOut,Number(u))/Z(a.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m}}function rr(t){return ar.apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Yt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Yt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sr(){return(sr=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Wn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Wn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,tr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,rr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(s),fees:st(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Wn||(Wn={}));var or,ur={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?$n({pool:e,reserves:n,assetIn:r,decimals:a}):nr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:$n,getFixedOutputSwapQuote:nr,generateTxns:function(t){return Zn.apply(this,arguments)},signTxns:function(t){return Hn.apply(this,arguments)},execute:function(t){return sr.apply(this,arguments)},executeFixedOutputSwap:rr},ir=4*t.ALGORAND_MIN_TX_FEE;function cr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof On})))throw new On;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function pr(){return pr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var s=c(u().mark((function s(i,c){var p,l;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedInputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Xn.getFixedInputSwapQuote(l);case 8:p=s.sent;case 9:i(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),pr.apply(this,arguments)}function lr(){return lr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var s=c(u().mark((function s(i,c){var p,l;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedOutputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Xn.getFixedOutputSwapQuote(l);case 8:p=s.sent;case 9:i(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),lr.apply(this,arguments)}function dr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:bt(t.route)}function fr(t){var e,n=t[0],r=dr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=dr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}function mr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var gr=(f(or={},S.V1_1,ur),f(or,S.V2,Xn),f(or,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return le(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return pr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return lr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(or,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&mr(t.quote)===S.V1_1?ur.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):Xn.generateTxns(t)})),f(or,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&mr(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return ur.signTxns(o({pool:e},t))}return Xn.signTxns(t)})),f(or,"execute",(function(t){return t.contractVersion===S.V1_1?ur.execute(t):Xn.execute(t)})),or);function xr(){return(xr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,Ir({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:st(a),confirmedRound:d,txnID:l,groupID:ot(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function Ir(t){return Ar.apply(this,arguments)}function Ar(){return(Ar=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hr(){return hr=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:st(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,rt(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),hr.apply(this,arguments)}function Tr(){return(Tr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:q([it("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function vr(){return(vr=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=yr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Tn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Xe,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Jn,exports.Swap=gr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=vn,exports.V2AddLiquidityTxnIndices=ze,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=Gt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=ge,exports.prepareCommitTransactions=function(t){return vr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return hr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return xr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=st,exports.tinymanContract_v1_1=Ct,exports.tinymanContract_v2=qt,exports.tinymanJSSDKConfig=pt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",s=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,s=Object.create(a.prototype),o=new w(r||[]);return s._invoke=function(t,e,n){var r="suspendedStart";return function(a,s){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw s;return D()}for(n.method=a,n.arg=s;;){var o=n.delegate;if(o){var u=y(o,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,o),s}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,a)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,s,o,u){var i=p(t[a],t,s);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,u)}),(function(t){r("throw",t,o,u)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function s(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(s,s):s()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function w(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,s=function e(){for(;++r=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function X(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[s?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function q(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=D(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var j=100000n,Q=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return j+Q*BigInt(o.length)+U*BigInt(s.length+a.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(nt(e),2),s=a[0],o=a[1],u=v(nt(Math.round(Number(et(s,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return at.apply(this,arguments)}function at(){return(at=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function st(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),Tt=it("swap"),yt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),vt={ASSET_OPT_IN:it("asset_opt_in")},_t={testnet:159521633,mainnet:0},wt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function bt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Dt(t){if(!_t[t])throw new Error("Unknown network or network not supported: ".concat(t));return _t[t]}function St(t){return Et.apply(this,arguments)}function Et(){return(Et=c(u().mark((function e(n){var a,s,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[vt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ot(t){return Nt.apply(this,arguments)}function Nt(){return(Nt=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E,O,N;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(c[0].quote.amount_in.amount),Number(c[c.length-1].quote.amount_out.amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Dt(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[Tt,yt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p}),b=i===exports.SwapType.FixedInput?7:8,w.fee=t.ALGORAND_MIN_TX_FEE*(b+1),D=[_,w],e.next=16,Pt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 16:if(!((E=e.sent).length>0)){e.next=22;break}return e.next=20,St({client:s,accountAddress:a,assetIDs:E,routerAppID:v});case 20:O=e.sent,D.unshift(O);case 22:return N=r.default.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[a]}})));case 24:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Pt(t){return kt.apply(this,arguments)}function kt(){return(kt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(wt[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Rt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Bt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Mt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Ct=new(function(n){m(a,Mt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:Gt(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Ut(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?X({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Gt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Wt,zt,Jt=it("e");function Yt(t){return Kt.apply(this,arguments)}function Kt(){return(Kt=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=X({stateArray:A}),T=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,Jt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,Jt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,Jt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ht(){return(Ht=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=X({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mte)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(te,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ae(){return ae=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,Qt(n,r);case 6:return o=t.sent,i=Ut(o,dt(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[Zt[S.V1_1].asset1],asset2ID:i[Zt[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ae.apply(this,arguments)}var se=Object.freeze({__proto__:null,getPoolInfo:ee,getPoolReserves:function(t,e){return re.apply(this,arguments)},getPoolAssets:function(t){return ae.apply(this,arguments)}});function oe(t){return ue.apply(this,arguments)}function ue(){return(ue=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=jt(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(a,s),t.next=8,Qt(n,p);case 8:return d=t.sent,f=Ut(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[Zt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Zt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Zt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Zt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Zt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Zt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Zt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Zt.v2.totalFeeShare]),m.poolTokenID=Number(f[Zt.v2.poolTokenID]),m.asset1ID=Number(f[Zt.v2.asset1]),m.asset2ID=Number(f[Zt.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ie(){return(ie=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Qt(e,n.account.address());case 2:return r=t.sent,a=Ut(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[Zt.v2.asset1Reserves]),s.asset2=BigInt(a[Zt.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[Zt.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ce(){return(ce=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,Qt(n,r);case 3:return s=t.sent,o=Ut(s,dt(a,S.V2)),i=null,o&&(i={asset1ID:o[Zt[S.V2].asset1],asset2ID:o[Zt[S.V2].asset2],poolTokenID:o[Zt[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pe=Object.freeze({__proto__:null,getPoolInfo:oe,getPoolReserves:function(t,e){return ie.apply(this,arguments)},getPoolAssets:function(t){return ce.apply(this,arguments)}});function le(t){return Boolean(t&&!(t.asset1+t.asset2))}var de,fe,me=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=le(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:le,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([ee(t),oe(t)])}}),ge=o((f(de={},S.V1_1,o(o({},se),me)),f(de,S.V2,o(o({},pe),me)),de),me);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(fe||(fe={}));var xe=96e4,Ie=859e3;function Ae(){return(Ae=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,P;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(a,S.V1_1),T=Ct.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:he(A),suggestedParams:c}),(E=[])[fe.FUNDING_TXN]=D,E[fe.VALIDATOR_APP_CALL]=_,E[fe.POOL_TOKEN_CREATE]=w,E[fe.ASSET1_OPT_IN]=b,A||(E[fe.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),P=[{txn:N[fe.FUNDING_TXN],signers:[i]},{txn:N[fe.VALIDATOR_APP_CALL],signers:[y]},{txn:N[fe.POOL_TOKEN_CREATE],signers:[y]},{txn:N[fe.ASSET1_OPT_IN],signers:[y]}],N[fe.ASSET2_OPT_IN]&&P.push({txn:N[fe.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",P);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function he(t){return t?xe:Ie}function Te(){return(Te=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Ct.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===fe.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ye(t){return ve.apply(this,arguments)}function ve(){return(ve=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,a[fe.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function _e(){return(_e=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,ye({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",ge.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var we,be={generateTxns:function(t){return Ae.apply(this,arguments)},signTxns:function(t){return Te.apply(this,arguments)},execute:function(t){return _e.apply(this,arguments)},getBootstrapFundingTxnAmount:he};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(we||(we={}));var De=5,Se=6;function Ee(t,e){return Oe.apply(this,arguments)}function Oe(){return(Oe=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ne(t,e){return Pe.apply(this,arguments)}function Pe(){return(Pe=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Ee(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ke(){return(ke=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=dt(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,ge.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=qt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Fe(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Le(h),suggestedParams:p}),(_=[])[we.FUNDING_TXN]=y,_[we.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[we.FUNDING_TXN]={txn:w[we.FUNDING_TXN],signers:[c]},b[we.VALIDATOR_APP_CALL]={txn:w[we.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Le(t){return function(t,e){var n=jt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Fe(t)+1e5}function Fe(e){return((e?De:Se)+1)*t.ALGORAND_MIN_TX_FEE}function Re(){return(Re=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=qt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===we.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(){return(Be=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Ee(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Zt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",ge.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Me,Ce={generateTxns:function(t){return ke.apply(this,arguments)},signTxns:function(t){return Re.apply(this,arguments)},execute:function(t){return Be.apply(this,arguments)},getBootstrapFundingTxnAmount:Le,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Le(e)}};var Ve,Xe=(f(Me={},S.V1_1,be),f(Me,S.V2,Ce),f(Me,"generateTxns",(function(t){return t.contractVersion===S.V1_1?be.generateTxns(t):Ce.generateTxns(t)})),f(Me,"signTxns",(function(t){return t.contractVersion===S.V1_1?be.signTxns(t):Ce.signTxns(t)})),f(Me,"execute",(function(t){return t.contractVersion===S.V1_1?be.execute(t):Ce.execute(t)})),f(Me,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?be.getBootstrapFundingTxnAmount(n):Ce.getBootstrapFundingTxnAmount(n)})),Me);exports.V1_1AddLiquidityTxnIndices=void 0,(Ve=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ve.FEE_TXN=0]="FEE_TXN",Ve[Ve.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ve[Ve.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ve[Ve.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ve[Ve.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var qe,je,Qe,Ue,Ge=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Ue=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Ue.FLEXIBLE="flexible",Ue.INITIAL="initial";var We,ze=(f(qe={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(qe,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(qe,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),qe),Je=(f(je={},exports.V2AddLiquidityType.INITIAL,1),f(je,exports.V2AddLiquidityType.SINGLE,2),f(je,exports.V2AddLiquidityType.FLEXIBLE,2),je),Ye=(f(Qe={},exports.V2AddLiquidityType.INITIAL,3),f(Qe,exports.V2AddLiquidityType.FLEXIBLE,3),f(Qe,exports.V2AddLiquidityType.SINGLE,2),Qe);function Ke(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function He(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=Ke({assetIn:r,assetOut:a}),o=Z(a.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(s/o-1))}function Ze(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,u=t.asset1Amount,i=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(u),d=s.asset2+BigInt(i),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(u)-x,h=BigInt(i)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=$e(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=$e(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:He({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function $e(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function tn(e){return(Je[e]+1)*t.ALGORAND_MIN_TX_FEE}var en=it("add_liquidity"),nn=(f(We={},S.V1_1,[it("mint")]),f(We,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[en,it("single")],FLEXIBLE_MODE:[en,it("flexible")]}),We);function rn(){return(rn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(s,S.V1_1),appArgs:nn.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function an(){return(an=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sn(){return(sn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Yt({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=st(a),x=ot(a),t.next=17,Yt({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var on=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Nn(){return(Nn=c(u().mark((function t(e){var n,a,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Ot(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,a=e.initiatorAddr,s=e.slippage,i=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=C(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:H("positive",s,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",s,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:l.validatorAppID,appArgs:[Tt,yt[i],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Pn(i),(_=[])[It.INPUT_TXN]=y,_[It.APP_CALL_TXN]=v,w=r.default.assignGroupID(_),t.abrupt("return",[{txn:w[It.INPUT_TXN],signers:[a]},{txn:w[It.APP_CALL_TXN],signers:[a]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(e){return(ht[e]+1)*t.ALGORAND_MIN_TX_FEE}function kn(){return(kn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,rt(a,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Ne(a,o);case 10:return m=t.sent,g=s.type===exports.SwapQuoteType.Direct?{id:s.quoteWithPool.quote.assetInID,amount:s.quoteWithPool.quote.assetInAmount}:{id:Number(s.asset_in_id),amount:Number(s.route[0].quote.amount_in.amount)},x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ln(){return(Ln=c(u().mark((function t(e,n,r,a,s,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e!==exports.SwapType.FixedInput){t.next=6;break}return t.next=3,Fn({pool:n,assetIn:r,decimals:a,isSwapRouterEnabled:o,network:s});case 3:i=t.sent,t.next=9;break;case 6:return t.next=8,Bn({pool:n,assetOut:r,decimals:a,isSwapRouterEnabled:o,network:s});case 8:i=t.sent;case 9:return t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Fn(t){return Rn.apply(this,arguments)}function Rn(){return(Rn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,i=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 17:throw new F({pool:a,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=Cn({inputSupply:d,outputSupply:f,swapInputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapOutputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(g>f)){t.next=21;break}throw new On;case 21:if(A={assetInID:n.id,assetInAmount:c,assetOutID:l,assetOutAmount:g,swapFee:Number(x),rate:Z(r.assetOut,Number(g))/Z(r.assetIn,Number(c)),priceImpact:I},!s){t.next=28;break}return t.next=25,Lt({amount:n.amount,assetInID:Number(n.id),assetOutID:l,swapType:exports.SwapType.FixedInput,network:i});case 25:if(!((h=t.sent).route.length>1&&BigInt(h.route[h.route.length-1].quote.amount_out.amount)>A.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},h),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:a,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Bn(t){return Mn.apply(this,arguments)}function Mn(){return(Mn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,i=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 17:throw new F({pool:a,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=Vn({inputSupply:d,outputSupply:f,swapOutputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapInputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(c>f)){t.next=21;break}throw new On;case 21:if(A={assetInID:l,assetInAmount:g,assetOutID:n.id,assetOutAmount:c,swapFee:Number(x),rate:Z(r.assetOut,Number(c))/Z(r.assetIn,Number(g)),priceImpact:I},!s){t.next=28;break}return t.next=25,Lt({amount:n.amount,assetInID:l,assetOutID:n.id,swapType:exports.SwapType.FixedInput,network:i});case 25:if(!((h=t.sent).route.length>1&&BigInt(h.route[h.route.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function jn(){return(jn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[yn.ASSET_TRANSFER_TXN]=g,I[yn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[yn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[yn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Qn(){return(Qn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[yn.ASSET_TRANSFER_TXN]=T,v[yn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[yn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[yn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Un(){return(Un=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,rt(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Ne(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Gn,Wn,zn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=qn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=qn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Xn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Xn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return jn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Qn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Un.apply(this,arguments)}},Jn=(f(Gn={},S.V1_1,En),f(Gn,S.V2,zn),Gn),Yn=3n,Kn=1000n;function Hn(){return(Hn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===Wn.FEE_TXN_INDEX?c:e===Wn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.quote,o=e.swapType,i=e.slippage,c=e.initiatorAddr,p=s.pool,l=s.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:s},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",i,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",i,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),_=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:a,suggestedParams:x}),w=r.default.assignGroupID([_,A,T,v]),t.abrupt("return",[{txn:w[0],signers:[c]},{txn:w[1],signers:[m]},{txn:w[2],signers:[c]},{txn:w[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*Yn/Kn,l=i-u*i/(u+(c-p));if(l>i)throw new On;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:Ke(d),priceImpact:He(o({inputSupply:u,outputSupply:i},d))}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Yt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Yt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new On;var c=o*u/(u-i)-o,p=c*Kn/(Kn-Yn),l=p-c,d=Z(a.assetOut,Number(i))/Z(a.assetIn,Number(p)),f=Z(a.assetOut,Number(u))/Z(a.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m}}function rr(t){return ar.apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Yt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Yt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sr(){return(sr=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Wn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Wn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,tr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,rr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(s),fees:st(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Wn||(Wn={}));var or,ur={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?$n({pool:e,reserves:n,assetIn:r,decimals:a}):nr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:$n,getFixedOutputSwapQuote:nr,generateTxns:function(t){return Zn.apply(this,arguments)},signTxns:function(t){return Hn.apply(this,arguments)},execute:function(t){return sr.apply(this,arguments)},executeFixedOutputSwap:rr},ir=4*t.ALGORAND_MIN_TX_FEE;function cr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof On})))throw new On;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function pr(){return pr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var s=c(u().mark((function s(i,c){var p,l;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedInputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Xn.getFixedInputSwapQuote(l);case 8:p=s.sent;case 9:i(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),pr.apply(this,arguments)}function lr(){return lr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var s=c(u().mark((function s(i,c){var p,l;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedOutputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Xn.getFixedOutputSwapQuote(l);case 8:p=s.sent;case 9:i(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),lr.apply(this,arguments)}function dr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:bt(t.route)}function fr(t){var e,n=t[0],r=dr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=dr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}function mr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var gr=(f(or={},S.V1_1,ur),f(or,S.V2,Xn),f(or,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return le(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return pr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return lr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(or,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&mr(t.quote)===S.V1_1?ur.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):Xn.generateTxns(t)})),f(or,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&mr(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return ur.signTxns(o({pool:e},t))}return Xn.signTxns(t)})),f(or,"execute",(function(t){return t.contractVersion===S.V1_1?ur.execute(t):Xn.execute(t)})),or);function xr(){return(xr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,Ir({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:st(a),confirmedRound:d,txnID:l,groupID:ot(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function Ir(t){return Ar.apply(this,arguments)}function Ar(){return(Ar=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hr(){return hr=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:st(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,rt(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),hr.apply(this,arguments)}function Tr(){return(Tr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:q([it("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function vr(){return(vr=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=yr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Tn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Xe,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Jn,exports.Swap=gr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=vn,exports.V2AddLiquidityTxnIndices=ze,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=Gt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=ge,exports.prepareCommitTransactions=function(t){return vr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return hr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return xr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=st,exports.tinymanContract_v1_1=Ct,exports.tinymanContract_v2=qt,exports.tinymanJSSDKConfig=pt; From 2069945a547414c4b2a90b289d629c1112d6c53e Mon Sep 17 00:00:00 2001 From: anlerkan Date: Tue, 7 Mar 2023 13:02:34 +0300 Subject: [PATCH 15/39] refactor: code improvements --- dist/index.js | 2 +- dist/swap/index.d.ts | 15 +++++++++---- dist/swap/types.d.ts | 13 ++++++----- dist/swap/v2/constants.d.ts | 2 +- dist/swap/v2/index.d.ts | 15 +++++++++---- dist/swap/v2/router/constants.d.ts | 5 +++++ dist/swap/v2/router/util.d.ts | 11 ++++++++-- dist/util/asset/assetUtils.d.ts | 2 +- src/swap/types.ts | 15 +++++++------ src/swap/utils.ts | 5 +---- src/swap/v1_1/index.ts | 1 - src/swap/v2/constants.ts | 2 +- src/swap/v2/index.ts | 29 +++++++++++++++---------- src/swap/v2/router/constants.ts | 10 +++++++++ src/swap/v2/router/swap-router.ts | 35 +++++++++++++++++------------- src/swap/v2/router/util.ts | 14 +++++++----- src/util/asset/assetUtils.ts | 2 +- 17 files changed, 114 insertions(+), 64 deletions(-) diff --git a/dist/index.js b/dist/index.js index 2fef0f23..8e09c228 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),a=Uint8Array.from([1]);function s(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function X(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[s?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function q(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=D(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var j=100000n,Q=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return j+Q*BigInt(o.length)+U*BigInt(s.length+a.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(nt(e),2),s=a[0],o=a[1],u=v(nt(Math.round(Number(et(s,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return at.apply(this,arguments)}function at(){return(at=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function st(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),Tt=it("swap"),yt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),vt={ASSET_OPT_IN:it("asset_opt_in")},_t={testnet:159521633,mainnet:0},wt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function bt(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Dt(t){if(!_t[t])throw new Error("Unknown network or network not supported: ".concat(t));return _t[t]}function St(t){return Et.apply(this,arguments)}function Et(){return(Et=c(u().mark((function e(n){var a,s,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[vt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ot(t){return Nt.apply(this,arguments)}function Nt(){return(Nt=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E,O,N;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(c[0].quote.amount_in.amount),Number(c[c.length-1].quote.amount_out.amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Dt(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[Tt,yt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p}),b=i===exports.SwapType.FixedInput?7:8,w.fee=t.ALGORAND_MIN_TX_FEE*(b+1),D=[_,w],e.next=16,Pt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 16:if(!((E=e.sent).length>0)){e.next=22;break}return e.next=20,St({client:s,accountAddress:a,assetIDs:E,routerAppID:v});case 20:O=e.sent,D.unshift(O);case 22:return N=r.default.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[a]}})));case 24:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Pt(t){return kt.apply(this,arguments)}function kt(){return(kt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(wt[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Rt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Bt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Mt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Ct=new(function(n){m(a,Mt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:Gt(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Ut(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?X({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Gt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Wt,zt,Jt=it("e");function Yt(t){return Kt.apply(this,arguments)}function Kt(){return(Kt=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=X({stateArray:A}),T=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,Jt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,Jt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,Jt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ht(){return(Ht=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=X({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mte)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(te,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ae(){return ae=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,Qt(n,r);case 6:return o=t.sent,i=Ut(o,dt(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[Zt[S.V1_1].asset1],asset2ID:i[Zt[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ae.apply(this,arguments)}var se=Object.freeze({__proto__:null,getPoolInfo:ee,getPoolReserves:function(t,e){return re.apply(this,arguments)},getPoolAssets:function(t){return ae.apply(this,arguments)}});function oe(t){return ue.apply(this,arguments)}function ue(){return(ue=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=jt(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(a,s),t.next=8,Qt(n,p);case 8:return d=t.sent,f=Ut(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[Zt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Zt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Zt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Zt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Zt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Zt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Zt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Zt.v2.totalFeeShare]),m.poolTokenID=Number(f[Zt.v2.poolTokenID]),m.asset1ID=Number(f[Zt.v2.asset1]),m.asset2ID=Number(f[Zt.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ie(){return(ie=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Qt(e,n.account.address());case 2:return r=t.sent,a=Ut(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[Zt.v2.asset1Reserves]),s.asset2=BigInt(a[Zt.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[Zt.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ce(){return(ce=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,Qt(n,r);case 3:return s=t.sent,o=Ut(s,dt(a,S.V2)),i=null,o&&(i={asset1ID:o[Zt[S.V2].asset1],asset2ID:o[Zt[S.V2].asset2],poolTokenID:o[Zt[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pe=Object.freeze({__proto__:null,getPoolInfo:oe,getPoolReserves:function(t,e){return ie.apply(this,arguments)},getPoolAssets:function(t){return ce.apply(this,arguments)}});function le(t){return Boolean(t&&!(t.asset1+t.asset2))}var de,fe,me=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=le(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:le,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([ee(t),oe(t)])}}),ge=o((f(de={},S.V1_1,o(o({},se),me)),f(de,S.V2,o(o({},pe),me)),de),me);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(fe||(fe={}));var xe=96e4,Ie=859e3;function Ae(){return(Ae=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,P;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(a,S.V1_1),T=Ct.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:he(A),suggestedParams:c}),(E=[])[fe.FUNDING_TXN]=D,E[fe.VALIDATOR_APP_CALL]=_,E[fe.POOL_TOKEN_CREATE]=w,E[fe.ASSET1_OPT_IN]=b,A||(E[fe.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),P=[{txn:N[fe.FUNDING_TXN],signers:[i]},{txn:N[fe.VALIDATOR_APP_CALL],signers:[y]},{txn:N[fe.POOL_TOKEN_CREATE],signers:[y]},{txn:N[fe.ASSET1_OPT_IN],signers:[y]}],N[fe.ASSET2_OPT_IN]&&P.push({txn:N[fe.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",P);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function he(t){return t?xe:Ie}function Te(){return(Te=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Ct.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===fe.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ye(t){return ve.apply(this,arguments)}function ve(){return(ve=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,a[fe.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function _e(){return(_e=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,ye({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",ge.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var we,be={generateTxns:function(t){return Ae.apply(this,arguments)},signTxns:function(t){return Te.apply(this,arguments)},execute:function(t){return _e.apply(this,arguments)},getBootstrapFundingTxnAmount:he};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(we||(we={}));var De=5,Se=6;function Ee(t,e){return Oe.apply(this,arguments)}function Oe(){return(Oe=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ne(t,e){return Pe.apply(this,arguments)}function Pe(){return(Pe=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Ee(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ke(){return(ke=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=dt(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,ge.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=qt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Fe(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Le(h),suggestedParams:p}),(_=[])[we.FUNDING_TXN]=y,_[we.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[we.FUNDING_TXN]={txn:w[we.FUNDING_TXN],signers:[c]},b[we.VALIDATOR_APP_CALL]={txn:w[we.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Le(t){return function(t,e){var n=jt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Fe(t)+1e5}function Fe(e){return((e?De:Se)+1)*t.ALGORAND_MIN_TX_FEE}function Re(){return(Re=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=qt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===we.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(){return(Be=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Ee(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Zt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",ge.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Me,Ce={generateTxns:function(t){return ke.apply(this,arguments)},signTxns:function(t){return Re.apply(this,arguments)},execute:function(t){return Be.apply(this,arguments)},getBootstrapFundingTxnAmount:Le,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Le(e)}};var Ve,Xe=(f(Me={},S.V1_1,be),f(Me,S.V2,Ce),f(Me,"generateTxns",(function(t){return t.contractVersion===S.V1_1?be.generateTxns(t):Ce.generateTxns(t)})),f(Me,"signTxns",(function(t){return t.contractVersion===S.V1_1?be.signTxns(t):Ce.signTxns(t)})),f(Me,"execute",(function(t){return t.contractVersion===S.V1_1?be.execute(t):Ce.execute(t)})),f(Me,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?be.getBootstrapFundingTxnAmount(n):Ce.getBootstrapFundingTxnAmount(n)})),Me);exports.V1_1AddLiquidityTxnIndices=void 0,(Ve=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ve.FEE_TXN=0]="FEE_TXN",Ve[Ve.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ve[Ve.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ve[Ve.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ve[Ve.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var qe,je,Qe,Ue,Ge=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Ue=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Ue.FLEXIBLE="flexible",Ue.INITIAL="initial";var We,ze=(f(qe={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(qe,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(qe,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),qe),Je=(f(je={},exports.V2AddLiquidityType.INITIAL,1),f(je,exports.V2AddLiquidityType.SINGLE,2),f(je,exports.V2AddLiquidityType.FLEXIBLE,2),je),Ye=(f(Qe={},exports.V2AddLiquidityType.INITIAL,3),f(Qe,exports.V2AddLiquidityType.FLEXIBLE,3),f(Qe,exports.V2AddLiquidityType.SINGLE,2),Qe);function Ke(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function He(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=Ke({assetIn:r,assetOut:a}),o=Z(a.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(s/o-1))}function Ze(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,u=t.asset1Amount,i=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(u),d=s.asset2+BigInt(i),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(u)-x,h=BigInt(i)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=$e(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=$e(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:He({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function $e(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function tn(e){return(Je[e]+1)*t.ALGORAND_MIN_TX_FEE}var en=it("add_liquidity"),nn=(f(We={},S.V1_1,[it("mint")]),f(We,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[en,it("single")],FLEXIBLE_MODE:[en,it("flexible")]}),We);function rn(){return(rn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(s,S.V1_1),appArgs:nn.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function an(){return(an=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sn(){return(sn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Yt({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=st(a),x=ot(a),t.next=17,Yt({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var on=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Nn(){return(Nn=c(u().mark((function t(e){var n,a,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Ot(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,a=e.initiatorAddr,s=e.slippage,i=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=C(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:H("positive",s,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",s,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:l.validatorAppID,appArgs:[Tt,yt[i],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Pn(i),(_=[])[It.INPUT_TXN]=y,_[It.APP_CALL_TXN]=v,w=r.default.assignGroupID(_),t.abrupt("return",[{txn:w[It.INPUT_TXN],signers:[a]},{txn:w[It.APP_CALL_TXN],signers:[a]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(e){return(ht[e]+1)*t.ALGORAND_MIN_TX_FEE}function kn(){return(kn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,rt(a,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Ne(a,o);case 10:return m=t.sent,g=s.type===exports.SwapQuoteType.Direct?{id:s.quoteWithPool.quote.assetInID,amount:s.quoteWithPool.quote.assetInAmount}:{id:Number(s.asset_in_id),amount:Number(s.route[0].quote.amount_in.amount)},x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ln(){return(Ln=c(u().mark((function t(e,n,r,a,s,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e!==exports.SwapType.FixedInput){t.next=6;break}return t.next=3,Fn({pool:n,assetIn:r,decimals:a,isSwapRouterEnabled:o,network:s});case 3:i=t.sent,t.next=9;break;case 6:return t.next=8,Bn({pool:n,assetOut:r,decimals:a,isSwapRouterEnabled:o,network:s});case 8:i=t.sent;case 9:return t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Fn(t){return Rn.apply(this,arguments)}function Rn(){return(Rn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,i=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 17:throw new F({pool:a,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=Cn({inputSupply:d,outputSupply:f,swapInputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapOutputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(g>f)){t.next=21;break}throw new On;case 21:if(A={assetInID:n.id,assetInAmount:c,assetOutID:l,assetOutAmount:g,swapFee:Number(x),rate:Z(r.assetOut,Number(g))/Z(r.assetIn,Number(c)),priceImpact:I},!s){t.next=28;break}return t.next=25,Lt({amount:n.amount,assetInID:Number(n.id),assetOutID:l,swapType:exports.SwapType.FixedInput,network:i});case 25:if(!((h=t.sent).route.length>1&&BigInt(h.route[h.route.length-1].quote.amount_out.amount)>A.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},h),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:a,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Bn(t){return Mn.apply(this,arguments)}function Mn(){return(Mn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,i=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 17:throw new F({pool:a,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=Vn({inputSupply:d,outputSupply:f,swapOutputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapInputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(c>f)){t.next=21;break}throw new On;case 21:if(A={assetInID:l,assetInAmount:g,assetOutID:n.id,assetOutAmount:c,swapFee:Number(x),rate:Z(r.assetOut,Number(c))/Z(r.assetIn,Number(g)),priceImpact:I},!s){t.next=28;break}return t.next=25,Lt({amount:n.amount,assetInID:l,assetOutID:n.id,swapType:exports.SwapType.FixedInput,network:i});case 25:if(!((h=t.sent).route.length>1&&BigInt(h.route[h.route.length-1].quote.amount_in.amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function jn(){return(jn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[yn.ASSET_TRANSFER_TXN]=g,I[yn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[yn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[yn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Qn(){return(Qn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[_n,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[yn.ASSET_TRANSFER_TXN]=T,v[yn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[yn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[yn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Un(){return(Un=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,rt(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Ne(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Gn,Wn,zn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=qn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=qn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Xn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Xn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return jn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Qn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Un.apply(this,arguments)}},Jn=(f(Gn={},S.V1_1,En),f(Gn,S.V2,zn),Gn),Yn=3n,Kn=1000n;function Hn(){return(Hn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===Wn.FEE_TXN_INDEX?c:e===Wn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.quote,o=e.swapType,i=e.slippage,c=e.initiatorAddr,p=s.pool,l=s.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:s},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",i,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",i,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),_=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:a,suggestedParams:x}),w=r.default.assignGroupID([_,A,T,v]),t.abrupt("return",[{txn:w[0],signers:[c]},{txn:w[1],signers:[m]},{txn:w[2],signers:[c]},{txn:w[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $n(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*Yn/Kn,l=i-u*i/(u+(c-p));if(l>i)throw new On;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:Ke(d),priceImpact:He(o({inputSupply:u,outputSupply:i},d))}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Yt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Yt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new On;var c=o*u/(u-i)-o,p=c*Kn/(Kn-Yn),l=p-c,d=Z(a.assetOut,Number(i))/Z(a.assetIn,Number(p)),f=Z(a.assetOut,Number(u))/Z(a.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m}}function rr(t){return ar.apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,Yt({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Yt({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sr(){return(sr=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Wn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Wn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Wn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,tr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,rr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(s),fees:st(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Wn||(Wn={}));var or,ur={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?$n({pool:e,reserves:n,assetIn:r,decimals:a}):nr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:$n,getFixedOutputSwapQuote:nr,generateTxns:function(t){return Zn.apply(this,arguments)},signTxns:function(t){return Hn.apply(this,arguments)},execute:function(t){return sr.apply(this,arguments)},executeFixedOutputSwap:rr},ir=4*t.ALGORAND_MIN_TX_FEE;function cr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof On})))throw new On;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function pr(){return pr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var s=c(u().mark((function s(i,c){var p,l;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedInputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Xn.getFixedInputSwapQuote(l);case 8:p=s.sent;case 9:i(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),pr.apply(this,arguments)}function lr(){return lr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var s=c(u().mark((function s(i,c){var p,l;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:ur.getFixedOutputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Xn.getFixedOutputSwapQuote(l);case 8:p=s.sent;case 9:i(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,cr(i);case 4:return p=t.sent,t.abrupt("return",fr(p));case 6:case"end":return t.stop()}}),t)}))),lr.apply(this,arguments)}function dr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:bt(t.route)}function fr(t){var e,n=t[0],r=dr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=dr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}function mr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var gr=(f(or={},S.V1_1,ur),f(or,S.V2,Xn),f(or,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return le(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return pr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return lr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(or,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&mr(t.quote)===S.V1_1?ur.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):Xn.generateTxns(t)})),f(or,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&mr(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return ur.signTxns(o({pool:e},t))}return Xn.signTxns(t)})),f(or,"execute",(function(t){return t.contractVersion===S.V1_1?ur.execute(t):Xn.execute(t)})),or);function xr(){return(xr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,Ir({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:st(a),confirmedRound:d,txnID:l,groupID:ot(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function Ir(t){return Ar.apply(this,arguments)}function Ar(){return(Ar=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hr(){return hr=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:st(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,rt(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),hr.apply(this,arguments)}function Tr(){return(Tr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:q([it("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function vr(){return(vr=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=yr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Tn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Xe,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Jn,exports.Swap=gr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=vn,exports.V2AddLiquidityTxnIndices=ze,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=Gt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=ge,exports.prepareCommitTransactions=function(t){return vr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return hr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return xr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=st,exports.tinymanContract_v1_1=Ct,exports.tinymanContract_v2=qt,exports.tinymanJSSDKConfig=pt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",s=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,s=Object.create(a.prototype),o=new w(r||[]);return s._invoke=function(t,e,n){var r="suspendedStart";return function(a,s){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw s;return D()}for(n.method=a,n.arg=s;;){var o=n.delegate;if(o){var u=y(o,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,o),s}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,a)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,s,o,u){var i=p(t[a],t,s);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,u)}),(function(t){r("throw",t,o,u)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function s(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(s,s):s()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function w(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,s=function e(){for(;++r=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",k="logic eval error:",P="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function X(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[s?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function j(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=D(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var q=100000n,Q=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return q+Q*BigInt(o.length)+U*BigInt(s.length+a.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(nt(e),2),s=a[0],o=a[1],u=v(nt(Math.round(Number(et(s,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return at.apply(this,arguments)}function at(){return(at=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function st(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht,Tt=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),yt=it("swap"),vt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),_t={ASSET_OPT_IN:it("asset_opt_in")},wt={testnet:159521633,mainnet:0},bt=(f(ht={},exports.SwapType.FixedInput,7),f(ht,exports.SwapType.FixedOutput,8),ht),Dt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function St(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Et(t){if(!wt[t])throw new Error("Unknown network or network not supported: ".concat(t));return wt[t]}function Ot(t){return t[t.length-1].quote.amount_out}function Nt(t){return t[0].quote.amount_in}function kt(t){return Pt.apply(this,arguments)}function Pt(){return(Pt=c(u().mark((function e(n){var a,s,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[_t.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E,O;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Nt(c).amount),Number(Ot(c).amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Et(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[yt,vt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(bt[i]+1),b=[_,w],e.next=15,Rt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((D=e.sent).length>0)){e.next=21;break}return e.next=19,kt({client:s,accountAddress:a,assetIDs:D,routerAppID:v});case 19:E=e.sent,b.unshift(E);case 21:return O=r.default.assignGroupID(b),e.abrupt("return",O.map((function(t){return{txn:t,signers:[a]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Rt(t){return Bt.apply(this,arguments)}function Bt(){return(Bt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(Dt[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Vt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Xt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},jt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),qt=new(function(n){m(a,jt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:Yt(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Jt(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?X({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Yt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Kt,Ht,Zt=it("e");function $t(t){return te.apply(this,arguments)}function te(){return(te=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=X({stateArray:A}),T=e.fromByteArray(j([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(j([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(j([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ee(){return(ee=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=X({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mae)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ae,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ie(){return ie=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,zt(n,r);case 6:return o=t.sent,i=Jt(o,dt(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[ne[S.V1_1].asset1],asset2ID:i[ne[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ie.apply(this,arguments)}var ce=Object.freeze({__proto__:null,getPoolInfo:se,getPoolReserves:function(t,e){return ue.apply(this,arguments)},getPoolAssets:function(t){return ie.apply(this,arguments)}});function pe(t){return le.apply(this,arguments)}function le(){return(le=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Wt(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(a,s),t.next=8,zt(n,p);case 8:return d=t.sent,f=Jt(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[ne.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ne.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ne.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ne.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ne.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ne.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ne.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ne.v2.totalFeeShare]),m.poolTokenID=Number(f[ne.v2.poolTokenID]),m.asset1ID=Number(f[ne.v2.asset1]),m.asset2ID=Number(f[ne.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function de(){return(de=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,zt(e,n.account.address());case 2:return r=t.sent,a=Jt(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[ne.v2.asset1Reserves]),s.asset2=BigInt(a[ne.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[ne.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(){return(fe=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,zt(n,r);case 3:return s=t.sent,o=Jt(s,dt(a,S.V2)),i=null,o&&(i={asset1ID:o[ne[S.V2].asset1],asset2ID:o[ne[S.V2].asset2],poolTokenID:o[ne[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var me=Object.freeze({__proto__:null,getPoolInfo:pe,getPoolReserves:function(t,e){return de.apply(this,arguments)},getPoolAssets:function(t){return fe.apply(this,arguments)}});function ge(t){return Boolean(t&&!(t.asset1+t.asset2))}var xe,Ie,Ae=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ge(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:ge,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([se(t),pe(t)])}}),he=o((f(xe={},S.V1_1,o(o({},ce),Ae)),f(xe,S.V2,o(o({},me),Ae)),xe),Ae);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Ie||(Ie={}));var Te=96e4,ye=859e3;function ve(){return(ve=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,k;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(a,S.V1_1),T=qt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:_e(A),suggestedParams:c}),(E=[])[Ie.FUNDING_TXN]=D,E[Ie.VALIDATOR_APP_CALL]=_,E[Ie.POOL_TOKEN_CREATE]=w,E[Ie.ASSET1_OPT_IN]=b,A||(E[Ie.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),k=[{txn:N[Ie.FUNDING_TXN],signers:[i]},{txn:N[Ie.VALIDATOR_APP_CALL],signers:[y]},{txn:N[Ie.POOL_TOKEN_CREATE],signers:[y]},{txn:N[Ie.ASSET1_OPT_IN],signers:[y]}],N[Ie.ASSET2_OPT_IN]&&k.push({txn:N[Ie.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",k);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(t){return t?Te:ye}function we(){return(we=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=qt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ie.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function be(t){return De.apply(this,arguments)}function De(){return(De=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,a[Ie.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Se(){return(Se=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,be({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",he.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ee,Oe={generateTxns:function(t){return ve.apply(this,arguments)},signTxns:function(t){return we.apply(this,arguments)},execute:function(t){return Se.apply(this,arguments)},getBootstrapFundingTxnAmount:_e};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ee||(Ee={}));var Ne=5,ke=6;function Pe(t,e){return Le.apply(this,arguments)}function Le(){return(Le=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fe(t,e){return Re.apply(this,arguments)}function Re(){return(Re=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Pe(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(){return(Be=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=dt(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,he.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Gt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ce(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Me(h),suggestedParams:p}),(_=[])[Ee.FUNDING_TXN]=y,_[Ee.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Ee.FUNDING_TXN]={txn:w[Ee.FUNDING_TXN],signers:[c]},b[Ee.VALIDATOR_APP_CALL]={txn:w[Ee.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Me(t){return function(t,e){var n=Wt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ce(t)+1e5}function Ce(e){return((e?Ne:ke)+1)*t.ALGORAND_MIN_TX_FEE}function Ve(){return(Ve=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Gt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ee.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Pe(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ne.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",he.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var je,qe={generateTxns:function(t){return Be.apply(this,arguments)},signTxns:function(t){return Ve.apply(this,arguments)},execute:function(t){return Xe.apply(this,arguments)},getBootstrapFundingTxnAmount:Me,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Me(e)}};var Qe,Ue=(f(je={},S.V1_1,Oe),f(je,S.V2,qe),f(je,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Oe.generateTxns(t):qe.generateTxns(t)})),f(je,"signTxns",(function(t){return t.contractVersion===S.V1_1?Oe.signTxns(t):qe.signTxns(t)})),f(je,"execute",(function(t){return t.contractVersion===S.V1_1?Oe.execute(t):qe.execute(t)})),f(je,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Oe.getBootstrapFundingTxnAmount(n):qe.getBootstrapFundingTxnAmount(n)})),je);exports.V1_1AddLiquidityTxnIndices=void 0,(Qe=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Qe.FEE_TXN=0]="FEE_TXN",Qe[Qe.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Qe[Qe.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Qe[Qe.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Qe[Qe.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Ge,We,ze,Je,Ye=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Je=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Je.FLEXIBLE="flexible",Je.INITIAL="initial";var Ke,He=(f(Ge={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Ge,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Ge,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Ge),Ze=(f(We={},exports.V2AddLiquidityType.INITIAL,1),f(We,exports.V2AddLiquidityType.SINGLE,2),f(We,exports.V2AddLiquidityType.FLEXIBLE,2),We),$e=(f(ze={},exports.V2AddLiquidityType.INITIAL,3),f(ze,exports.V2AddLiquidityType.FLEXIBLE,3),f(ze,exports.V2AddLiquidityType.SINGLE,2),ze);function tn(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function en(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=tn({assetIn:r,assetOut:a}),o=Z(a.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(s/o-1))}function nn(t){var e,n,r,a,s=t.reserves,o=t.totalFeeShare,u=t.asset1Amount,i=t.asset2Amount,c=t.decimals,p=s.asset1*s.asset2,l=s.asset1+BigInt(u),d=s.asset2+BigInt(i),f=l*d,m=BigInt(parseInt(String(Math.sqrt(Number(f*s.issuedLiquidity*s.issuedLiquidity/p))))),g=m-s.issuedLiquidity,x=g*l/m,I=g*d/m,A=BigInt(u)-x,h=BigInt(i)-I;if(A>h){var T=A;r=BigInt(Math.abs(Math.min(Number(h),0))),e=!0,n=T+(a=rn(T,o)),g-=a*m/(l*BigInt(2))}else{var y=h;r=BigInt(Math.abs(Math.min(Number(A),0))),e=!1,n=y+(a=rn(y,o)),g-=a*m/(d*BigInt(2))}return{poolTokenAssetAmount:g,swapFromAsset1ToAsset2:e,swapInAmount:n,swapOutAmount:r,swapTotalFeeAmount:a,swapPriceImpact:en({inputSupply:e?s.asset1:s.asset2,outputSupply:e?s.asset2:s.asset1,assetIn:{amount:n,decimals:c.asset1},assetOut:{amount:r,decimals:c.asset2}})}}function rn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function an(e){return(Ze[e]+1)*t.ALGORAND_MIN_TX_FEE}var sn=it("add_liquidity"),on=(f(Ke={},S.V1_1,[it("mint")]),f(Ke,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[sn,it("single")],FLEXIBLE_MODE:[sn,it("flexible")]}),Ke);function un(){return(un=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(s,S.V1_1),appArgs:on.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(){return(cn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pn(){return(pn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,$t({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=st(a),x=ot(a),t.next=17,$t({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var ln=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Fn(){return(Fn=c(u().mark((function t(e){var n,a,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Lt(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,a=e.initiatorAddr,s=e.slippage,i=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=C(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:H("positive",s,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",s,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:l.validatorAppID,appArgs:[yt,vt[i],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Rn(i),(_=[])[It.INPUT_TXN]=y,_[It.APP_CALL_TXN]=v,w=r.default.assignGroupID(_),t.abrupt("return",[{txn:w[It.INPUT_TXN],signers:[a]},{txn:w[It.APP_CALL_TXN],signers:[a]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rn(e){return(Tt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Bn(){return(Bn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,rt(a,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Fe(a,o);case 10:return m=t.sent,g=s.type===exports.SwapQuoteType.Direct?{id:s.quoteWithPool.quote.assetInID,amount:s.quoteWithPool.quote.assetInAmount}:{id:Number(s.asset_in_id),amount:Number(s.route[0].quote.amount_in.amount)},x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Mn(){return(Mn=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.pool,a=e.asset,s=e.decimals,o=e.network,i=e.isSwapRouterEnabled,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,Cn({pool:r,assetIn:a,decimals:s,isSwapRouterEnabled:i,network:o});case 4:c=t.sent,t.next=10;break;case 7:return t.next=9,Xn({pool:r,assetOut:a,decimals:s,isSwapRouterEnabled:i,network:o});case 9:c=t.sent;case 10:return t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Cn(t){return Vn.apply(this,arguments)}function Vn(){return(Vn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,i=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 17:throw new F({pool:a,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=qn({inputSupply:d,outputSupply:f,swapInputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapOutputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(g>f)){t.next=21;break}throw new Ln;case 21:if(A={assetInID:n.id,assetInAmount:c,assetOutID:l,assetOutAmount:g,swapFee:Number(x),rate:Z(r.assetOut,Number(g))/Z(r.assetIn,Number(c)),priceImpact:I},!s){t.next=28;break}return t.next=25,Mt({amount:n.amount,assetInID:Number(n.id),assetOutID:l,swapType:exports.SwapType.FixedInput,network:i});case 25:if(!((h=t.sent).route.length>1&&BigInt(Ot(h.route).amount)>A.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},h),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:a,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xn(t){return jn.apply(this,arguments)}function jn(){return(jn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,i=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 17:throw new F({pool:a,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=Qn({inputSupply:d,outputSupply:f,swapOutputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapInputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(c>f)){t.next=21;break}throw new Ln;case 21:if(A={assetInID:l,assetInAmount:g,assetOutID:n.id,assetOutAmount:c,swapFee:Number(x),rate:Z(r.assetOut,Number(c))/Z(r.assetIn,Number(g)),priceImpact:I},!s){t.next=28;break}return t.next=25,Mt({amount:n.amount,assetInID:l,assetOutID:n.id,swapType:exports.SwapType.FixedInput,network:i});case 25:if(!((h=t.sent).route.length>1&&BigInt(Ot(h.route).amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Wn(){return(Wn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Sn,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[bn.ASSET_TRANSFER_TXN]=g,I[bn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[bn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[bn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zn(){return(zn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Sn,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[bn.ASSET_TRANSFER_TXN]=T,v[bn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[bn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[bn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jn(){return(Jn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,rt(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Fe(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Yn,Kn,Hn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Gn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=Gn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Un.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Un.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Wn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return zn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Jn.apply(this,arguments)}},Zn=(f(Yn={},S.V1_1,Pn),f(Yn,S.V2,Hn),Yn),$n=3n,tr=1000n;function er(){return(er=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===Kn.FEE_TXN_INDEX?c:e===Kn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.quote,o=e.swapType,i=e.slippage,c=e.initiatorAddr,p=s.pool,l=s.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:s},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",i,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",i,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),_=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:a,suggestedParams:x}),w=r.default.assignGroupID([_,A,T,v]),t.abrupt("return",[{txn:w[0],signers:[c]},{txn:w[1],signers:[m]},{txn:w[2],signers:[c]},{txn:w[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*$n/tr,l=i-u*i/(u+(c-p));if(l>i)throw new Ln;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:tn(d),priceImpact:en(o({inputSupply:u,outputSupply:i},d))}}function ar(t){return sr.apply(this,arguments)}function sr(){return(sr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new Ln;var c=o*u/(u-i)-o,p=c*tr/(tr-$n),l=p-c,d=Z(a.assetOut,Number(i))/Z(a.assetIn,Number(p)),f=Z(a.assetOut,Number(u))/Z(a.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m}}function ur(t){return ir.apply(this,arguments)}function ir(){return(ir=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cr(){return(cr=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Kn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Kn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Kn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Kn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,ar({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,ur({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(s),fees:st(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Kn||(Kn={}));var pr,lr={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?rr({pool:e,reserves:n,assetIn:r,decimals:a}):or({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:rr,getFixedOutputSwapQuote:or,generateTxns:function(t){return nr.apply(this,arguments)},signTxns:function(t){return er.apply(this,arguments)},execute:function(t){return cr.apply(this,arguments)},executeFixedOutputSwap:ur},dr=4*t.ALGORAND_MIN_TX_FEE;function fr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Ln})))throw new Ln;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function mr(){return mr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var s=c(u().mark((function s(i,c){var p,l;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:lr.getFixedInputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Un.getFixedInputSwapQuote(l);case 8:p=s.sent;case 9:i(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,fr(i);case 4:return p=t.sent,t.abrupt("return",Ir(p));case 6:case"end":return t.stop()}}),t)}))),mr.apply(this,arguments)}function gr(){return gr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var s=c(u().mark((function s(i,c){var p,l;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:lr.getFixedOutputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Un.getFixedOutputSwapQuote(l);case 8:p=s.sent;case 9:i(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,fr(i);case 4:return p=t.sent,t.abrupt("return",Ir(p));case 6:case"end":return t.stop()}}),t)}))),gr.apply(this,arguments)}function xr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:St(t.route)}function Ir(t){var e,n=t[0],r=xr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=xr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}function Ar(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var hr=(f(pr={},S.V1_1,lr),f(pr,S.V2,Un),f(pr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ge(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return mr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return gr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(pr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&Ar(t.quote)===S.V1_1?lr.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):Un.generateTxns(t)})),f(pr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&Ar(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return lr.signTxns(o({pool:e},t))}return Un.signTxns(t)})),f(pr,"execute",(function(t){return t.contractVersion===S.V1_1?lr.execute(t):Un.execute(t)})),pr);function Tr(){return(Tr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,yr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:st(a),confirmedRound:d,txnID:l,groupID:ot(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function yr(t){return vr.apply(this,arguments)}function vr(){return(vr=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _r(){return _r=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:st(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,rt(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),_r.apply(this,arguments)}function wr(){return(wr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:j([it("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function Dr(){return(Dr=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=br({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=wn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Ue,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Zn,exports.Swap=hr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Dn,exports.V2AddLiquidityTxnIndices=He,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=Yt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=he,exports.prepareCommitTransactions=function(t){return Dr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return _r.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Tr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=st,exports.tinymanContract_v1_1=qt,exports.tinymanContract_v2=Gt,exports.tinymanJSSDKConfig=pt; diff --git a/dist/swap/index.d.ts b/dist/swap/index.d.ts index 7cb00bf3..e1b1b14d 100644 --- a/dist/swap/index.d.ts +++ b/dist/swap/index.d.ts @@ -47,10 +47,17 @@ export declare const Swap: { }) => Promise>; }; v2: { - getQuote: (type: import("./constants").SwapType, pool: import("..").V2PoolInfo, asset: import("../util/asset/assetModels").AssetWithIdAndAmount, decimals: { - assetIn: number; - assetOut: number; - }, network: import("..").SupportedNetwork, isSwapRouterEnabled?: boolean | undefined) => Promise; + getQuote: ({ type, pool, asset, decimals, network, isSwapRouterEnabled }: { + type: import("./constants").SwapType; + pool: import("..").V2PoolInfo; + asset: import("../util/asset/assetModels").AssetWithIdAndAmount; + decimals: { + assetIn: number; + assetOut: number; + }; + network: import("..").SupportedNetwork; + isSwapRouterEnabled?: boolean | undefined; + }) => Promise; getFixedInputSwapQuote: ({ assetIn, decimals, pool, isSwapRouterEnabled, network }: { pool: import("..").V2PoolInfo; assetIn: import("../util/asset/assetModels").AssetWithIdAndAmount; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index 9c799463..6ff9a4ff 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -72,6 +72,7 @@ export declare type GetSwapQuoteParams = { amount: number | bigint; type: SwapType; network: SupportedNetwork; + /** If `true`, the function will also check the quotes that use swap route */ isSwapRouterEnabled?: boolean; }; export interface SwapQuoteWithPool { @@ -85,12 +86,6 @@ export declare type SwapQuote = { type: SwapQuoteType.Router; }); export declare type GetSwapQuoteBySwapTypeParams = Omit; -export interface FetchSwapRouteQuotesPayload { - asset_in_id: string; - asset_out_id: string; - amount: string; - swap_type: SwapType; -} export interface SwapRouteAsset { id: string; name: string; @@ -146,6 +141,12 @@ export interface SwapRouterQuote { price: number; price_impact: number; } +export interface FetchSwapRouteQuotesPayload { + asset_in_id: string; + asset_out_id: string; + amount: string; + swap_type: SwapType; +} export declare type FetchSwapRouteQuotesResponse = FetchSwapRouteQuotesPayload & { route: SwapRoute; price_impact: string; diff --git a/dist/swap/v2/constants.d.ts b/dist/swap/v2/constants.d.ts index 5acf62b1..a07c9c4d 100644 --- a/dist/swap/v2/constants.d.ts +++ b/dist/swap/v2/constants.d.ts @@ -19,5 +19,5 @@ export declare const V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED: { readonly "fixed-output": Uint8Array; }; export declare const V2_SWAP_ROUTER_APP_ARGS_ENCODED: { - ASSET_OPT_IN: Uint8Array; + readonly ASSET_OPT_IN: Uint8Array; }; diff --git a/dist/swap/v2/index.d.ts b/dist/swap/v2/index.d.ts index 43f3f2e3..2fbfdffd 100644 --- a/dist/swap/v2/index.d.ts +++ b/dist/swap/v2/index.d.ts @@ -27,10 +27,17 @@ declare function execute({ client, quote, txGroup, signedTxns }: { * @param decimals.assetOut - Decimals quantity for the output asset * @returns A promise for the Swap quote */ -declare function getQuote(type: SwapType, pool: V2PoolInfo, asset: AssetWithIdAndAmount, decimals: { - assetIn: number; - assetOut: number; -}, network: SupportedNetwork, isSwapRouterEnabled?: boolean): Promise; +declare function getQuote({ type, pool, asset, decimals, network, isSwapRouterEnabled }: { + type: SwapType; + pool: V2PoolInfo; + asset: AssetWithIdAndAmount; + decimals: { + assetIn: number; + assetOut: number; + }; + network: SupportedNetwork; + isSwapRouterEnabled?: boolean; +}): Promise; /** * @returns A quote for a fixed input swap. Does NOT execute any transactions. */ diff --git a/dist/swap/v2/router/constants.d.ts b/dist/swap/v2/router/constants.d.ts index e17160a1..9dc40598 100644 --- a/dist/swap/v2/router/constants.d.ts +++ b/dist/swap/v2/router/constants.d.ts @@ -1,5 +1,10 @@ import { SupportedNetwork } from "../../../util/commonTypes"; +import { SwapType } from "../../constants"; export declare const SWAP_ROUTER_APP_ID: Record; +/** + * Inner txn counts according to the swap type + */ +export declare const SWAP_ROUTER_INNER_TXN_COUNT: Record; declare const TINYMAN_ANALYTICS_API_BASE_URLS: Record; -export interface FetchSwapRouteQuotesPayload { - asset_in_id: string; - asset_out_id: string; - amount: string; - swap_type: SwapType; -} - export interface SwapRouteAsset { id: string; name: string; @@ -166,6 +160,13 @@ export interface SwapRouterQuote { price_impact: number; } +export interface FetchSwapRouteQuotesPayload { + asset_in_id: string; + asset_out_id: string; + amount: string; + swap_type: SwapType; +} + export type FetchSwapRouteQuotesResponse = FetchSwapRouteQuotesPayload & { route: SwapRoute; price_impact: string; diff --git a/src/swap/utils.ts b/src/swap/utils.ts index 319b1f45..91dcf830 100644 --- a/src/swap/utils.ts +++ b/src/swap/utils.ts @@ -159,10 +159,7 @@ export async function getFixedOutputSwapQuote( } /** - * Returns the rate of a swap quote. - * - * @param quote - The quote. - * @returns The rate. + * @returns The asset amount ratio for the given quote */ function getSwapQuoteRate(quote: SwapQuote): number { if (quote.type === SwapQuoteType.Direct) { diff --git a/src/swap/v1_1/index.ts b/src/swap/v1_1/index.ts index 79fa1624..4c005e93 100644 --- a/src/swap/v1_1/index.ts +++ b/src/swap/v1_1/index.ts @@ -68,7 +68,6 @@ async function generateTxns({ initiatorAddr }: GenerateV1_1SwapTxnsParams): Promise { const {pool, quote: swapQuote} = quote; - const {assetInID, assetOutID} = swapQuote; const poolAddress = pool.account.address(); diff --git a/src/swap/v2/constants.ts b/src/swap/v2/constants.ts index 22f39e20..90a77f91 100644 --- a/src/swap/v2/constants.ts +++ b/src/swap/v2/constants.ts @@ -28,4 +28,4 @@ export const V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED = { export const V2_SWAP_ROUTER_APP_ARGS_ENCODED = { ASSET_OPT_IN: encodeString("asset_opt_in") -}; +} as const; diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index 29faf70f..48386686 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -34,6 +34,7 @@ import {AssetWithIdAndAmount} from "../../util/asset/assetModels"; import {tinymanJSSDKConfig} from "../../config"; import {CONTRACT_VERSION} from "../../contract/constants"; import {generateSwapRouterTxns, getSwapRoute} from "./router/swap-router"; +import {getAssetOutFromSwapRoute} from "./router/util"; async function generateTxns( params: GenerateSwapTxnsParams @@ -212,15 +213,21 @@ async function execute({ * @param decimals.assetOut - Decimals quantity for the output asset * @returns A promise for the Swap quote */ -// eslint-disable-next-line max-params -async function getQuote( - type: SwapType, - pool: V2PoolInfo, - asset: AssetWithIdAndAmount, - decimals: {assetIn: number; assetOut: number}, - network: SupportedNetwork, - isSwapRouterEnabled?: boolean -): Promise { +async function getQuote({ + type, + pool, + asset, + decimals, + network, + isSwapRouterEnabled +}: { + type: SwapType; + pool: V2PoolInfo; + asset: AssetWithIdAndAmount; + decimals: {assetIn: number; assetOut: number}; + network: SupportedNetwork; + isSwapRouterEnabled?: boolean; +}): Promise { let quote: SwapQuote; if (type === SwapType.FixedInput) { @@ -321,7 +328,7 @@ async function getFixedInputSwapQuote({ if ( swapRoute.route.length > 1 && - BigInt(swapRoute.route[swapRoute.route.length - 1].quote.amount_out.amount) > + BigInt(getAssetOutFromSwapRoute(swapRoute.route).amount) > directSwapQuote.assetOutAmount ) { return { @@ -418,7 +425,7 @@ async function getFixedOutputSwapQuote({ if ( swapRoute.route.length > 1 && - BigInt(swapRoute.route[swapRoute.route.length - 1].quote.amount_in.amount) < + BigInt(getAssetOutFromSwapRoute(swapRoute.route).amount) < directSwapQuote.assetInAmount ) { return { diff --git a/src/swap/v2/router/constants.ts b/src/swap/v2/router/constants.ts index 9904f2c6..9612481c 100644 --- a/src/swap/v2/router/constants.ts +++ b/src/swap/v2/router/constants.ts @@ -1,4 +1,5 @@ import {SupportedNetwork} from "../../../util/commonTypes"; +import {SwapType} from "../../constants"; export const SWAP_ROUTER_APP_ID: Record = { testnet: 159521633, @@ -6,6 +7,15 @@ export const SWAP_ROUTER_APP_ID: Record = { mainnet: 0 }; +/** + * Inner txn counts according to the swap type + */ +export const SWAP_ROUTER_INNER_TXN_COUNT: Record = { + [SwapType.FixedInput]: 7, + [SwapType.FixedOutput]: 8 + // ... +} as const; + const TINYMAN_ANALYTICS_API_BASE_URLS: Record< SupportedNetwork, {base: string; v1: string} diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index 44cc1646..67cad0a7 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -17,8 +17,12 @@ import { V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED, V2_SWAP_ROUTER_APP_ARGS_ENCODED } from "../constants"; -import {TINYMAN_ANALYTICS_API_BASE_URLS} from "./constants"; -import {getSwapRouterAppID} from "./util"; +import {SWAP_ROUTER_INNER_TXN_COUNT, TINYMAN_ANALYTICS_API_BASE_URLS} from "./constants"; +import { + getAssetInFromSwapRoute, + getAssetOutFromSwapRoute, + getSwapRouterAppID +} from "./util"; export async function generateSwapRouterAssetOptInTransaction({ client, @@ -32,7 +36,7 @@ export async function generateSwapRouterAssetOptInTransaction({ accountAddress: string; }): Promise { const suggestedParams = await client.getTransactionParams().do(); - // We need to create a NoOp transaction for each asset we want to opt-in to + // We need to create a NoOp transaction to opt-in to the assets const assetOptInTxn = algosdk.makeApplicationNoOpTxnFromObject({ from: accountAddress, appIndex: routerAppID, @@ -43,8 +47,10 @@ export async function generateSwapRouterAssetOptInTransaction({ // The number of inner transactions is the number of assets we're opting in to const innerTransactionCount = assetIDs.length; - // The fee for the transaction is the fee for the outer transaction (which is a NoOp) - // multiplied by the number of inner transactions + /** + * The opt-in transaction fee should cover the total cost of inner transactions, + * and the outer transaction (thus the +1) + */ assetOptInTxn.fee = ALGORAND_MIN_TX_FEE * (innerTransactionCount + 1); return assetOptInTxn; @@ -65,8 +71,8 @@ export async function generateSwapRouterTxns({ getAssetId(route[1].quote.amount_out.asset) ]; const [assetInAmount, assetOutAmount] = [ - Number(route[0].quote.amount_in.amount), - Number(route[route.length - 1].quote.amount_out.amount) + Number(getAssetInFromSwapRoute(route).amount), + Number(getAssetOutFromSwapRoute(route).amount) ]; const [pool1Address, pool2Address] = [route[0].pool.address, route[1].pool.address]; @@ -103,27 +109,26 @@ export async function generateSwapRouterTxns({ suggestedParams }); - const innerTransactionCount = swapType === SwapType.FixedInput ? 7 : 8; - - routerAppCallTxn.fee = ALGORAND_MIN_TX_FEE * (innerTransactionCount + 1); + routerAppCallTxn.fee = + ALGORAND_MIN_TX_FEE * (SWAP_ROUTER_INNER_TXN_COUNT[swapType] + 1); const txnList = [inputTxn, routerAppCallTxn]; - const swapRouterAppOptInRequiredAssetIDs = await getSwapRouterAppOptInRequiredAssetIDs({ + const optInRequiredAssetIDs = await getSwapRouterAppOptInRequiredAssetIDs({ client, routerAppID, assetIDs: [assetInID, intermediaryAssetID, assetOutID] }); - if (swapRouterAppOptInRequiredAssetIDs.length > 0) { - const routerAppOptInTxn = await generateSwapRouterAssetOptInTransaction({ + if (optInRequiredAssetIDs.length > 0) { + const routerAppAssetOptInTxn = await generateSwapRouterAssetOptInTransaction({ client, accountAddress: initiatorAddr, - assetIDs: swapRouterAppOptInRequiredAssetIDs, + assetIDs: optInRequiredAssetIDs, routerAppID }); - txnList.unshift(routerAppOptInTxn); + txnList.unshift(routerAppAssetOptInTxn); } const txGroup = algosdk.assignGroupID(txnList); diff --git a/src/swap/v2/router/util.ts b/src/swap/v2/router/util.ts index 7821986b..c8850c2a 100644 --- a/src/swap/v2/router/util.ts +++ b/src/swap/v2/router/util.ts @@ -1,12 +1,8 @@ import {SupportedNetwork} from "../../../util/commonTypes"; import {convertFromBaseUnits} from "../../../util/util"; -import {SwapRoute, SwapRouterQuote} from "../../types"; +import {SwapRoute} from "../../types"; import {SWAP_ROUTER_APP_ID} from "./constants"; -export function getSwapFeesFromSwapRoute(route: SwapRouterQuote[]): number { - return route.reduce((acc, quote) => acc + Number(quote.swap_fees.amount), 0); -} - export function getSwapRouteRate(route: SwapRoute) { const [assetIn, assetOut] = [ route[0].quote.amount_in, @@ -28,3 +24,11 @@ export function getSwapRouterAppID(network: SupportedNetwork) { return SWAP_ROUTER_APP_ID[network]; } + +export function getAssetOutFromSwapRoute(route: SwapRoute) { + return route[route.length - 1].quote.amount_out; +} + +export function getAssetInFromSwapRoute(route: SwapRoute) { + return route[0].quote.amount_in; +} diff --git a/src/util/asset/assetUtils.ts b/src/util/asset/assetUtils.ts index 82847e81..3362cd8a 100644 --- a/src/util/asset/assetUtils.ts +++ b/src/util/asset/assetUtils.ts @@ -75,7 +75,7 @@ export function isAlgo(id: number | string) { } /** - * Returns asset.asset_id + * @returns Asset id as a number */ export function getAssetId(asset: {id: string | number}): number { return Number(asset.id); From 0c563b5a0eb8c5c1c31073dc79ede6d5d8ac14c3 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Wed, 8 Mar 2023 14:32:54 +0300 Subject: [PATCH 16/39] fix(swap): fix swap type for getFixedOutputQuote --- dist/index.js | 2 +- src/swap/v2/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 9c5c52df..28332498 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),a=Uint8Array.from([1]);function s(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function u(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){u(s,r,a,o,i,"next",t)}function i(t){u(s,r,a,o,i,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",k="logic eval error:",P="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;ir?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function Q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=D(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function X(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=D(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var j=100000n,q=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return j+q*BigInt(o.length)+U*BigInt(s.length+a.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(nt(e),2),s=a[0],o=a[1],i=v(nt(Math.round(Number(et(s,o+r)))),2),u=i[0],c=i[1];return Number(et(u,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return at.apply(this,arguments)}function at(){return(at=c(i().mark((function t(e,n){var r,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return u=t.sent,c=u.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function st(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function it(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ut(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return ut("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht,Tt=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),yt=ut("swap"),vt=(f(xt={},exports.SwapType.FixedInput,ut("fixed-input")),f(xt,exports.SwapType.FixedOutput,ut("fixed-output")),xt),_t={ASSET_OPT_IN:ut("asset_opt_in")},wt={testnet:159521633,mainnet:0},bt=(f(ht={},exports.SwapType.FixedInput,7),f(ht,exports.SwapType.FixedOutput,8),ht),Dt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function St(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Et(t){if(!wt[t])throw new Error("Unknown network or network not supported: ".concat(t));return wt[t]}function Ot(t){return t[t.length-1].quote.amount_out}function Nt(t){return t[0].quote.amount_in}function kt(t){return Pt.apply(this,arguments)}function Pt(){return(Pt=c(i().mark((function e(n){var a,s,o,u,c,p,l;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,u=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s,appArgs:[_t.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E,O;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,u=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Nt(c).amount),Number(Ot(c).amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Et(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[yt,vt[u],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(bt[u]+1),b=[_,w],e.next=15,Rt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((D=e.sent).length>0)){e.next=21;break}return e.next=19,kt({client:s,accountAddress:a,assetIDs:D,routerAppID:v});case 19:E=e.sent,b.unshift(E);case 21:return O=r.default.assignGroupID(b),e.abrupt("return",O.map((function(t){return{txn:t,signers:[a]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Rt(t){return Bt.apply(this,arguments)}function Bt(){return(Bt=c(i().mark((function e(n){var r,a,s,o,u,c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o=e.network,u={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(Dt[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Vt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Qt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Xt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),jt=new(function(n){m(a,Xt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(i().mark((function t(a,s){var u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:u=t.sent,a(o(o({},u),{},{minimum_required_balance:Yt(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Jt(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Yt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Kt,Ht,Zt=ut("e");function $t(t){return te.apply(this,arguments)}function te(){return(te=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,u=a.accountAddr,n.next=3,s.accountInformation(u).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=Q({stateArray:A}),T=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ee(){return(ee=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,u=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==u})),d=[],l&&l["key-value"])for(f=Q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mae)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ae,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ue(){return ue=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d=arguments;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,zt(n,r);case 6:return o=t.sent,u=Jt(o,dt(a,S.V1_1)),c=null,u&&(l=o["created-assets"][0],p=l.index,c={asset1ID:u[ne[S.V1_1].asset1],asset2ID:u[ne[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ue.apply(this,arguments)}var ce=Object.freeze({__proto__:null,getPoolInfo:se,getPoolReserves:function(t,e){return ie.apply(this,arguments)},getPoolAssets:function(t){return ue.apply(this,arguments)}});function pe(t){return le.apply(this,arguments)}function le(){return(le=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Wt(S.V2),u=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=u.address(),l=M(a,s),t.next=8,zt(n,p);case 8:return d=t.sent,f=Jt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[ne.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ne.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ne.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ne.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ne.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ne.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ne.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ne.v2.totalFeeShare]),m.poolTokenID=Number(f[ne.v2.poolTokenID]),m.asset1ID=Number(f[ne.v2.asset1]),m.asset2ID=Number(f[ne.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function de(){return(de=c(i().mark((function t(e,n){var r,a,s;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,zt(e,n.account.address());case 2:return r=t.sent,a=Jt(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[ne.v2.asset1Reserves]),s.asset2=BigInt(a[ne.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[ne.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(){return(fe=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,zt(n,r);case 3:return s=t.sent,o=Jt(s,dt(a,S.V2)),u=null,o&&(u={asset1ID:o[ne[S.V2].asset1],asset2ID:o[ne[S.V2].asset2],poolTokenID:o[ne[S.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var me=Object.freeze({__proto__:null,getPoolInfo:pe,getPoolReserves:function(t,e){return de.apply(this,arguments)},getPoolAssets:function(t){return fe.apply(this,arguments)}});function ge(t){return Boolean(t&&!(t.asset1+t.asset2))}var xe,Ie,Ae=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ge(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:ge,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([se(t),pe(t)])}}),he=o((f(xe={},S.V1_1,o(o({},ce),Ae)),f(xe,S.V2,o(o({},me),Ae)),xe),Ae);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Ie||(Ie={}));var Te=96e4,ye=859e3;function ve(){return(ve=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,k;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,u=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(a,S.V1_1),T=jt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[ut("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:_e(A),suggestedParams:c}),(E=[])[Ie.FUNDING_TXN]=D,E[Ie.VALIDATOR_APP_CALL]=_,E[Ie.POOL_TOKEN_CREATE]=w,E[Ie.ASSET1_OPT_IN]=b,A||(E[Ie.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),k=[{txn:N[Ie.FUNDING_TXN],signers:[u]},{txn:N[Ie.VALIDATOR_APP_CALL],signers:[y]},{txn:N[Ie.POOL_TOKEN_CREATE],signers:[y]},{txn:N[Ie.ASSET1_OPT_IN],signers:[y]}],N[Ie.ASSET2_OPT_IN]&&k.push({txn:N[Ie.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",k);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(t){return t?Te:ye}function we(){return(we=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,u),f=v(d,2),m=f[0],g=f[1],x=jt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ie.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function be(t){return De.apply(this,arguments)}function De(){return(De=c(i().mark((function t(e){var n,r,a,s,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,a[Ie.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Se(){return(Se=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,be({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",he.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ee,Oe={generateTxns:function(t){return ve.apply(this,arguments)},signTxns:function(t){return we.apply(this,arguments)},execute:function(t){return Se.apply(this,arguments)},getBootstrapFundingTxnAmount:_e};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ee||(Ee={}));var Ne=5,ke=6;function Pe(t,e){return Le.apply(this,arguments)}function Le(){return(Le=c(i().mark((function e(n,r){var a,s,o;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fe(t,e){return Re.apply(this,arguments)}function Re(){return(Re=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Pe(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(){return(Be=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,u=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=dt(s,S.V2),d=t.getApplicationAddress(l),f=B(o,u),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,he.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(u.unit_name," already exists"));case 12:return I=Gt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[ut("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ce(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Me(h),suggestedParams:p}),(_=[])[Ee.FUNDING_TXN]=y,_[Ee.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Ee.FUNDING_TXN]={txn:w[Ee.FUNDING_TXN],signers:[c]},b[Ee.VALIDATOR_APP_CALL]={txn:w[Ee.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Me(t){return function(t,e){var n=Wt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ce(t)+1e5}function Ce(e){return((e?Ne:ke)+1)*t.ALGORAND_MIN_TX_FEE}function Ve(){return(Ve=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,u),f=v(d,2),m=f[0],g=f[1],x=Gt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ee.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Qe(){return(Qe=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Pe(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ne.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",he.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Xe,je={generateTxns:function(t){return Be.apply(this,arguments)},signTxns:function(t){return Ve.apply(this,arguments)},execute:function(t){return Qe.apply(this,arguments)},getBootstrapFundingTxnAmount:Me,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Me(e)}};var qe,Ue=(f(Xe={},S.V1_1,Oe),f(Xe,S.V2,je),f(Xe,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Oe.generateTxns(t):je.generateTxns(t)})),f(Xe,"signTxns",(function(t){return t.contractVersion===S.V1_1?Oe.signTxns(t):je.signTxns(t)})),f(Xe,"execute",(function(t){return t.contractVersion===S.V1_1?Oe.execute(t):je.execute(t)})),f(Xe,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Oe.getBootstrapFundingTxnAmount(n):je.getBootstrapFundingTxnAmount(n)})),Xe);exports.V1_1AddLiquidityTxnIndices=void 0,(qe=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[qe.FEE_TXN=0]="FEE_TXN",qe[qe.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",qe[qe.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",qe[qe.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",qe[qe.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Ge,We,ze,Je,Ye=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Je=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Je.FLEXIBLE="flexible",Je.INITIAL="initial";var Ke,He=(f(Ge={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Ge,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Ge,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Ge),Ze=(f(We={},exports.V2AddLiquidityType.INITIAL,1),f(We,exports.V2AddLiquidityType.SINGLE,2),f(We,exports.V2AddLiquidityType.FLEXIBLE,2),We),$e=(f(ze={},exports.V2AddLiquidityType.INITIAL,3),f(ze,exports.V2AddLiquidityType.FLEXIBLE,3),f(ze,exports.V2AddLiquidityType.SINGLE,2),ze);function tn(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function en(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=tn({assetIn:r,assetOut:a}),o=Z(a.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(s/o-1))}function nn(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(x>I){var A=x;r=rn(A,s),e={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=rn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:en({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function rn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function an(e){return(Ze[e]+1)*t.ALGORAND_MIN_TX_FEE}var sn=ut("add_liquidity"),on=(f(Ke={},S.V1_1,[ut("mint")]),f(Ke,S.V2,{INITIAL_LIQUIDITY:[ut("add_initial_liquidity")],SINGLE_ASSET_MODE:[sn,ut("single")],FLEXIBLE_MODE:[sn,ut("flexible")]}),Ke);function un(){return(un=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,u=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(s,S.V1_1),appArgs:on.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[u.id,p.id]:[u.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(){return(cn=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pn(){return(pn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,u=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,$t({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=st(a),x=ot(a),t.next=17,$t({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var ln=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Fn(){return(Fn=c(i().mark((function t(e){var n,a,s,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Lt(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,a=e.initiatorAddr,s=e.slippage,u=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=C(f),h=u===exports.SwapType.FixedInput?d.assetInAmount:H("positive",s,d.assetInAmount),T=u===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",s,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:l.validatorAppID,appArgs:[yt,vt[u],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Rn(u),(_=[])[It.INPUT_TXN]=y,_[It.APP_CALL_TXN]=v,w=r.default.assignGroupID(_),t.abrupt("return",[{txn:w[It.INPUT_TXN],signers:[a]},{txn:w[It.APP_CALL_TXN],signers:[a]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rn(e){return(Tt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Bn(){return(Bn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,u=e.signedTxns,t.next=3,rt(a,[u]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Fe(a,o);case 10:return m=t.sent,g=s.type===exports.SwapQuoteType.Direct?{id:s.quoteWithPool.quote.assetInID,amount:s.quoteWithPool.quote.assetInAmount}:{id:Number(s.asset_in_id),amount:Number(s.route[0].quote.amount_in.amount)},x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Mn(){return(Mn=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.pool,a=e.asset,s=e.decimals,o=e.network,u=e.isSwapRouterEnabled,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,Cn({pool:r,assetIn:a,decimals:s,isSwapRouterEnabled:u,network:o});case 4:c=t.sent,t.next=10;break;case 7:return t.next=9,Qn({pool:r,assetOut:a,decimals:s,isSwapRouterEnabled:u,network:o});case 9:c=t.sent;case 10:return t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Cn(t){return Vn.apply(this,arguments)}function Vn(){return(Vn=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,u=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 17:throw new F({pool:a,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=jn({inputSupply:d,outputSupply:f,swapInputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapOutputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(g>f)){t.next=21;break}throw new Ln;case 21:if(A={assetInID:n.id,assetInAmount:c,assetOutID:l,assetOutAmount:g,swapFee:Number(x),rate:Z(r.assetOut,Number(g))/Z(r.assetIn,Number(c)),priceImpact:I},!s){t.next=28;break}return t.next=25,Mt({amount:n.amount,assetInID:Number(n.id),assetOutID:l,swapType:exports.SwapType.FixedInput,network:u});case 25:if(!((h=t.sent).route.length>1&&BigInt(Ot(h.route).amount)>A.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},h),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:a,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Qn(t){return Xn.apply(this,arguments)}function Xn(){return(Xn=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,u=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 17:throw new F({pool:a,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=qn({inputSupply:d,outputSupply:f,swapOutputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapInputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(c>f)){t.next=21;break}throw new Ln;case 21:if(A={assetInID:l,assetInAmount:g,assetOutID:n.id,assetOutAmount:c,swapFee:Number(x),rate:Z(r.assetOut,Number(c))/Z(r.assetIn,Number(g)),priceImpact:I},!s){t.next=28;break}return t.next=25,Mt({amount:n.amount,assetInID:l,assetOutID:n.id,swapType:exports.SwapType.FixedInput,network:u});case 25:if(!((h=t.sent).route.length>1&&BigInt(Ot(h.route).amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Wn(){return(Wn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,u=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Sn,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[bn.ASSET_TRANSFER_TXN]=g,I[bn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[bn.ASSET_TRANSFER_TXN],signers:[u]},{txn:A[bn.APP_CALL_TXN],signers:[u]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zn(){return(zn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,u=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:u,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Sn,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[bn.ASSET_TRANSFER_TXN]=T,v[bn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[bn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[bn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jn(){return(Jn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,rt(r,[s]);case 3:return o=t.sent,u=v(o,1),c=u[0].txnID,t.next=8,Fe(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Yn,Kn,Hn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Gn(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=Gn(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Un.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Un.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Wn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return zn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Jn.apply(this,arguments)}},Zn=(f(Yn={},S.V1_1,Pn),f(Yn,S.V2,Hn),Yn),$n=3n,tr=1000n;function er(){return(er=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,u=v(o,2),c=u[0],p=u[1],l=a.map((function(t,e){return e===Kn.FEE_TXN_INDEX?c:e===Kn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.quote,o=e.swapType,u=e.slippage,c=e.initiatorAddr,p=s.pool,l=s.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:s},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[ut("swap"),o===exports.SwapType.FixedInput?ut("fi"):ut("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",u,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",u,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),_=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:a,suggestedParams:x}),w=r.default.assignGroupID([_,A,T,v]),t.abrupt("return",[{txn:w[0],signers:[c]},{txn:w[1],signers:[m]},{txn:w[2],signers:[c]},{txn:w[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,i,u,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*$n/tr,l=u-i*u/(i+(c-p));if(l>u)throw new Ln;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:tn(d),priceImpact:en(o({inputSupply:i,outputSupply:u},d))}}function ar(t){return sr.apply(this,arguments)}function sr(){return(sr=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new Ln;var c=o*i/(i-u)-o,p=c*tr/(tr-$n),l=p-c,d=Z(a.assetOut,Number(u))/Z(a.assetIn,Number(p)),f=Z(a.assetOut,Number(i))/Z(a.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m}}function ir(t){return ur.apply(this,arguments)}function ur(){return(ur=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cr(){return(cr=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Kn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Kn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Kn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Kn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,ar({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,ir({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(s),fees:st(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Kn||(Kn={}));var pr,lr={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?rr({pool:e,reserves:n,assetIn:r,decimals:a}):or({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:rr,getFixedOutputSwapQuote:or,generateTxns:function(t){return nr.apply(this,arguments)},signTxns:function(t){return er.apply(this,arguments)},execute:function(t){return cr.apply(this,arguments)},executeFixedOutputSwap:ir},dr=4*t.ALGORAND_MIN_TX_FEE;function fr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Ln})))throw new Ln;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function mr(){return mr=c(i().mark((function t(e){var n,r,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,u=s.map((function(t){return new Promise(function(){var s=c(i().mark((function s(u,c){var p,l;return i().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:lr.getFixedInputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Un.getFixedInputSwapQuote(l);case 8:p=s.sent;case 9:u(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,fr(u);case 4:return p=t.sent,t.abrupt("return",Ir(p));case 6:case"end":return t.stop()}}),t)}))),mr.apply(this,arguments)}function gr(){return gr=c(i().mark((function t(e){var n,r,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,u=s.map((function(t){return new Promise(function(){var s=c(i().mark((function s(u,c){var p,l;return i().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:lr.getFixedOutputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Un.getFixedOutputSwapQuote(l);case 8:p=s.sent;case 9:u(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,fr(u);case 4:return p=t.sent,t.abrupt("return",Ir(p));case 6:case"end":return t.stop()}}),t)}))),gr.apply(this,arguments)}function xr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:St(t.route)}function Ir(t){var e,n=t[0],r=xr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=xr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}function Ar(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var hr=(f(pr={},S.V1_1,lr),f(pr,S.V2,Un),f(pr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ge(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return mr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return gr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(pr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&Ar(t.quote)===S.V1_1?lr.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):Un.generateTxns(t)})),f(pr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&Ar(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return lr.signTxns(o({pool:e},t))}return Un.signTxns(t)})),f(pr,"execute",(function(t){return t.contractVersion===S.V1_1?lr.execute(t):Un.execute(t)})),pr);function Tr(){return(Tr=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,yr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return u=t.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:st(a),confirmedRound:d,txnID:l,groupID:ot(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function yr(t){return vr.apply(this,arguments)}function vr(){return(vr=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,u=v(o,1),c=u[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _r(){return _r=c(i().mark((function t(e){var n,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:st(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return u=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(i().mark((function a(s,o){var c,p,l,d,f,m;return i().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?u[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,rt(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),_r.apply(this,arguments)}function wr(){return(wr=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,u=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[ut("redeem")],note:pt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(u),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(u),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ut("commit"),u],note:X([ut("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Dr(){return(Dr=c(i().mark((function e(n){var r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=br({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[ut("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=wn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Ue,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Zn,exports.Swap=hr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Dn,exports.V2AddLiquidityTxnIndices=He,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=Yt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=he,exports.prepareCommitTransactions=function(t){return Dr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return _r.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Tr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=st,exports.tinymanContract_v1_1=jt,exports.tinymanContract_v2=Gt,exports.tinymanJSSDKConfig=pt; +i=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",s=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function u(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,s=Object.create(a.prototype),o=new w(r||[]);return s._invoke=function(t,e,n){var r="suspendedStart";return function(a,s){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw s;return D()}for(n.method=a,n.arg=s;;){var o=n.delegate;if(o){var i=y(o,n);if(i){if(i===l)continue;return i}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var u=p(t,e,n);if("normal"===u.type){if(r=n.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:n.done}}"throw"===u.type&&(r="completed",n.method="throw",n.arg=u.arg)}}}(t,n,o),s}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};u(g,a,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,a)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,s,o,i){var u=p(t[a],t,s);if("throw"!==u.type){var c=u.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,i)}),(function(t){r("throw",t,o,i)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,i)}))}i(u.arg)}var a;this._invoke=function(t,n){function s(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(s,s):s()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function w(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,s=function e(){for(;++r=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function u(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){u(s,r,a,o,i,"next",t)}function i(t){u(s,r,a,o,i,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",k="logic eval error:",P="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;ir?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function Q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=D(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function X(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=D(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var j=100000n,q=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return j+q*BigInt(o.length)+U*BigInt(s.length+a.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(nt(e),2),s=a[0],o=a[1],i=v(nt(Math.round(Number(et(s,o+r)))),2),u=i[0],c=i[1];return Number(et(u,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return at.apply(this,arguments)}function at(){return(at=c(i().mark((function t(e,n){var r,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return u=t.sent,c=u.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function st(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function it(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ut(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return ut("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht,Tt=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),yt=ut("swap"),vt=(f(xt={},exports.SwapType.FixedInput,ut("fixed-input")),f(xt,exports.SwapType.FixedOutput,ut("fixed-output")),xt),_t={ASSET_OPT_IN:ut("asset_opt_in")},wt={testnet:159521633,mainnet:0},bt=(f(ht={},exports.SwapType.FixedInput,7),f(ht,exports.SwapType.FixedOutput,8),ht),Dt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function St(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Et(t){if(!wt[t])throw new Error("Unknown network or network not supported: ".concat(t));return wt[t]}function Ot(t){return t[t.length-1].quote.amount_out}function Nt(t){return t[0].quote.amount_in}function kt(t){return Pt.apply(this,arguments)}function Pt(){return(Pt=c(i().mark((function e(n){var a,s,o,u,c,p,l;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,u=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s,appArgs:[_t.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E,O;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,u=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Nt(c).amount),Number(Ot(c).amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Et(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[yt,vt[u],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(bt[u]+1),b=[_,w],e.next=15,Rt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((D=e.sent).length>0)){e.next=21;break}return e.next=19,kt({client:s,accountAddress:a,assetIDs:D,routerAppID:v});case 19:E=e.sent,b.unshift(E);case 21:return O=r.default.assignGroupID(b),e.abrupt("return",O.map((function(t){return{txn:t,signers:[a]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Rt(t){return Bt.apply(this,arguments)}function Bt(){return(Bt=c(i().mark((function e(n){var r,a,s,o,u,c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o=e.network,u={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(Dt[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Vt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Qt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Xt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),jt=new(function(n){m(a,Xt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(i().mark((function t(a,s){var u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:u=t.sent,a(o(o({},u),{},{minimum_required_balance:Yt(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Jt(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Yt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Kt,Ht,Zt=ut("e");function $t(t){return te.apply(this,arguments)}function te(){return(te=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,u=a.accountAddr,n.next=3,s.accountInformation(u).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=Q({stateArray:A}),T=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ee(){return(ee=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,u=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==u})),d=[],l&&l["key-value"])for(f=Q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mae)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ae,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ue(){return ue=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d=arguments;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,zt(n,r);case 6:return o=t.sent,u=Jt(o,dt(a,S.V1_1)),c=null,u&&(l=o["created-assets"][0],p=l.index,c={asset1ID:u[ne[S.V1_1].asset1],asset2ID:u[ne[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ue.apply(this,arguments)}var ce=Object.freeze({__proto__:null,getPoolInfo:se,getPoolReserves:function(t,e){return ie.apply(this,arguments)},getPoolAssets:function(t){return ue.apply(this,arguments)}});function pe(t){return le.apply(this,arguments)}function le(){return(le=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Wt(S.V2),u=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=u.address(),l=M(a,s),t.next=8,zt(n,p);case 8:return d=t.sent,f=Jt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[ne.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ne.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ne.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ne.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ne.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ne.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ne.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ne.v2.totalFeeShare]),m.poolTokenID=Number(f[ne.v2.poolTokenID]),m.asset1ID=Number(f[ne.v2.asset1]),m.asset2ID=Number(f[ne.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function de(){return(de=c(i().mark((function t(e,n){var r,a,s;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,zt(e,n.account.address());case 2:return r=t.sent,a=Jt(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[ne.v2.asset1Reserves]),s.asset2=BigInt(a[ne.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[ne.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(){return(fe=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,zt(n,r);case 3:return s=t.sent,o=Jt(s,dt(a,S.V2)),u=null,o&&(u={asset1ID:o[ne[S.V2].asset1],asset2ID:o[ne[S.V2].asset2],poolTokenID:o[ne[S.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var me=Object.freeze({__proto__:null,getPoolInfo:pe,getPoolReserves:function(t,e){return de.apply(this,arguments)},getPoolAssets:function(t){return fe.apply(this,arguments)}});function ge(t){return Boolean(t&&!(t.asset1+t.asset2))}var xe,Ie,Ae=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ge(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:ge,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([se(t),pe(t)])}}),he=o((f(xe={},S.V1_1,o(o({},ce),Ae)),f(xe,S.V2,o(o({},me),Ae)),xe),Ae);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Ie||(Ie={}));var Te=96e4,ye=859e3;function ve(){return(ve=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,k;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,u=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(a,S.V1_1),T=jt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[ut("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:_e(A),suggestedParams:c}),(E=[])[Ie.FUNDING_TXN]=D,E[Ie.VALIDATOR_APP_CALL]=_,E[Ie.POOL_TOKEN_CREATE]=w,E[Ie.ASSET1_OPT_IN]=b,A||(E[Ie.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),k=[{txn:N[Ie.FUNDING_TXN],signers:[u]},{txn:N[Ie.VALIDATOR_APP_CALL],signers:[y]},{txn:N[Ie.POOL_TOKEN_CREATE],signers:[y]},{txn:N[Ie.ASSET1_OPT_IN],signers:[y]}],N[Ie.ASSET2_OPT_IN]&&k.push({txn:N[Ie.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",k);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(t){return t?Te:ye}function we(){return(we=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,u),f=v(d,2),m=f[0],g=f[1],x=jt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ie.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function be(t){return De.apply(this,arguments)}function De(){return(De=c(i().mark((function t(e){var n,r,a,s,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,a[Ie.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Se(){return(Se=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,be({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",he.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ee,Oe={generateTxns:function(t){return ve.apply(this,arguments)},signTxns:function(t){return we.apply(this,arguments)},execute:function(t){return Se.apply(this,arguments)},getBootstrapFundingTxnAmount:_e};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ee||(Ee={}));var Ne=5,ke=6;function Pe(t,e){return Le.apply(this,arguments)}function Le(){return(Le=c(i().mark((function e(n,r){var a,s,o;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fe(t,e){return Re.apply(this,arguments)}function Re(){return(Re=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Pe(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(){return(Be=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,u=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=dt(s,S.V2),d=t.getApplicationAddress(l),f=B(o,u),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,he.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(u.unit_name," already exists"));case 12:return I=Gt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[ut("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ce(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Me(h),suggestedParams:p}),(_=[])[Ee.FUNDING_TXN]=y,_[Ee.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Ee.FUNDING_TXN]={txn:w[Ee.FUNDING_TXN],signers:[c]},b[Ee.VALIDATOR_APP_CALL]={txn:w[Ee.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Me(t){return function(t,e){var n=Wt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ce(t)+1e5}function Ce(e){return((e?Ne:ke)+1)*t.ALGORAND_MIN_TX_FEE}function Ve(){return(Ve=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,u),f=v(d,2),m=f[0],g=f[1],x=Gt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ee.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Qe(){return(Qe=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Pe(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ne.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",he.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Xe,je={generateTxns:function(t){return Be.apply(this,arguments)},signTxns:function(t){return Ve.apply(this,arguments)},execute:function(t){return Qe.apply(this,arguments)},getBootstrapFundingTxnAmount:Me,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Me(e)}};var qe,Ue=(f(Xe={},S.V1_1,Oe),f(Xe,S.V2,je),f(Xe,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Oe.generateTxns(t):je.generateTxns(t)})),f(Xe,"signTxns",(function(t){return t.contractVersion===S.V1_1?Oe.signTxns(t):je.signTxns(t)})),f(Xe,"execute",(function(t){return t.contractVersion===S.V1_1?Oe.execute(t):je.execute(t)})),f(Xe,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Oe.getBootstrapFundingTxnAmount(n):je.getBootstrapFundingTxnAmount(n)})),Xe);exports.V1_1AddLiquidityTxnIndices=void 0,(qe=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[qe.FEE_TXN=0]="FEE_TXN",qe[qe.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",qe[qe.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",qe[qe.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",qe[qe.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Ge,We,ze,Je,Ye=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Je=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Je.FLEXIBLE="flexible",Je.INITIAL="initial";var Ke,He=(f(Ge={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Ge,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Ge,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Ge),Ze=(f(We={},exports.V2AddLiquidityType.INITIAL,1),f(We,exports.V2AddLiquidityType.SINGLE,2),f(We,exports.V2AddLiquidityType.FLEXIBLE,2),We),$e=(f(ze={},exports.V2AddLiquidityType.INITIAL,3),f(ze,exports.V2AddLiquidityType.FLEXIBLE,3),f(ze,exports.V2AddLiquidityType.SINGLE,2),ze);function tn(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function en(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=tn({assetIn:r,assetOut:a}),o=Z(a.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(s/o-1))}function nn(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(x>I){var A=x;r=rn(A,s),e={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=rn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:en({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function rn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function an(e){return(Ze[e]+1)*t.ALGORAND_MIN_TX_FEE}var sn=ut("add_liquidity"),on=(f(Ke={},S.V1_1,[ut("mint")]),f(Ke,S.V2,{INITIAL_LIQUIDITY:[ut("add_initial_liquidity")],SINGLE_ASSET_MODE:[sn,ut("single")],FLEXIBLE_MODE:[sn,ut("flexible")]}),Ke);function un(){return(un=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,u=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(s,S.V1_1),appArgs:on.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[u.id,p.id]:[u.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(){return(cn=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pn(){return(pn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,u=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,$t({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=st(a),x=ot(a),t.next=17,$t({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var ln=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Fn(){return(Fn=c(i().mark((function t(e){var n,a,s,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Lt(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,a=e.initiatorAddr,s=e.slippage,u=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=C(f),h=u===exports.SwapType.FixedInput?d.assetInAmount:H("positive",s,d.assetInAmount),T=u===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",s,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:l.validatorAppID,appArgs:[yt,vt[u],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Rn(u),(_=[])[It.INPUT_TXN]=y,_[It.APP_CALL_TXN]=v,w=r.default.assignGroupID(_),t.abrupt("return",[{txn:w[It.INPUT_TXN],signers:[a]},{txn:w[It.APP_CALL_TXN],signers:[a]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rn(e){return(Tt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Bn(){return(Bn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,u=e.signedTxns,t.next=3,rt(a,[u]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Fe(a,o);case 10:return m=t.sent,g=s.type===exports.SwapQuoteType.Direct?{id:s.quoteWithPool.quote.assetInID,amount:s.quoteWithPool.quote.assetInAmount}:{id:Number(s.asset_in_id),amount:Number(s.route[0].quote.amount_in.amount)},x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Mn(){return(Mn=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.pool,a=e.asset,s=e.decimals,o=e.network,u=e.isSwapRouterEnabled,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,Cn({pool:r,assetIn:a,decimals:s,isSwapRouterEnabled:u,network:o});case 4:c=t.sent,t.next=10;break;case 7:return t.next=9,Qn({pool:r,assetOut:a,decimals:s,isSwapRouterEnabled:u,network:o});case 9:c=t.sent;case 10:return t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Cn(t){return Vn.apply(this,arguments)}function Vn(){return(Vn=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,u=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 17:throw new F({pool:a,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=jn({inputSupply:d,outputSupply:f,swapInputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapOutputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(g>f)){t.next=21;break}throw new Ln;case 21:if(A={assetInID:n.id,assetInAmount:c,assetOutID:l,assetOutAmount:g,swapFee:Number(x),rate:Z(r.assetOut,Number(g))/Z(r.assetIn,Number(c)),priceImpact:I},!s){t.next=28;break}return t.next=25,Mt({amount:n.amount,assetInID:Number(n.id),assetOutID:l,swapType:exports.SwapType.FixedInput,network:u});case 25:if(!((h=t.sent).route.length>1&&BigInt(Ot(h.route).amount)>A.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},h),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:a,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Qn(t){return Xn.apply(this,arguments)}function Xn(){return(Xn=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,u=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 17:throw new F({pool:a,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=qn({inputSupply:d,outputSupply:f,swapOutputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapInputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(c>f)){t.next=21;break}throw new Ln;case 21:if(A={assetInID:l,assetInAmount:g,assetOutID:n.id,assetOutAmount:c,swapFee:Number(x),rate:Z(r.assetOut,Number(c))/Z(r.assetIn,Number(g)),priceImpact:I},!s){t.next=28;break}return t.next=25,Mt({amount:n.amount,assetInID:l,assetOutID:n.id,swapType:exports.SwapType.FixedOutput,network:u});case 25:if(!((h=t.sent).route.length>1&&BigInt(Ot(h.route).amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Wn(){return(Wn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,u=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Sn,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[bn.ASSET_TRANSFER_TXN]=g,I[bn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[bn.ASSET_TRANSFER_TXN],signers:[u]},{txn:A[bn.APP_CALL_TXN],signers:[u]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zn(){return(zn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,u=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:u,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Sn,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[bn.ASSET_TRANSFER_TXN]=T,v[bn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[bn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[bn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jn(){return(Jn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,rt(r,[s]);case 3:return o=t.sent,u=v(o,1),c=u[0].txnID,t.next=8,Fe(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Yn,Kn,Hn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Gn(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=Gn(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Un.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Un.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Wn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return zn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Jn.apply(this,arguments)}},Zn=(f(Yn={},S.V1_1,Pn),f(Yn,S.V2,Hn),Yn),$n=3n,tr=1000n;function er(){return(er=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,u=v(o,2),c=u[0],p=u[1],l=a.map((function(t,e){return e===Kn.FEE_TXN_INDEX?c:e===Kn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.quote,o=e.swapType,u=e.slippage,c=e.initiatorAddr,p=s.pool,l=s.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:s},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[ut("swap"),o===exports.SwapType.FixedInput?ut("fi"):ut("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",u,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",u,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),_=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:a,suggestedParams:x}),w=r.default.assignGroupID([_,A,T,v]),t.abrupt("return",[{txn:w[0],signers:[c]},{txn:w[1],signers:[m]},{txn:w[2],signers:[c]},{txn:w[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,i,u,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*$n/tr,l=u-i*u/(i+(c-p));if(l>u)throw new Ln;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:tn(d),priceImpact:en(o({inputSupply:i,outputSupply:u},d))}}function ar(t){return sr.apply(this,arguments)}function sr(){return(sr=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new Ln;var c=o*i/(i-u)-o,p=c*tr/(tr-$n),l=p-c,d=Z(a.assetOut,Number(u))/Z(a.assetIn,Number(p)),f=Z(a.assetOut,Number(i))/Z(a.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m}}function ir(t){return ur.apply(this,arguments)}function ur(){return(ur=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cr(){return(cr=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Kn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Kn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Kn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Kn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,ar({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,ir({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(s),fees:st(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Kn||(Kn={}));var pr,lr={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?rr({pool:e,reserves:n,assetIn:r,decimals:a}):or({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:rr,getFixedOutputSwapQuote:or,generateTxns:function(t){return nr.apply(this,arguments)},signTxns:function(t){return er.apply(this,arguments)},execute:function(t){return cr.apply(this,arguments)},executeFixedOutputSwap:ir},dr=4*t.ALGORAND_MIN_TX_FEE;function fr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Ln})))throw new Ln;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function mr(){return mr=c(i().mark((function t(e){var n,r,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,u=s.map((function(t){return new Promise(function(){var s=c(i().mark((function s(u,c){var p,l;return i().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:lr.getFixedInputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Un.getFixedInputSwapQuote(l);case 8:p=s.sent;case 9:u(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,fr(u);case 4:return p=t.sent,t.abrupt("return",Ir(p));case 6:case"end":return t.stop()}}),t)}))),mr.apply(this,arguments)}function gr(){return gr=c(i().mark((function t(e){var n,r,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,u=s.map((function(t){return new Promise(function(){var s=c(i().mark((function s(u,c){var p,l;return i().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:lr.getFixedOutputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Un.getFixedOutputSwapQuote(l);case 8:p=s.sent;case 9:u(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,fr(u);case 4:return p=t.sent,t.abrupt("return",Ir(p));case 6:case"end":return t.stop()}}),t)}))),gr.apply(this,arguments)}function xr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:St(t.route)}function Ir(t){var e,n=t[0],r=xr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=xr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}function Ar(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var hr=(f(pr={},S.V1_1,lr),f(pr,S.V2,Un),f(pr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ge(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return mr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return gr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(pr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&Ar(t.quote)===S.V1_1?lr.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):Un.generateTxns(t)})),f(pr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&Ar(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return lr.signTxns(o({pool:e},t))}return Un.signTxns(t)})),f(pr,"execute",(function(t){return t.contractVersion===S.V1_1?lr.execute(t):Un.execute(t)})),pr);function Tr(){return(Tr=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,yr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return u=t.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:st(a),confirmedRound:d,txnID:l,groupID:ot(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function yr(t){return vr.apply(this,arguments)}function vr(){return(vr=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,u=v(o,1),c=u[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _r(){return _r=c(i().mark((function t(e){var n,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:st(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return u=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(i().mark((function a(s,o){var c,p,l,d,f,m;return i().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?u[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,rt(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),_r.apply(this,arguments)}function wr(){return(wr=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,u=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[ut("redeem")],note:pt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(u),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(u),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ut("commit"),u],note:X([ut("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Dr(){return(Dr=c(i().mark((function e(n){var r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=br({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[ut("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=wn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Ue,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Zn,exports.Swap=hr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Dn,exports.V2AddLiquidityTxnIndices=He,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=Yt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=he,exports.prepareCommitTransactions=function(t){return Dr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return _r.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Tr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=st,exports.tinymanContract_v1_1=jt,exports.tinymanContract_v2=Gt,exports.tinymanJSSDKConfig=pt; diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index 48386686..29dc6c3c 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -419,7 +419,7 @@ async function getFixedOutputSwapQuote({ amount: assetOut.amount, assetInID, assetOutID: assetOut.id, - swapType: SwapType.FixedInput, + swapType: SwapType.FixedOutput, network }); From f0ed07e07c7a78fa7340c9ba96ed99c25f0aa108 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Thu, 9 Mar 2023 15:57:17 +0300 Subject: [PATCH 17/39] feat(swap): make swap getQuote functions return type consistent, add utils for getting assets from swap quote and swap route, add util for getting price impact from swap quote --- dist/index.js | 4 +-- dist/swap/common/utils.d.ts | 12 +++++-- dist/swap/index.d.ts | 6 ++-- dist/swap/v1_1/index.d.ts | 8 ++--- dist/swap/v2/router/util.d.ts | 19 +++++++--- src/swap/common/utils.ts | 65 +++++++++++++++++++++++++++++++++-- src/swap/utils.ts | 16 ++------- src/swap/v1_1/index.ts | 36 +++++++++++++++---- src/swap/v2/router/util.ts | 28 ++++++++++----- 9 files changed, 148 insertions(+), 46 deletions(-) diff --git a/dist/index.js b/dist/index.js index 28332498..d8302698 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),a=Uint8Array.from([1]);function s(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;_(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function u(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){u(s,r,a,o,i,"next",t)}function i(t){u(s,r,a,o,i,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",k="logic eval error:",P="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;ir?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function Q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=D(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function X(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=D(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var j=100000n,q=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return j+q*BigInt(o.length)+U*BigInt(s.length+a.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(nt(e),2),s=a[0],o=a[1],i=v(nt(Math.round(Number(et(s,o+r)))),2),u=i[0],c=i[1];return Number(et(u,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return at.apply(this,arguments)}function at(){return(at=c(i().mark((function t(e,n){var r,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return u=t.sent,c=u.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function st(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function it(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ut(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return ut("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(i().mark((function t(e){var n,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht,Tt=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),yt=ut("swap"),vt=(f(xt={},exports.SwapType.FixedInput,ut("fixed-input")),f(xt,exports.SwapType.FixedOutput,ut("fixed-output")),xt),_t={ASSET_OPT_IN:ut("asset_opt_in")},wt={testnet:159521633,mainnet:0},bt=(f(ht={},exports.SwapType.FixedInput,7),f(ht,exports.SwapType.FixedOutput,8),ht),Dt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function St(t){var e=[t[0].quote.amount_in,t[t.length-1].quote.amount_out],n=e[0],r=e[1];return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Et(t){if(!wt[t])throw new Error("Unknown network or network not supported: ".concat(t));return wt[t]}function Ot(t){return t[t.length-1].quote.amount_out}function Nt(t){return t[0].quote.amount_in}function kt(t){return Pt.apply(this,arguments)}function Pt(){return(Pt=c(i().mark((function e(n){var a,s,o,u,c,p,l;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,u=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s,appArgs:[_t.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E,O;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,u=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Nt(c).amount),Number(Ot(c).amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Et(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(w=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[yt,vt[u],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(bt[u]+1),b=[_,w],e.next=15,Rt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((D=e.sent).length>0)){e.next=21;break}return e.next=19,kt({client:s,accountAddress:a,assetIDs:D,routerAppID:v});case 19:E=e.sent,b.unshift(E);case 21:return O=r.default.assignGroupID(b),e.abrupt("return",O.map((function(t){return{txn:t,signers:[a]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Rt(t){return Bt.apply(this,arguments)}function Bt(){return(Bt=c(i().mark((function e(n){var r,a,s,o,u,c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o=e.network,u={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(Dt[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Vt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Qt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Xt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),jt=new(function(n){m(a,Xt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(i().mark((function t(a,s){var u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:u=t.sent,a(o(o({},u),{},{minimum_required_balance:Yt(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Jt(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Yt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Kt,Ht,Zt=ut("e");function $t(t){return te.apply(this,arguments)}function te(){return(te=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,u=a.accountAddr,n.next=3,s.accountInformation(u).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=Q({stateArray:A}),T=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,Zt,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ee(){return(ee=c(i().mark((function n(a){var s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,u=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==u})),d=[],l&&l["key-value"])for(f=Q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mae)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ae,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ue(){return ue=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d=arguments;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,zt(n,r);case 6:return o=t.sent,u=Jt(o,dt(a,S.V1_1)),c=null,u&&(l=o["created-assets"][0],p=l.index,c={asset1ID:u[ne[S.V1_1].asset1],asset2ID:u[ne[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ue.apply(this,arguments)}var ce=Object.freeze({__proto__:null,getPoolInfo:se,getPoolReserves:function(t,e){return ie.apply(this,arguments)},getPoolAssets:function(t){return ue.apply(this,arguments)}});function pe(t){return le.apply(this,arguments)}function le(){return(le=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=Wt(S.V2),u=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=u.address(),l=M(a,s),t.next=8,zt(n,p);case 8:return d=t.sent,f=Jt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[ne.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ne.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ne.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ne.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ne.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ne.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ne.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ne.v2.totalFeeShare]),m.poolTokenID=Number(f[ne.v2.poolTokenID]),m.asset1ID=Number(f[ne.v2.asset1]),m.asset2ID=Number(f[ne.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function de(){return(de=c(i().mark((function t(e,n){var r,a,s;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,zt(e,n.account.address());case 2:return r=t.sent,a=Jt(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[ne.v2.asset1Reserves]),s.asset2=BigInt(a[ne.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[ne.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(){return(fe=c(i().mark((function t(e){var n,r,a,s,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,zt(n,r);case 3:return s=t.sent,o=Jt(s,dt(a,S.V2)),u=null,o&&(u={asset1ID:o[ne[S.V2].asset1],asset2ID:o[ne[S.V2].asset2],poolTokenID:o[ne[S.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var me=Object.freeze({__proto__:null,getPoolInfo:pe,getPoolReserves:function(t,e){return de.apply(this,arguments)},getPoolAssets:function(t){return fe.apply(this,arguments)}});function ge(t){return Boolean(t&&!(t.asset1+t.asset2))}var xe,Ie,Ae=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ge(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:ge,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([se(t),pe(t)])}}),he=o((f(xe={},S.V1_1,o(o({},ce),Ae)),f(xe,S.V2,o(o({},me),Ae)),xe),Ae);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Ie||(Ie={}));var Te=96e4,ye=859e3;function ve(){return(ve=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,k;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,u=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(a,S.V1_1),T=jt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[ut("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:_e(A),suggestedParams:c}),(E=[])[Ie.FUNDING_TXN]=D,E[Ie.VALIDATOR_APP_CALL]=_,E[Ie.POOL_TOKEN_CREATE]=w,E[Ie.ASSET1_OPT_IN]=b,A||(E[Ie.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),k=[{txn:N[Ie.FUNDING_TXN],signers:[u]},{txn:N[Ie.VALIDATOR_APP_CALL],signers:[y]},{txn:N[Ie.POOL_TOKEN_CREATE],signers:[y]},{txn:N[Ie.ASSET1_OPT_IN],signers:[y]}],N[Ie.ASSET2_OPT_IN]&&k.push({txn:N[Ie.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",k);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(t){return t?Te:ye}function we(){return(we=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,u),f=v(d,2),m=f[0],g=f[1],x=jt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ie.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function be(t){return De.apply(this,arguments)}function De(){return(De=c(i().mark((function t(e){var n,r,a,s,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,a[Ie.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Se(){return(Se=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,be({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",he.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ee,Oe={generateTxns:function(t){return ve.apply(this,arguments)},signTxns:function(t){return we.apply(this,arguments)},execute:function(t){return Se.apply(this,arguments)},getBootstrapFundingTxnAmount:_e};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ee||(Ee={}));var Ne=5,ke=6;function Pe(t,e){return Le.apply(this,arguments)}function Le(){return(Le=c(i().mark((function e(n,r){var a,s,o;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fe(t,e){return Re.apply(this,arguments)}function Re(){return(Re=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Pe(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(){return(Be=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,u=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=dt(s,S.V2),d=t.getApplicationAddress(l),f=B(o,u),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,he.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(u.unit_name," already exists"));case 12:return I=Gt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[ut("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ce(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Me(h),suggestedParams:p}),(_=[])[Ee.FUNDING_TXN]=y,_[Ee.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Ee.FUNDING_TXN]={txn:w[Ee.FUNDING_TXN],signers:[c]},b[Ee.VALIDATOR_APP_CALL]={txn:w[Ee.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Me(t){return function(t,e){var n=Wt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ce(t)+1e5}function Ce(e){return((e?Ne:ke)+1)*t.ALGORAND_MIN_TX_FEE}function Ve(){return(Ve=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,u),f=v(d,2),m=f[0],g=f[1],x=Gt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ee.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Qe(){return(Qe=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Pe(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ne.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",he.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Xe,je={generateTxns:function(t){return Be.apply(this,arguments)},signTxns:function(t){return Ve.apply(this,arguments)},execute:function(t){return Qe.apply(this,arguments)},getBootstrapFundingTxnAmount:Me,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Me(e)}};var qe,Ue=(f(Xe={},S.V1_1,Oe),f(Xe,S.V2,je),f(Xe,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Oe.generateTxns(t):je.generateTxns(t)})),f(Xe,"signTxns",(function(t){return t.contractVersion===S.V1_1?Oe.signTxns(t):je.signTxns(t)})),f(Xe,"execute",(function(t){return t.contractVersion===S.V1_1?Oe.execute(t):je.execute(t)})),f(Xe,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Oe.getBootstrapFundingTxnAmount(n):je.getBootstrapFundingTxnAmount(n)})),Xe);exports.V1_1AddLiquidityTxnIndices=void 0,(qe=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[qe.FEE_TXN=0]="FEE_TXN",qe[qe.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",qe[qe.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",qe[qe.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",qe[qe.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Ge,We,ze,Je,Ye=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Je=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Je.FLEXIBLE="flexible",Je.INITIAL="initial";var Ke,He=(f(Ge={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Ge,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Ge,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Ge),Ze=(f(We={},exports.V2AddLiquidityType.INITIAL,1),f(We,exports.V2AddLiquidityType.SINGLE,2),f(We,exports.V2AddLiquidityType.FLEXIBLE,2),We),$e=(f(ze={},exports.V2AddLiquidityType.INITIAL,3),f(ze,exports.V2AddLiquidityType.FLEXIBLE,3),f(ze,exports.V2AddLiquidityType.SINGLE,2),ze);function tn(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function en(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=tn({assetIn:r,assetOut:a}),o=Z(a.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(s/o-1))}function nn(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(x>I){var A=x;r=rn(A,s),e={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=rn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:en({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function rn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function an(e){return(Ze[e]+1)*t.ALGORAND_MIN_TX_FEE}var sn=ut("add_liquidity"),on=(f(Ke={},S.V1_1,[ut("mint")]),f(Ke,S.V2,{INITIAL_LIQUIDITY:[ut("add_initial_liquidity")],SINGLE_ASSET_MODE:[sn,ut("single")],FLEXIBLE_MODE:[sn,ut("flexible")]}),Ke);function un(){return(un=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,u=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(s,S.V1_1),appArgs:on.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[u.id,p.id]:[u.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(){return(cn=c(i().mark((function t(e){var n,a,s,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pn(){return(pn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,u=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,$t({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=st(a),x=ot(a),t.next=17,$t({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var ln=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Fn(){return(Fn=c(i().mark((function t(e){var n,a,s,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Lt(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,a=e.initiatorAddr,s=e.slippage,u=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=C(f),h=u===exports.SwapType.FixedInput?d.assetInAmount:H("positive",s,d.assetInAmount),T=u===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",s,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:l.validatorAppID,appArgs:[yt,vt[u],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Rn(u),(_=[])[It.INPUT_TXN]=y,_[It.APP_CALL_TXN]=v,w=r.default.assignGroupID(_),t.abrupt("return",[{txn:w[It.INPUT_TXN],signers:[a]},{txn:w[It.APP_CALL_TXN],signers:[a]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rn(e){return(Tt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Bn(){return(Bn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,u=e.signedTxns,t.next=3,rt(a,[u]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Fe(a,o);case 10:return m=t.sent,g=s.type===exports.SwapQuoteType.Direct?{id:s.quoteWithPool.quote.assetInID,amount:s.quoteWithPool.quote.assetInAmount}:{id:Number(s.asset_in_id),amount:Number(s.route[0].quote.amount_in.amount)},x=s.type===exports.SwapQuoteType.Direct?s.quoteWithPool.quote.assetOutID:Number(s.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Mn(){return(Mn=c(i().mark((function t(e){var n,r,a,s,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.pool,a=e.asset,s=e.decimals,o=e.network,u=e.isSwapRouterEnabled,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,Cn({pool:r,assetIn:a,decimals:s,isSwapRouterEnabled:u,network:o});case 4:c=t.sent,t.next=10;break;case 7:return t.next=9,Qn({pool:r,assetOut:a,decimals:s,isSwapRouterEnabled:u,network:o});case 9:c=t.sent;case 10:return t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Cn(t){return Vn.apply(this,arguments)}function Vn(){return(Vn=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,u=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 17:throw new F({pool:a,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=jn({inputSupply:d,outputSupply:f,swapInputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapOutputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(g>f)){t.next=21;break}throw new Ln;case 21:if(A={assetInID:n.id,assetInAmount:c,assetOutID:l,assetOutAmount:g,swapFee:Number(x),rate:Z(r.assetOut,Number(g))/Z(r.assetIn,Number(c)),priceImpact:I},!s){t.next=28;break}return t.next=25,Mt({amount:n.amount,assetInID:Number(n.id),assetOutID:l,swapType:exports.SwapType.FixedInput,network:u});case 25:if(!((h=t.sent).route.length>1&&BigInt(Ot(h.route).amount)>A.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},h),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:a,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Qn(t){return Xn.apply(this,arguments)}function Xn(){return(Xn=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,u=e.network,a.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:a,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}l=a.asset2ID,d=a.asset2Reserves,f=a.asset1Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}l=a.asset1ID,d=a.asset1Reserves,f=a.asset2Reserves,t.next=18;break;case 17:throw new F({pool:a,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(m=qn({inputSupply:d,outputSupply:f,swapOutputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapInputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(c>f)){t.next=21;break}throw new Ln;case 21:if(A={assetInID:l,assetInAmount:g,assetOutID:n.id,assetOutAmount:c,swapFee:Number(x),rate:Z(r.assetOut,Number(c))/Z(r.assetIn,Number(g)),priceImpact:I},!s){t.next=28;break}return t.next=25,Mt({amount:n.amount,assetInID:l,assetOutID:n.id,swapType:exports.SwapType.FixedOutput,network:u});case 25:if(!((h=t.sent).route.length>1&&BigInt(Ot(h.route).amount)a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Wn(){return(Wn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,u=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Sn,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[bn.ASSET_TRANSFER_TXN]=g,I[bn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[bn.ASSET_TRANSFER_TXN],signers:[u]},{txn:A[bn.APP_CALL_TXN],signers:[u]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zn(){return(zn=c(i().mark((function e(n){var a,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,u=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:u,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Sn,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[bn.ASSET_TRANSFER_TXN]=T,v[bn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[bn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[bn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jn(){return(Jn=c(i().mark((function t(e){var n,r,a,s,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,rt(r,[s]);case 3:return o=t.sent,u=v(o,1),c=u[0].txnID,t.next=8,Fe(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Yn,Kn,Hn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Gn(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=Gn(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Un.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Un.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Wn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return zn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Jn.apply(this,arguments)}},Zn=(f(Yn={},S.V1_1,Pn),f(Yn,S.V2,Hn),Yn),$n=3n,tr=1000n;function er(){return(er=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,u=v(o,2),c=u[0],p=u[1],l=a.map((function(t,e){return e===Kn.FEE_TXN_INDEX?c:e===Kn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.quote,o=e.swapType,u=e.slippage,c=e.initiatorAddr,p=s.pool,l=s.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:s},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[ut("swap"),o===exports.SwapType.FixedInput?ut("fi"):ut("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",u,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",u,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),_=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:a,suggestedParams:x}),w=r.default.assignGroupID([_,A,T,v]),t.abrupt("return",[{txn:w[0],signers:[c]},{txn:w[1],signers:[m]},{txn:w[2],signers:[c]},{txn:w[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,i,u,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*$n/tr,l=u-i*u/(i+(c-p));if(l>u)throw new Ln;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:tn(d),priceImpact:en(o({inputSupply:i,outputSupply:u},d))}}function ar(t){return sr.apply(this,arguments)}function sr(){return(sr=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new Ln;var c=o*i/(i-u)-o,p=c*tr/(tr-$n),l=p-c,d=Z(a.assetOut,Number(u))/Z(a.assetIn,Number(p)),f=Z(a.assetOut,Number(i))/Z(a.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m}}function ir(t){return ur.apply(this,arguments)}function ur(){return(ur=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,$t({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,$t({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cr(){return(cr=c(i().mark((function t(e){var n,r,a,s,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Kn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Kn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Kn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Kn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,ar({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,ir({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(s),fees:st(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Kn||(Kn={}));var pr,lr={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?rr({pool:e,reserves:n,assetIn:r,decimals:a}):or({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:rr,getFixedOutputSwapQuote:or,generateTxns:function(t){return nr.apply(this,arguments)},signTxns:function(t){return er.apply(this,arguments)},execute:function(t){return cr.apply(this,arguments)},executeFixedOutputSwap:ir},dr=4*t.ALGORAND_MIN_TX_FEE;function fr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Ln})))throw new Ln;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function mr(){return mr=c(i().mark((function t(e){var n,r,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,u=s.map((function(t){return new Promise(function(){var s=c(i().mark((function s(u,c){var p,l;return i().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:lr.getFixedInputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Un.getFixedInputSwapQuote(l);case 8:p=s.sent;case 9:u(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,fr(u);case 4:return p=t.sent,t.abrupt("return",Ir(p));case 6:case"end":return t.stop()}}),t)}))),mr.apply(this,arguments)}function gr(){return gr=c(i().mark((function t(e){var n,r,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,u=s.map((function(t){return new Promise(function(){var s=c(i().mark((function s(u,c){var p,l;return i().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p={type:exports.SwapQuoteType.Direct,quoteWithPool:{quote:lr.getFixedOutputSwapQuote(l),pool:t.info}},s.next=9;break;case 6:return s.next=8,Un.getFixedOutputSwapQuote(l);case 8:p=s.sent;case 9:u(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,fr(u);case 4:return p=t.sent,t.abrupt("return",Ir(p));case 6:case"end":return t.stop()}}),t)}))),gr.apply(this,arguments)}function xr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:St(t.route)}function Ir(t){var e,n=t[0],r=xr(n),a=D(t);try{for(a.s();!(e=a.n()).done;){var s=e.value,o=xr(s);o>r&&(n=s,r=o)}}catch(t){a.e(t)}finally{a.f()}return n}function Ar(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var hr=(f(pr={},S.V1_1,lr),f(pr,S.V2,Un),f(pr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ge(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return mr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return gr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(pr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&Ar(t.quote)===S.V1_1?lr.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):Un.generateTxns(t)})),f(pr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&Ar(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return lr.signTxns(o({pool:e},t))}return Un.signTxns(t)})),f(pr,"execute",(function(t){return t.contractVersion===S.V1_1?lr.execute(t):Un.execute(t)})),pr);function Tr(){return(Tr=c(i().mark((function t(e){var n,r,a,s,o,u,c,p,l,d;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,yr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return u=t.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:st(a),confirmedRound:d,txnID:l,groupID:ot(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function yr(t){return vr.apply(this,arguments)}function vr(){return(vr=c(i().mark((function t(e){var n,a,s,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,u=v(o,1),c=u[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _r(){return _r=c(i().mark((function t(e){var n,a,s,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:st(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return u=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(i().mark((function a(s,o){var c,p,l,d,f,m;return i().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?u[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,rt(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),_r.apply(this,arguments)}function wr(){return(wr=c(i().mark((function t(e){var n,s,o,u,c,p,l,d,f,m,g;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,u=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[ut("redeem")],note:pt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(u),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(u),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ut("commit"),u],note:X([ut("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Dr(){return(Dr=c(i().mark((function e(n){var r,a,s,o,u,c,p,l,d,f,m;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=br({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[ut("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=wn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Ue,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Zn,exports.Swap=hr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Dn,exports.V2AddLiquidityTxnIndices=He,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=Yt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=he,exports.prepareCommitTransactions=function(t){return Dr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return _r.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Tr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=st,exports.tinymanContract_v1_1=jt,exports.tinymanContract_v2=Gt,exports.tinymanJSSDKConfig=pt; +i=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},s=r.iterator||"@@iterator",a=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function u(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var s=e&&e.prototype instanceof d?e:d,a=Object.create(s.prototype),o=new w(r||[]);return a._invoke=function(t,e,n){var r="suspendedStart";return function(s,a){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===s)throw a;return D()}for(n.method=s,n.arg=a;;){var o=n.delegate;if(o){var i=y(o,n);if(i){if(i===l)continue;return i}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var u=p(t,e,n);if("normal"===u.type){if(r=n.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:n.done}}"throw"===u.type&&(r="completed",n.method="throw",n.arg=u.arg)}}}(t,n,o),a}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};u(g,s,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,s)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(s,a,o,i){var u=p(t[s],t,a);if("throw"!==u.type){var c=u.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,i)}),(function(t){r("throw",t,o,i)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,i)}))}i(u.arg)}var s;this._invoke=function(t,n){function a(){return new e((function(e,s){r(t,n,e,s)}))}return s=s?s.then(a,a):a()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var s=r.arg;return s?s.done?(e[t.resultName]=s.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):s:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function w(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[s];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,a=function e(){for(;++r=0;--s){var a=this.tryEntries[s],o=a.completion;if("root"===a.tryLoc)return r("end");if(a.tryLoc<=this.prev){var i=n.call(a,"catchLoc"),u=n.call(a,"finallyLoc");if(i&&u){if(this.prev=0;--r){var s=this.tryEntries[r];if(s.tryLoc<=this.prev&&n.call(s,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var s=r.arg;_(n)}return s}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function u(t,e,n,r,s,a,o){try{var i=t[a](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,s)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,s){var a=t.apply(e,n);function o(t){u(a,r,s,o,i,"next",t)}function i(t){u(a,r,s,o,i,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:s}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,a=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw a}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",k="logic eval error:",P="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var s;p(this,n);for(var a=arguments.length,o=new Array(a>2?a-2:0),i=2;ir?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function Q(t){var e,n=t.stateArray,r=void 0===n?[]:n,s=t.shouldDecodeKeys,a=void 0!==s&&s,o={},i=D(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[a?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function X(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),s=0,a=D(t);try{for(a.s();!(e=a.n()).done;){var o=e.value;r.set(o,s),s+=o.length}}catch(t){a.e(t)}finally{a.f()}return r}var j=100000n,q=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var s=t["apps-local-state"]||[],a=t["created-apps"]||[],o=t.assets||[];return j+q*BigInt(o.length)+U*BigInt(a.length+s.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var s="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*s))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var s=v(nt(e),2),a=s[0],o=s[1],i=v(nt(Math.round(Number(et(a,o+r)))),2),u=i[0],c=i[1];return Number(et(u,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return st.apply(this,arguments)}function st(){return(st=c(i().mark((function t(e,n){var r,s,a,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=D(n),t.prev=3,s.s();case 5:if((a=s.n()).done){t.next=18;break}return o=a.value,t.next=9,e.sendRawTransaction(o).do();case 9:return u=t.sent,c=u.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function at(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function it(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ut(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return ut("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(i().mark((function t(e){var n,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(s,a),note:pt.getAppCallTxnNoteWithClientName(a),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(i().mark((function t(e){var n,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(s,a),note:pt.getAppCallTxnNoteWithClientName(a),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht,Tt=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),yt=ut("swap"),vt=(f(xt={},exports.SwapType.FixedInput,ut("fixed-input")),f(xt,exports.SwapType.FixedOutput,ut("fixed-output")),xt),_t={ASSET_OPT_IN:ut("asset_opt_in")},wt={testnet:159521633,mainnet:0},bt=(f(ht={},exports.SwapType.FixedInput,7),f(ht,exports.SwapType.FixedOutput,8),ht),Dt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function St(t){var e=kt(t),n=e.assetIn,r=e.assetOut;return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Et(t){if(!wt[t])throw new Error("Unknown network or network not supported: ".concat(t));return wt[t]}function Ot(t){return t[t.length-1].quote.amount_out}function Nt(t){return t[0].quote.amount_in}function kt(t){return{assetIn:Nt(t),assetOut:Ot(t)}}function Pt(t){return Lt.apply(this,arguments)}function Lt(){return(Lt=c(i().mark((function e(n){var s,a,o,u,c,p,l;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.routerAppID,o=n.assetIDs,u=n.accountAddress,e.next=3,s.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:a,appArgs:[_t.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ft(t){return Rt.apply(this,arguments)}function Rt(){return(Rt=c(i().mark((function e(n){var s,a,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E,O;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.initiatorAddr,a=n.client,o=n.network,u=n.swapType,c=n.route,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Nt(c).amount),Number(Ot(c).amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Et(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(w=r.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:v,appArgs:[yt,vt[u],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(bt[u]+1),b=[_,w],e.next=15,Bt({client:a,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((D=e.sent).length>0)){e.next=21;break}return e.next=19,Pt({client:a,accountAddress:s,assetIDs:D,routerAppID:v});case 19:E=e.sent,b.unshift(E);case 21:return O=r.default.assignGroupID(b),e.abrupt("return",O.map((function(t){return{txn:t,signers:[s]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bt(t){return Mt.apply(this,arguments)}function Mt(){return(Mt=c(i().mark((function e(n){var r,s,a,o,u,c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,a=n.assetIDs,o=t.getApplicationAddress(s),e.next=4,r.accountInformation(o).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),p=a.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ct(t){return Vt.apply(this,arguments)}function Vt(){return(Vt=c(i().mark((function t(e){var n,r,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,a=e.swapType,o=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:a,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(Dt[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Qt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Xt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},jt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),qt=new(function(n){m(s,jt);var r=y(s);function s(t,e){var n;return p(this,s),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(s,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),s=v(M(n.asset1ID,n.asset2ID),2),a=s[0],o=s[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:a,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(i().mark((function t(s,a){var u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:u=t.sent,s(o(o({},u),{},{minimum_required_balance:Kt(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),a(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Yt(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Kt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Ht,Zt,$t=ut("e");function te(t){return ee.apply(this,arguments)}function ee(){return(ee=c(i().mark((function n(s){var a,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.pool,u=s.accountAddr,n.next=3,a.accountInformation(u).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=Q({stateArray:A}),T=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,$t,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,$t,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,$t,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ne(){return(ne=c(i().mark((function n(s){var a,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.accountAddr,u=s.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==u})),d=[],l&&l["key-value"])for(f=Q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mae)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ae,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ce(){return ce=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d=arguments;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(a=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",a[r]);case 4:return t.next=6,Jt(n,r);case 6:return o=t.sent,u=Yt(o,dt(s,S.V1_1)),c=null,u&&(l=o["created-assets"][0],p=l.index,c={asset1ID:u[re[S.V1_1].asset1],asset2ID:u[re[S.V1_1].asset2],poolTokenID:p},a[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ce.apply(this,arguments)}var pe=Object.freeze({__proto__:null,getPoolInfo:oe,getPoolReserves:function(t,e){return ue.apply(this,arguments)},getPoolAssets:function(t){return ce.apply(this,arguments)}});function le(t){return de.apply(this,arguments)}function de(){return(de=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,a=e.asset2ID,o=zt(S.V2),u=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=u.address(),l=M(s,a),t.next=8,Jt(n,p);case 8:return d=t.sent,f=Yt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[re.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[re.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[re.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[re.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[re.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[re.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[re.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[re.v2.totalFeeShare]),m.poolTokenID=Number(f[re.v2.poolTokenID]),m.asset1ID=Number(f[re.v2.asset1]),m.asset2ID=Number(f[re.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(){return(fe=c(i().mark((function t(e,n){var r,s,a;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Jt(e,n.account.address());case 2:return r=t.sent,s=Yt(r,n.validatorAppID),a={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(a.asset1=BigInt(s[re.v2.asset1Reserves]),a.asset2=BigInt(s[re.v2.asset2Reserves]),a.issuedLiquidity=BigInt(s[re.v2.issuedPoolTokens])),t.abrupt("return",a);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function me(){return(me=c(i().mark((function t(e){var n,r,s,a,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,Jt(n,r);case 3:return a=t.sent,o=Yt(a,dt(s,S.V2)),u=null,o&&(u={asset1ID:o[re[S.V2].asset1],asset2ID:o[re[S.V2].asset2],poolTokenID:o[re[S.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ge=Object.freeze({__proto__:null,getPoolInfo:le,getPoolReserves:function(t,e){return fe.apply(this,arguments)},getPoolAssets:function(t){return me.apply(this,arguments)}});function xe(t){return Boolean(t&&!(t.asset1+t.asset2))}var Ie,Ae,he=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=xe(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:xe,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([oe(t),le(t)])}}),Te=o((f(Ie={},S.V1_1,o(o({},pe),he)),f(Ie,S.V2,o(o({},ge),he)),Ie),he);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Ae||(Ae={}));var ye=96e4,ve=859e3;function _e(){return(_e=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,k;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.asset_1,o=e.asset_2,u=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(a,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(s,S.V1_1),T=qt.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[ut("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:we(A),suggestedParams:c}),(E=[])[Ae.FUNDING_TXN]=D,E[Ae.VALIDATOR_APP_CALL]=_,E[Ae.POOL_TOKEN_CREATE]=w,E[Ae.ASSET1_OPT_IN]=b,A||(E[Ae.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),k=[{txn:N[Ae.FUNDING_TXN],signers:[u]},{txn:N[Ae.VALIDATOR_APP_CALL],signers:[y]},{txn:N[Ae.POOL_TOKEN_CREATE],signers:[y]},{txn:N[Ae.ASSET1_OPT_IN],signers:[y]}],N[Ae.ASSET2_OPT_IN]&&k.push({txn:N[Ae.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",k);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function we(t){return t?ye:ve}function be(){return(be=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,u),f=v(d,2),m=f[0],g=f[1],x=qt.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ae.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),s=n.txID,a=n.blob;return I.push(s),a})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function De(t){return Se.apply(this,arguments)}function Se(){return(Se=c(i().mark((function t(e){var n,r,s,a,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,s[Ae.POOL_TOKEN_CREATE]);case 6:if(a=t.sent,"number"==typeof(o=a["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Ee(){return(Ee=c(i().mark((function t(e){var n,r,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,De({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",Te.v1_1.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Oe,Ne={generateTxns:function(t){return _e.apply(this,arguments)},signTxns:function(t){return be.apply(this,arguments)},execute:function(t){return Ee.apply(this,arguments)},getBootstrapFundingTxnAmount:we};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Oe||(Oe={}));var ke=5,Pe=6;function Le(t,e){return Fe.apply(this,arguments)}function Fe(){return(Fe=c(i().mark((function e(n,r){var s,a,o;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(a=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,a,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Re(t,e){return Be.apply(this,arguments)}function Be(){return(Be=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Le(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Me(){return(Me=c(i().mark((function e(n){var s,a,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.network,o=n.asset_1,u=n.asset_2,c=n.initiatorAddr,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=dt(a,S.V2),d=t.getApplicationAddress(l),f=B(o,u),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,Te.v2.getPoolInfo({client:s,network:a,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(u.unit_name," already exists"));case 12:return I=Wt.generateLogicSigAccountForPool({network:a,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[ut("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ve(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Ce(h),suggestedParams:p}),(_=[])[Oe.FUNDING_TXN]=y,_[Oe.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Oe.FUNDING_TXN]={txn:w[Oe.FUNDING_TXN],signers:[c]},b[Oe.VALIDATOR_APP_CALL]={txn:w[Oe.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ce(t){return function(t,e){var n=zt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ve(t)+1e5}function Ve(e){return((e?ke:Pe)+1)*t.ALGORAND_MIN_TX_FEE}function Qe(){return(Qe=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,u),f=v(d,2),m=f[0],g=f[1],x=Wt.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Oe.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),s=n.txID,a=n.blob;return I.push(s),a})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Le(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(re.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Te.v2.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var je,qe={generateTxns:function(t){return Me.apply(this,arguments)},signTxns:function(t){return Qe.apply(this,arguments)},execute:function(t){return Xe.apply(this,arguments)},getBootstrapFundingTxnAmount:Ce,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Ce(e)}};var Ue,Ge=(f(je={},S.V1_1,Ne),f(je,S.V2,qe),f(je,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Ne.generateTxns(t):qe.generateTxns(t)})),f(je,"signTxns",(function(t){return t.contractVersion===S.V1_1?Ne.signTxns(t):qe.signTxns(t)})),f(je,"execute",(function(t){return t.contractVersion===S.V1_1?Ne.execute(t):qe.execute(t)})),f(je,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Ne.getBootstrapFundingTxnAmount(n):qe.getBootstrapFundingTxnAmount(n)})),je);exports.V1_1AddLiquidityTxnIndices=void 0,(Ue=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ue.FEE_TXN=0]="FEE_TXN",Ue[Ue.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ue[Ue.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ue[Ue.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ue[Ue.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var We,ze,Je,Ye,Ke=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Ye=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Ye.FLEXIBLE="flexible",Ye.INITIAL="initial";var He,Ze,$e=(f(We={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(We,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(We,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),We),tn=(f(ze={},exports.V2AddLiquidityType.INITIAL,1),f(ze,exports.V2AddLiquidityType.SINGLE,2),f(ze,exports.V2AddLiquidityType.FLEXIBLE,2),ze),en=(f(Je={},exports.V2AddLiquidityType.INITIAL,3),f(Je,exports.V2AddLiquidityType.FLEXIBLE,3),f(Je,exports.V2AddLiquidityType.SINGLE,2),Je);function nn(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function rn(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,s=t.assetOut,a=nn({assetIn:r,assetOut:s}),o=Z(s.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(a/o-1))}function sn(t){var e,n,r,s=t.reserves,a=t.totalFeeShare,o=t.asset1,i=t.asset2,u=s.asset1*s.asset2,c=s.asset1+BigInt(o.amount),p=s.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*s.issuedLiquidity*s.issuedLiquidity/u))))),f=d-s.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(x>I){var A=x;r=an(A,a),e={id:o.id,amount:A+r,decimals:o.decimals,reserves:s.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:s.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=an(h,a),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:s.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:o.decimals,reserves:s.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:rn({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function an(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function on(e){return(tn[e]+1)*t.ALGORAND_MIN_TX_FEE}exports.SwapQuoteType=void 0,(He=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",He.Router="router";var un=ut("add_liquidity"),cn=(f(Ze={},S.V1_1,[ut("mint")]),f(Ze,S.V2,{INITIAL_LIQUIDITY:[ut("add_initial_liquidity")],SINGLE_ASSET_MODE:[un,ut("single")],FLEXIBLE_MODE:[un,ut("flexible")]}),Ze);function pn(){return(pn=c(i().mark((function t(e){var n,a,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,o=e.poolAddress,u=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,S.V1_1),appArgs:cn.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[u.id,p.id]:[u.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:s,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ln(){return(ln=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,o=n.account,t.next=4,a([s]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dn(){return(dn=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.signedTxns,o=e.initiatorAddr,t.prev=1,u=BigInt(s[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,te({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[a]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=at(s),x=ot(s),t.next=17,te({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var fn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,s=t.asset2In;if(0n===n.issuedLiquidity){var a=BigInt(Math.floor(Math.sqrt(Number(r)*Number(s))));if(a<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(a,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(s),poolTokenID:e.poolTokenID,poolTokenOut:a-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(s)*n.issuedLiquidity/n.asset2,u=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Rn(){return(Rn=c(i().mark((function t(e){var n,s,a,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Ft(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,s=e.initiatorAddr,a=e.slippage,u=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=C(f),h=u===exports.SwapType.FixedInput?d.assetInAmount:H("positive",a,d.assetInAmount),T=u===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",a,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:l.validatorAppID,appArgs:[yt,vt[u],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Bn(u),(_=[])[It.INPUT_TXN]=y,_[It.APP_CALL_TXN]=v,w=r.default.assignGroupID(_),t.abrupt("return",[{txn:w[It.INPUT_TXN],signers:[s]},{txn:w[It.APP_CALL_TXN],signers:[s]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Bn(e){return(Tt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Mn(){return(Mn=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return s=e.client,a=e.quote,o=e.txGroup,u=e.signedTxns,t.next=3,rt(s,[u]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Re(s,o);case 10:return m=t.sent,g=a.type===exports.SwapQuoteType.Direct?{id:a.quoteWithPool.quote.assetInID,amount:a.quoteWithPool.quote.assetInAmount}:{id:Number(a.asset_in_id),amount:Number(a.route[0].quote.amount_in.amount)},x=a.type===exports.SwapQuoteType.Direct?a.quoteWithPool.quote.assetOutID:Number(a.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:a,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Cn(){return(Cn=c(i().mark((function t(e){var n,r,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.pool,s=e.asset,a=e.decimals,o=e.network,u=e.isSwapRouterEnabled,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,Vn({pool:r,assetIn:s,decimals:a,isSwapRouterEnabled:u,network:o});case 4:c=t.sent,t.next=10;break;case 7:return t.next=9,Xn({pool:r,assetOut:s,decimals:a,isSwapRouterEnabled:u,network:o});case 9:c=t.sent;case 10:return t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Vn(t){return Qn.apply(this,arguments)}function Qn(){return(Qn=c(i().mark((function t(e){var n,r,s,a,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,s=e.pool,a=e.isSwapRouterEnabled,u=e.network,s.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:s,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=s.totalFeeShare,n.id!==s.asset1ID){t.next=11;break}l=s.asset2ID,d=s.asset1Reserves,f=s.asset2Reserves,t.next=18;break;case 11:if(n.id!==s.asset2ID){t.next=17;break}l=s.asset1ID,d=s.asset2Reserves,f=s.asset1Reserves,t.next=18;break;case 17:throw new F({pool:s,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(s.account.address(),"."));case 18:if(m=qn({inputSupply:d,outputSupply:f,swapInputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapOutputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(g>f)){t.next=21;break}throw new Fn;case 21:if(A={assetInID:n.id,assetInAmount:c,assetOutID:l,assetOutAmount:g,swapFee:Number(x),rate:Z(r.assetOut,Number(g))/Z(r.assetIn,Number(c)),priceImpact:I},!a){t.next=28;break}return t.next=25,Ct({amount:n.amount,assetInID:Number(n.id),assetOutID:l,swapType:exports.SwapType.FixedInput,network:u});case 25:if(!((h=t.sent).route.length>1&&BigInt(Ot(h.route).amount)>A.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},h),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:s,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xn(t){return jn.apply(this,arguments)}function jn(){return(jn=c(i().mark((function t(e){var n,r,s,a,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,s=e.pool,a=e.isSwapRouterEnabled,u=e.network,s.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:s,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=s.totalFeeShare,n.id!==s.asset1ID){t.next=11;break}l=s.asset2ID,d=s.asset2Reserves,f=s.asset1Reserves,t.next=18;break;case 11:if(n.id!==s.asset2ID){t.next=17;break}l=s.asset1ID,d=s.asset1Reserves,f=s.asset2Reserves,t.next=18;break;case 17:throw new F({pool:s,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(s.account.address(),"."));case 18:if(m=Un({inputSupply:d,outputSupply:f,swapOutputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapInputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(c>f)){t.next=21;break}throw new Fn;case 21:if(A={assetInID:l,assetInAmount:g,assetOutID:n.id,assetOutAmount:c,swapFee:Number(x),rate:Z(r.assetOut,Number(c))/Z(r.assetIn,Number(g)),priceImpact:I},!a){t.next=28;break}return t.next=25,Ct({amount:n.amount,assetInID:l,assetOutID:n.id,swapType:exports.SwapType.FixedOutput,network:u});case 25:if(!((h=t.sent).route.length>1&&BigInt(Ot(h.route).amount)s+BigInt(1e3)?(n=s*e.asset1/a,r=s*e.asset2/a):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function zn(){return(zn=c(i().mark((function e(n){var s,a,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.poolTokenIn,u=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.account.address(),m=a.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[On,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[Sn.ASSET_TRANSFER_TXN]=g,I[Sn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[Sn.ASSET_TRANSFER_TXN],signers:[u]},{txn:A[Sn.APP_CALL_TXN],signers:[u]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jn(){return(Jn=c(i().mark((function e(n){var s,a,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.initiatorAddr,u=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.asset1ID,m=a.asset2ID,g=a.account.address(),x=a.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:u,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[On,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[Sn.ASSET_TRANSFER_TXN]=T,v[Sn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[Sn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[Sn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Yn(){return(Yn=c(i().mark((function t(e){var n,r,s,a,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,s=e.txGroup,a=e.signedTxns,t.next=3,rt(r,[a]);case 3:return o=t.sent,u=v(o,1),c=u[0].txnID,t.next=8,Re(r,s);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Hn,Zn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,s=BigInt(r),a=Wn(s,n),o=a.asset1OutputAmount,i=a.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:s}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,s=t.poolTokenIn,a=t.assetOutID,o=t.decimals,i=BigInt(s),u=Wn(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(a===n.asset1ID){var d=Gn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(a!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Gn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return zn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Jn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Yn.apply(this,arguments)}},$n=(f(Kn={},S.V1_1,Ln),f(Kn,S.V2,Zn),Kn),tr=3n,er=1000n;function nr(){return(nr=c(i().mark((function t(e){var n,s,a,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,t.next=3,a([s]);case 3:return o=t.sent,u=v(o,2),c=u[0],p=u[1],l=s.map((function(t,e){return e===Hn.FEE_TXN_INDEX?c:e===Hn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=c(i().mark((function t(e){var n,a,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,a=e.quote,o=e.swapType,u=e.slippage,c=e.initiatorAddr,p=a.pool,l=a.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:a},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[ut("swap"),o===exports.SwapType.FixedInput?ut("fi"):ut("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",u,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",u,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),_=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:s,suggestedParams:x}),w=r.default.assignGroupID([_,A,T,v]),t.abrupt("return",[{txn:w[0],signers:[c]},{txn:w[1],signers:[m]},{txn:w[2],signers:[c]},{txn:w[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sr(t){var e=t.pool,n=t.reserves,r=t.assetIn,s=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var a,i,u,c=BigInt(r.amount);if(r.id===e.asset1ID)a=e.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));a=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*tr/er,l=u-i*u/(i+(c-p));if(l>u)throw new Fn;var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:a,assetOutAmount:l,swapFee:Number(p),rate:nn(d),priceImpact:rn(o({inputSupply:i,outputSupply:u},d))};return{type:exports.SwapQuoteType.Direct,quoteWithPool:{pool:e,quote:f}}}function ar(t){return or.apply(this,arguments)}function or(){return(or=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,te({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,rt(n,[s]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,te({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ir(t){var e=t.pool,n=t.reserves,r=t.assetOut,s=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var a,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)a=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));a=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new Fn;var c=o*i/(i-u)-o,p=c*er/(er-tr),l=p-c,d=Z(s.assetOut,Number(u))/Z(s.assetIn,Number(p)),f=Z(s.assetOut,Number(i))/Z(s.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:a,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,quoteWithPool:{pool:e,quote:g}}}function ur(t){return cr.apply(this,arguments)}function cr(){return(cr=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,te({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,rt(n,[s]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,te({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,a.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:a.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pr(){return(pr=c(i().mark((function t(e){var n,r,s,a,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,s=e.swapType,a=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:s,txGroup:a},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:a[Hn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:a[Hn.ASSET_IN_TXN_INDEX].txn.amount},l={id:a[Hn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:a[Hn.ASSET_OUT_TXN_INDEX].txn.amount},s!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,ar({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,ur({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(a),fees:at(a)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Hn||(Hn={}));var lr,dr={getQuote:function(t,e,n,r,s){return t===exports.SwapType.FixedInput?sr({pool:e,reserves:n,assetIn:r,decimals:s}):ir({pool:e,reserves:n,assetOut:r,decimals:s})},getFixedInputSwapQuote:sr,getFixedOutputSwapQuote:ir,generateTxns:function(t){return rr.apply(this,arguments)},signTxns:function(t){return nr.apply(this,arguments)},execute:function(t){return pr.apply(this,arguments)},executeFixedOutputSwap:ur},fr=4*t.ALGORAND_MIN_TX_FEE;function mr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Fn})))throw new Fn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function gr(){return gr=c(i().mark((function t(e){var n,r,s,a,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,a=e.pools,o=e.isSwapRouterEnabled,u=a.map((function(t){return new Promise(function(){var a=c(i().mark((function a(u,c){var p,l;return i().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},a.prev=1,t.info.contractVersion!==S.V1_1){a.next=6;break}p=dr.getFixedInputSwapQuote(l),a.next=9;break;case 6:return a.next=8,Gn.getFixedInputSwapQuote(l);case 8:p=a.sent;case 9:u(p),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(1),c(a.t0);case 15:case"end":return a.stop()}}),a,null,[[1,12]])})));return function(t,e){return a.apply(this,arguments)}}())})),t.next=4,mr(u);case 4:return p=t.sent,t.abrupt("return",Ar(p));case 6:case"end":return t.stop()}}),t)}))),gr.apply(this,arguments)}function xr(){return xr=c(i().mark((function t(e){var n,r,s,a,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,a=e.pools,o=e.isSwapRouterEnabled,u=a.map((function(t){return new Promise(function(){var a=c(i().mark((function a(u,c){var p,l;return i().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(s.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},a.prev=1,t.info.contractVersion!==S.V1_1){a.next=6;break}p=dr.getFixedOutputSwapQuote(l),a.next=9;break;case 6:return a.next=8,Gn.getFixedOutputSwapQuote(l);case 8:p=a.sent;case 9:u(p),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(1),c(a.t0);case 15:case"end":return a.stop()}}),a,null,[[1,12]])})));return function(t,e){return a.apply(this,arguments)}}())})),t.next=4,mr(u);case 4:return p=t.sent,t.abrupt("return",Ar(p));case 6:case"end":return t.stop()}}),t)}))),xr.apply(this,arguments)}function Ir(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:St(t.route)}function Ar(t){var e,n=t[0],r=Ir(n),s=D(t);try{for(s.s();!(e=s.n()).done;){var a=e.value,o=Ir(a);o>r&&(n=a,r=o)}}catch(t){s.e(t)}finally{s.f()}return n}function hr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var Tr=(f(lr={},S.V1_1,dr),f(lr,S.V2,Gn),f(lr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return xe(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return gr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return xr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(lr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&hr(t.quote)===S.V1_1?dr.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):Gn.generateTxns(t)})),f(lr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&hr(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return dr.signTxns(o({pool:e},t))}return Gn.signTxns(t)})),f(lr,"execute",(function(t){return t.contractVersion===S.V1_1?dr.execute(t):Gn.execute(t)})),lr);function yr(){return(yr=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.initiatorSigner,t.prev=1,t.next=4,vr({txGroup:s,pool:r,initiatorSigner:a});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return u=t.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:at(s),confirmedRound:d,txnID:l,groupID:ot(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function vr(t){return _r.apply(this,arguments)}function _r(){return(_r=c(i().mark((function t(e){var n,s,a,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.pool,a=e.initiatorSigner,t.next=3,a([n]);case 3:return o=t.sent,u=v(o,1),c=u[0],p=s.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function wr(){return wr=c(i().mark((function t(e){var n,s,a,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.data,a=e.initiatorSigner,t.prev=1,o=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:at(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,a(o.map((function(t){return t.txns})));case 5:return u=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var s=c(i().mark((function s(a,o){var c,p,l,d,f,m;return i().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:return s.prev=0,c=t.txns.map((function(n,s){return 0===s?u[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),s.next=4,rt(n,[c]);case 4:p=s.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,a({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(0),o(s.t0);case 15:case"end":return s.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),wr.apply(this,arguments)}function br(){return(br=c(i().mark((function t(e){var n,a,o,u,c,p,l,d,f,m,g;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.pool,o=e.assetID,u=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=a.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:a.validatorAppID,appArgs:[ut("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[c],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(u),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(u),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:s,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Dr(e){var n=e.suggestedParams,r=e.stakingAppID,s=e.initiatorAddr,a=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:s,suggestedParams:n,foreignAssets:[a],accounts:[o.accountAddress],appArgs:[ut("commit"),u],note:X([ut("tinymanStaking/v1:b"),c,t.encodeUint64(a),u])})}function Sr(){return(Sr=c(i().mark((function e(n){var r,s,a,o,u,c,p,l,d,f,m;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,a=n.program,o=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Dr({suggestedParams:l,stakingAppID:s,program:a,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[o],accounts:[a.accountAddress],appArgs:[ut("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Dn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Ge,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=$n,exports.Swap=Tr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=En,exports.V2AddLiquidityTxnIndices=$e,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=Kt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=Te,exports.prepareCommitTransactions=function(t){return Sr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return yr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=at,exports.tinymanContract_v1_1=qt,exports.tinymanContract_v2=Wt,exports.tinymanJSSDKConfig=pt; diff --git a/dist/swap/common/utils.d.ts b/dist/swap/common/utils.d.ts index 2d87a35e..4d51cb0a 100644 --- a/dist/swap/common/utils.d.ts +++ b/dist/swap/common/utils.d.ts @@ -1,4 +1,5 @@ -import { AssetWithAmountAndDecimals } from "../../util/asset/assetModels"; +import { AssetWithAmountAndDecimals, AssetWithIdAndAmount } from "../../util/asset/assetModels"; +import { SwapQuote } from "../types"; declare function calculateSwapRate({ assetIn, assetOut }: { assetIn: AssetWithAmountAndDecimals; assetOut: AssetWithAmountAndDecimals; @@ -9,4 +10,11 @@ declare function calculatePriceImpact({ inputSupply, outputSupply, assetIn, asse assetIn: AssetWithAmountAndDecimals; assetOut: AssetWithAmountAndDecimals; }): number; -export { calculateSwapRate, calculatePriceImpact }; +declare function getSwapQuotePriceImpact(quote: SwapQuote): number; +declare function getAssetInFromSwapQuote(quote: SwapQuote): AssetWithIdAndAmount; +declare function getAssetOutFromSwapQuote(quote: SwapQuote): AssetWithIdAndAmount; +declare function getAssetInAndAssetOutFromSwapQuote(quote: SwapQuote): { + assetIn: AssetWithIdAndAmount; + assetOut: AssetWithIdAndAmount; +}; +export { calculateSwapRate, calculatePriceImpact, getSwapQuotePriceImpact, getAssetInFromSwapQuote, getAssetOutFromSwapQuote, getAssetInAndAssetOutFromSwapQuote }; diff --git a/dist/swap/index.d.ts b/dist/swap/index.d.ts index e1b1b14d..11b54255 100644 --- a/dist/swap/index.d.ts +++ b/dist/swap/index.d.ts @@ -4,7 +4,7 @@ export declare const Swap: { getQuote: (type: import("./constants").SwapType, pool: import("..").V1PoolInfo, reserves: import("..").PoolReserves, asset: import("../util/asset/assetModels").AssetWithIdAndAmount, decimals: { assetIn: number; assetOut: number; - }) => import("./types").DirectSwapQuote; + }) => import("./types").SwapQuote; getFixedInputSwapQuote: ({ pool, reserves, assetIn, decimals }: { pool: import("..").V1PoolInfo; reserves: import("..").PoolReserves; @@ -13,7 +13,7 @@ export declare const Swap: { assetIn: number; assetOut: number; }; - }) => import("./types").DirectSwapQuote; + }) => import("./types").SwapQuote; getFixedOutputSwapQuote: ({ pool, reserves, assetOut, decimals }: { pool: import("..").V1PoolInfo; reserves: import("..").PoolReserves; @@ -22,7 +22,7 @@ export declare const Swap: { assetIn: number; assetOut: number; }; - }) => import("./types").DirectSwapQuote; + }) => import("./types").SwapQuote; generateTxns: ({ client, quote, swapType, slippage, initiatorAddr }: import("./types").GenerateV1_1SwapTxnsParams) => Promise; signTxns: ({ pool, txGroup, initiatorSigner }: { pool: import("..").V1PoolInfo; diff --git a/dist/swap/v1_1/index.d.ts b/dist/swap/v1_1/index.d.ts index 00140977..1066fda8 100644 --- a/dist/swap/v1_1/index.d.ts +++ b/dist/swap/v1_1/index.d.ts @@ -1,7 +1,7 @@ import { Algodv2 } from "algosdk"; import { InitiatorSigner, SignerTransaction } from "../../util/commonTypes"; import { PoolReserves, V1PoolInfo } from "../../util/pool/poolTypes"; -import { DirectSwapQuote, GenerateV1_1SwapTxnsParams, V1SwapExecution } from "../types"; +import { GenerateV1_1SwapTxnsParams, SwapQuote, V1SwapExecution } from "../types"; import { SwapType } from "../constants"; import { AssetWithIdAndAmount } from "../../util/asset/assetModels"; declare function signTxns({ pool, txGroup, initiatorSigner }: { @@ -24,7 +24,7 @@ declare function generateTxns({ client, quote, swapType, slippage, initiatorAddr declare function getQuote(type: SwapType, pool: V1PoolInfo, reserves: PoolReserves, asset: AssetWithIdAndAmount, decimals: { assetIn: number; assetOut: number; -}): DirectSwapQuote; +}): SwapQuote; /** * Get a quote for a fixed input swap This does not execute any transactions. * @@ -42,7 +42,7 @@ declare function getFixedInputSwapQuote({ pool, reserves, assetIn, decimals }: { assetIn: number; assetOut: number; }; -}): DirectSwapQuote; +}): SwapQuote; /** * Get a quote for a fixed output swap This does not execute any transactions. * @@ -60,7 +60,7 @@ declare function getFixedOutputSwapQuote({ pool, reserves, assetOut, decimals }: assetIn: number; assetOut: number; }; -}): DirectSwapQuote; +}): SwapQuote; /** * Execute a fixed output swap with the desired quantities. * diff --git a/dist/swap/v2/router/util.d.ts b/dist/swap/v2/router/util.d.ts index f832be26..58f63844 100644 --- a/dist/swap/v2/router/util.d.ts +++ b/dist/swap/v2/router/util.d.ts @@ -1,12 +1,23 @@ import { SupportedNetwork } from "../../../util/commonTypes"; import { SwapRoute } from "../../types"; -export declare function getSwapRouteRate(route: SwapRoute): number; -export declare function getSwapRouterAppID(network: SupportedNetwork): number; -export declare function getAssetOutFromSwapRoute(route: SwapRoute): { +declare function getSwapRouteRate(route: SwapRoute): number; +declare function getSwapRouterAppID(network: SupportedNetwork): number; +declare function getAssetOutFromSwapRoute(route: SwapRoute): { asset: import("../../types").SwapRouteAsset; amount: string; }; -export declare function getAssetInFromSwapRoute(route: SwapRoute): { +declare function getAssetInFromSwapRoute(route: SwapRoute): { asset: import("../../types").SwapRouteAsset; amount: string; }; +declare function getAssetInAndOutFromSwapRoute(route: SwapRoute): { + assetIn: { + asset: import("../../types").SwapRouteAsset; + amount: string; + }; + assetOut: { + asset: import("../../types").SwapRouteAsset; + amount: string; + }; +}; +export { getSwapRouteRate, getSwapRouterAppID, getAssetOutFromSwapRoute, getAssetInFromSwapRoute, getAssetInAndOutFromSwapRoute }; diff --git a/src/swap/common/utils.ts b/src/swap/common/utils.ts index 50c7e973..2fba6601 100644 --- a/src/swap/common/utils.ts +++ b/src/swap/common/utils.ts @@ -1,5 +1,11 @@ -import {AssetWithAmountAndDecimals} from "../../util/asset/assetModels"; +import { + AssetWithAmountAndDecimals, + AssetWithIdAndAmount +} from "../../util/asset/assetModels"; +import {getAssetId} from "../../util/asset/assetUtils"; import {convertFromBaseUnits, roundNumber} from "../../util/util"; +import {SwapQuote, SwapQuoteType} from "../types"; +import {getAssetInFromSwapRoute, getAssetOutFromSwapRoute} from "../v2/router/util"; function calculateSwapRate({ assetIn, @@ -33,4 +39,59 @@ function calculatePriceImpact({ return roundNumber({decimalPlaces: 5}, Math.abs(swapRate / poolPrice - 1)); } -export {calculateSwapRate, calculatePriceImpact}; +function getSwapQuotePriceImpact(quote: SwapQuote) { + return quote.type === SwapQuoteType.Router + ? Number(quote.price_impact) + : quote.quoteWithPool.quote.priceImpact; +} + +function getAssetInFromSwapQuote(quote: SwapQuote): AssetWithIdAndAmount { + if (quote.type === SwapQuoteType.Router) { + const assetIn = getAssetInFromSwapRoute(quote.route); + + return { + id: getAssetId(assetIn.asset), + amount: Number(assetIn.amount) + }; + } + + return { + id: quote.quoteWithPool.quote.assetInID, + amount: quote.quoteWithPool.quote.assetInAmount + }; +} + +function getAssetOutFromSwapQuote(quote: SwapQuote): AssetWithIdAndAmount { + if (quote.type === SwapQuoteType.Router) { + const assetOut = getAssetOutFromSwapRoute(quote.route); + + return { + id: getAssetId(assetOut.asset), + amount: Number(assetOut.amount) + }; + } + + return { + id: quote.quoteWithPool.quote.assetOutID, + amount: quote.quoteWithPool.quote.assetOutAmount + }; +} + +function getAssetInAndAssetOutFromSwapQuote(quote: SwapQuote): { + assetIn: AssetWithIdAndAmount; + assetOut: AssetWithIdAndAmount; +} { + return { + assetIn: getAssetInFromSwapQuote(quote), + assetOut: getAssetOutFromSwapQuote(quote) + }; +} + +export { + calculateSwapRate, + calculatePriceImpact, + getSwapQuotePriceImpact, + getAssetInFromSwapQuote, + getAssetOutFromSwapQuote, + getAssetInAndAssetOutFromSwapQuote +}; diff --git a/src/swap/utils.ts b/src/swap/utils.ts index 91dcf830..9b566d1a 100644 --- a/src/swap/utils.ts +++ b/src/swap/utils.ts @@ -89,13 +89,7 @@ export async function getFixedInputSwapQuote( try { if (pool.info.contractVersion === CONTRACT_VERSION.V1_1) { - quote = { - type: SwapQuoteType.Direct, - quoteWithPool: { - quote: SwapV1_1.getFixedInputSwapQuote(quoteGetterArgs), - pool: pool.info - } - }; + quote = SwapV1_1.getFixedInputSwapQuote(quoteGetterArgs); } else { quote = await SwapV2.getFixedInputSwapQuote(quoteGetterArgs); } @@ -135,13 +129,7 @@ export async function getFixedOutputSwapQuote( try { if (pool.info.contractVersion === CONTRACT_VERSION.V1_1) { - quote = { - type: SwapQuoteType.Direct, - quoteWithPool: { - quote: SwapV1_1.getFixedOutputSwapQuote(quoteGetterArgs), - pool: pool.info - } - }; + quote = SwapV1_1.getFixedOutputSwapQuote(quoteGetterArgs); } else { quote = await SwapV2.getFixedOutputSwapQuote(quoteGetterArgs); } diff --git a/src/swap/v1_1/index.ts b/src/swap/v1_1/index.ts index 4c005e93..2fee44a9 100644 --- a/src/swap/v1_1/index.ts +++ b/src/swap/v1_1/index.ts @@ -15,7 +15,13 @@ import {DEFAULT_FEE_TXN_NOTE} from "../../util/constant"; import {ALGO_ASSET_ID} from "../../util/asset/assetConstants"; import {PoolReserves, PoolStatus, V1PoolInfo} from "../../util/pool/poolTypes"; import {getAccountExcessWithinPool} from "../../util/account/accountUtils"; -import {DirectSwapQuote, GenerateV1_1SwapTxnsParams, V1SwapExecution} from "../types"; +import { + DirectSwapQuote, + GenerateV1_1SwapTxnsParams, + SwapQuote, + SwapQuoteType, + V1SwapExecution +} from "../types"; import {SwapType} from "../constants"; import {calculatePriceImpact, calculateSwapRate} from "../common/utils"; import OutputAmountExceedsAvailableLiquidityError from "../../util/error/OutputAmountExceedsAvailableLiquidityError"; @@ -189,8 +195,8 @@ function getQuote( reserves: PoolReserves, asset: AssetWithIdAndAmount, decimals: {assetIn: number; assetOut: number} -): DirectSwapQuote { - let quote: DirectSwapQuote; +): SwapQuote { + let quote: SwapQuote; if (type === SwapType.FixedInput) { quote = getFixedInputSwapQuote({pool, reserves, assetIn: asset, decimals}); @@ -220,7 +226,7 @@ function getFixedInputSwapQuote({ reserves: PoolReserves; assetIn: AssetWithIdAndAmount; decimals: {assetIn: number; assetOut: number}; -}): DirectSwapQuote { +}): SwapQuote { if (pool.status !== PoolStatus.READY) { throw new TinymanError({pool, assetIn}, "Trying to swap on a non-existent pool"); } @@ -261,7 +267,7 @@ function getFixedInputSwapQuote({ assetOut: {amount: assetOutAmount, decimals: decimals.assetOut} }; - return { + const directSwapQuote: DirectSwapQuote = { round: reserves.round, assetInID: assetIn.id, assetInAmount, @@ -275,6 +281,14 @@ function getFixedInputSwapQuote({ ...assetDataForSwapUtils }) }; + + return { + type: SwapQuoteType.Direct, + quoteWithPool: { + pool, + quote: directSwapQuote + } + }; } /** @@ -371,7 +385,7 @@ function getFixedOutputSwapQuote({ reserves: PoolReserves; assetOut: AssetWithIdAndAmount; decimals: {assetIn: number; assetOut: number}; -}): DirectSwapQuote { +}): SwapQuote { if (pool.status !== PoolStatus.READY) { throw new TinymanError({pool, assetOut}, "Trying to swap on a non-existent pool"); } @@ -420,7 +434,7 @@ function getFixedOutputSwapQuote({ const priceImpact = roundNumber({decimalPlaces: 5}, Math.abs(rate / poolPrice - 1)); - return { + const directSwapQuote: DirectSwapQuote = { round: reserves.round, assetInID, assetInAmount, @@ -430,6 +444,14 @@ function getFixedOutputSwapQuote({ rate, priceImpact }; + + return { + type: SwapQuoteType.Direct, + quoteWithPool: { + pool, + quote: directSwapQuote + } + }; } /** diff --git a/src/swap/v2/router/util.ts b/src/swap/v2/router/util.ts index c8850c2a..c4dcb8f4 100644 --- a/src/swap/v2/router/util.ts +++ b/src/swap/v2/router/util.ts @@ -3,11 +3,8 @@ import {convertFromBaseUnits} from "../../../util/util"; import {SwapRoute} from "../../types"; import {SWAP_ROUTER_APP_ID} from "./constants"; -export function getSwapRouteRate(route: SwapRoute) { - const [assetIn, assetOut] = [ - route[0].quote.amount_in, - route[route.length - 1].quote.amount_out - ]; +function getSwapRouteRate(route: SwapRoute) { + const {assetIn, assetOut} = getAssetInAndOutFromSwapRoute(route); return ( convertFromBaseUnits(assetOut.asset.decimals, Number(assetOut.amount)) / @@ -15,7 +12,7 @@ export function getSwapRouteRate(route: SwapRoute) { ); } -export function getSwapRouterAppID(network: SupportedNetwork) { +function getSwapRouterAppID(network: SupportedNetwork) { const id = SWAP_ROUTER_APP_ID[network]; if (!id) { @@ -25,10 +22,25 @@ export function getSwapRouterAppID(network: SupportedNetwork) { return SWAP_ROUTER_APP_ID[network]; } -export function getAssetOutFromSwapRoute(route: SwapRoute) { +function getAssetOutFromSwapRoute(route: SwapRoute) { return route[route.length - 1].quote.amount_out; } -export function getAssetInFromSwapRoute(route: SwapRoute) { +function getAssetInFromSwapRoute(route: SwapRoute) { return route[0].quote.amount_in; } + +function getAssetInAndOutFromSwapRoute(route: SwapRoute) { + return { + assetIn: getAssetInFromSwapRoute(route), + assetOut: getAssetOutFromSwapRoute(route) + }; +} + +export { + getSwapRouteRate, + getSwapRouterAppID, + getAssetOutFromSwapRoute, + getAssetInFromSwapRoute, + getAssetInAndOutFromSwapRoute +}; From 2739487e50d553561c6fe7c8254342e6d622e8cc Mon Sep 17 00:00:00 2001 From: anlerkan Date: Thu, 9 Mar 2023 16:00:30 +0300 Subject: [PATCH 18/39] feat(swap/util): add getSwapQuoteContractVersion --- dist/swap/common/utils.d.ts | 3 ++- src/swap/common/utils.ts | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/dist/swap/common/utils.d.ts b/dist/swap/common/utils.d.ts index 4d51cb0a..b130eb48 100644 --- a/dist/swap/common/utils.d.ts +++ b/dist/swap/common/utils.d.ts @@ -17,4 +17,5 @@ declare function getAssetInAndAssetOutFromSwapQuote(quote: SwapQuote): { assetIn: AssetWithIdAndAmount; assetOut: AssetWithIdAndAmount; }; -export { calculateSwapRate, calculatePriceImpact, getSwapQuotePriceImpact, getAssetInFromSwapQuote, getAssetOutFromSwapQuote, getAssetInAndAssetOutFromSwapQuote }; +declare function getSwapQuoteContractVersion(quote: SwapQuote): import("../..").ContractVersionValue; +export { calculateSwapRate, calculatePriceImpact, getSwapQuotePriceImpact, getAssetInFromSwapQuote, getAssetOutFromSwapQuote, getAssetInAndAssetOutFromSwapQuote, getSwapQuoteContractVersion }; diff --git a/src/swap/common/utils.ts b/src/swap/common/utils.ts index 2fba6601..a86a205e 100644 --- a/src/swap/common/utils.ts +++ b/src/swap/common/utils.ts @@ -1,3 +1,4 @@ +import {CONTRACT_VERSION} from "../../contract/constants"; import { AssetWithAmountAndDecimals, AssetWithIdAndAmount @@ -87,11 +88,19 @@ function getAssetInAndAssetOutFromSwapQuote(quote: SwapQuote): { }; } +function getSwapQuoteContractVersion(quote: SwapQuote) { + if (quote.type === SwapQuoteType.Direct) { + return quote.quoteWithPool.pool.contractVersion; + } + return CONTRACT_VERSION.V2; +} + export { calculateSwapRate, calculatePriceImpact, getSwapQuotePriceImpact, getAssetInFromSwapQuote, getAssetOutFromSwapQuote, - getAssetInAndAssetOutFromSwapQuote + getAssetInAndAssetOutFromSwapQuote, + getSwapQuoteContractVersion }; From eb5a327dbabefb8c0fb4e936bed42aa16f834ba4 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Thu, 9 Mar 2023 16:02:50 +0300 Subject: [PATCH 19/39] chore: export common swap utils --- dist/index.d.ts | 1 + dist/index.js | 4 ++-- src/index.ts | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index ca7dc8d8..e1df00d3 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,6 +1,7 @@ export type { InitiatorSigner, SignerTransaction, SupportedNetwork } from "./util/commonTypes"; export { BASE_MINIMUM_BALANCE, MINIMUM_BALANCE_REQUIRED_PER_ASSET, MINIMUM_BALANCE_REQUIRED_PER_APP, MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA, MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE, MINIMUM_ADD_LIQUIDITY_AMOUNT } from "./util/constant"; export * from "./swap/v2/router"; +export * from "./swap/common/utils"; export { applySlippageToAmount, ASSET_OPT_IN_PROCESS_TXN_COUNT, convertFromBaseUnits, convertToBaseUnits, sendAndWaitRawTransaction, getTxnGroupID, sumUpTxnFees } from "./util/util"; export { generateOptIntoAssetTxns } from "./util/asset/assetUtils"; export type { AccountAsset, TinymanAnalyticsApiAsset, IndexerAssetInformation } from "./util/asset/assetModels"; diff --git a/dist/index.js b/dist/index.js index d8302698..b7bd2da5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),s=Uint8Array.from([1]);function a(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--s){var a=this.tryEntries[s],o=a.completion;if("root"===a.tryLoc)return r("end");if(a.tryLoc<=this.prev){var i=n.call(a,"catchLoc"),u=n.call(a,"finallyLoc");if(i&&u){if(this.prev=0;--r){var s=this.tryEntries[r];if(s.tryLoc<=this.prev&&n.call(s,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var s=r.arg;_(n)}return s}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function u(t,e,n,r,s,a,o){try{var i=t[a](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,s)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,s){var a=t.apply(e,n);function o(t){u(a,r,s,o,i,"next",t)}function i(t){u(a,r,s,o,i,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:s}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,a=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw a}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",k="logic eval error:",P="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var s;p(this,n);for(var a=arguments.length,o=new Array(a>2?a-2:0),i=2;ir?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function Q(t){var e,n=t.stateArray,r=void 0===n?[]:n,s=t.shouldDecodeKeys,a=void 0!==s&&s,o={},i=D(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[a?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function X(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),s=0,a=D(t);try{for(a.s();!(e=a.n()).done;){var o=e.value;r.set(o,s),s+=o.length}}catch(t){a.e(t)}finally{a.f()}return r}var j=100000n,q=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var s=t["apps-local-state"]||[],a=t["created-apps"]||[],o=t.assets||[];return j+q*BigInt(o.length)+U*BigInt(a.length+s.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var s="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*s))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var s=v(nt(e),2),a=s[0],o=s[1],i=v(nt(Math.round(Number(et(a,o+r)))),2),u=i[0],c=i[1];return Number(et(u,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return st.apply(this,arguments)}function st(){return(st=c(i().mark((function t(e,n){var r,s,a,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=D(n),t.prev=3,s.s();case 5:if((a=s.n()).done){t.next=18;break}return o=a.value,t.next=9,e.sendRawTransaction(o).do();case 9:return u=t.sent,c=u.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function at(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function it(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ut(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return ut("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(i().mark((function t(e){var n,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(s,a),note:pt.getAppCallTxnNoteWithClientName(a),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(i().mark((function t(e){var n,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(s,a),note:pt.getAppCallTxnNoteWithClientName(a),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht,Tt=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),yt=ut("swap"),vt=(f(xt={},exports.SwapType.FixedInput,ut("fixed-input")),f(xt,exports.SwapType.FixedOutput,ut("fixed-output")),xt),_t={ASSET_OPT_IN:ut("asset_opt_in")},wt={testnet:159521633,mainnet:0},bt=(f(ht={},exports.SwapType.FixedInput,7),f(ht,exports.SwapType.FixedOutput,8),ht),Dt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function St(t){var e=kt(t),n=e.assetIn,r=e.assetOut;return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Et(t){if(!wt[t])throw new Error("Unknown network or network not supported: ".concat(t));return wt[t]}function Ot(t){return t[t.length-1].quote.amount_out}function Nt(t){return t[0].quote.amount_in}function kt(t){return{assetIn:Nt(t),assetOut:Ot(t)}}function Pt(t){return Lt.apply(this,arguments)}function Lt(){return(Lt=c(i().mark((function e(n){var s,a,o,u,c,p,l;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.routerAppID,o=n.assetIDs,u=n.accountAddress,e.next=3,s.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:a,appArgs:[_t.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ft(t){return Rt.apply(this,arguments)}function Rt(){return(Rt=c(i().mark((function e(n){var s,a,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,D,E,O;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.initiatorAddr,a=n.client,o=n.network,u=n.swapType,c=n.route,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Nt(c).amount),Number(Ot(c).amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Et(o),_=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(w=r.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:v,appArgs:[yt,vt[u],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(bt[u]+1),b=[_,w],e.next=15,Bt({client:a,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((D=e.sent).length>0)){e.next=21;break}return e.next=19,Pt({client:a,accountAddress:s,assetIDs:D,routerAppID:v});case 19:E=e.sent,b.unshift(E);case 21:return O=r.default.assignGroupID(b),e.abrupt("return",O.map((function(t){return{txn:t,signers:[s]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bt(t){return Mt.apply(this,arguments)}function Mt(){return(Mt=c(i().mark((function e(n){var r,s,a,o,u,c,p;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,a=n.assetIDs,o=t.getApplicationAddress(s),e.next=4,r.accountInformation(o).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),p=a.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ct(t){return Vt.apply(this,arguments)}function Vt(){return(Vt=c(i().mark((function t(e){var n,r,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,a=e.swapType,o=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:a,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(Dt[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}var Qt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Xt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},jt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),qt=new(function(n){m(s,jt);var r=y(s);function s(t,e){var n;return p(this,s),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(s,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),s=v(M(n.asset1ID,n.asset2ID),2),a=s[0],o=s[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:a,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(i().mark((function t(s,a){var u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:u=t.sent,s(o(o({},u),{},{minimum_required_balance:Kt(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),a(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Yt(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Kt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Ht,Zt,$t=ut("e");function te(t){return ee.apply(this,arguments)}function ee(){return(ee=c(i().mark((function n(s){var a,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w,b,S;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.pool,u=s.accountAddr,n.next=3,a.accountInformation(u).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=Q({stateArray:A}),T=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,$t,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,$t,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,$t,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[y],b=h[v],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ne(){return(ne=c(i().mark((function n(s){var a,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.accountAddr,u=s.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==u})),d=[],l&&l["key-value"])for(f=Q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mae)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ae,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ce(){return ce=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d=arguments;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(a=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",a[r]);case 4:return t.next=6,Jt(n,r);case 6:return o=t.sent,u=Yt(o,dt(s,S.V1_1)),c=null,u&&(l=o["created-assets"][0],p=l.index,c={asset1ID:u[re[S.V1_1].asset1],asset2ID:u[re[S.V1_1].asset2],poolTokenID:p},a[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ce.apply(this,arguments)}var pe=Object.freeze({__proto__:null,getPoolInfo:oe,getPoolReserves:function(t,e){return ue.apply(this,arguments)},getPoolAssets:function(t){return ce.apply(this,arguments)}});function le(t){return de.apply(this,arguments)}function de(){return(de=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,a=e.asset2ID,o=zt(S.V2),u=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=u.address(),l=M(s,a),t.next=8,Jt(n,p);case 8:return d=t.sent,f=Yt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[re.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[re.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[re.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[re.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[re.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[re.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[re.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[re.v2.totalFeeShare]),m.poolTokenID=Number(f[re.v2.poolTokenID]),m.asset1ID=Number(f[re.v2.asset1]),m.asset2ID=Number(f[re.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fe(){return(fe=c(i().mark((function t(e,n){var r,s,a;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Jt(e,n.account.address());case 2:return r=t.sent,s=Yt(r,n.validatorAppID),a={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(a.asset1=BigInt(s[re.v2.asset1Reserves]),a.asset2=BigInt(s[re.v2.asset2Reserves]),a.issuedLiquidity=BigInt(s[re.v2.issuedPoolTokens])),t.abrupt("return",a);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function me(){return(me=c(i().mark((function t(e){var n,r,s,a,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,Jt(n,r);case 3:return a=t.sent,o=Yt(a,dt(s,S.V2)),u=null,o&&(u={asset1ID:o[re[S.V2].asset1],asset2ID:o[re[S.V2].asset2],poolTokenID:o[re[S.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ge=Object.freeze({__proto__:null,getPoolInfo:le,getPoolReserves:function(t,e){return fe.apply(this,arguments)},getPoolAssets:function(t){return me.apply(this,arguments)}});function xe(t){return Boolean(t&&!(t.asset1+t.asset2))}var Ie,Ae,he=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=xe(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:xe,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([oe(t),le(t)])}}),Te=o((f(Ie={},S.V1_1,o(o({},pe),he)),f(Ie,S.V2,o(o({},ge),he)),Ie),he);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Ae||(Ae={}));var ye=96e4,ve=859e3;function _e(){return(_e=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b,D,E,N,k;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.asset_1,o=e.asset_2,u=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(a,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(s,S.V1_1),T=qt.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:x}),y=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[ut("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:we(A),suggestedParams:c}),(E=[])[Ae.FUNDING_TXN]=D,E[Ae.VALIDATOR_APP_CALL]=_,E[Ae.POOL_TOKEN_CREATE]=w,E[Ae.ASSET1_OPT_IN]=b,A||(E[Ae.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),k=[{txn:N[Ae.FUNDING_TXN],signers:[u]},{txn:N[Ae.VALIDATOR_APP_CALL],signers:[y]},{txn:N[Ae.POOL_TOKEN_CREATE],signers:[y]},{txn:N[Ae.ASSET1_OPT_IN],signers:[y]}],N[Ae.ASSET2_OPT_IN]&&k.push({txn:N[Ae.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",k);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function we(t){return t?ye:ve}function be(){return(be=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,u),f=v(d,2),m=f[0],g=f[1],x=qt.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Ae.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),s=n.txID,a=n.blob;return I.push(s),a})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function De(t){return Se.apply(this,arguments)}function Se(){return(Se=c(i().mark((function t(e){var n,r,s,a,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,s[Ae.POOL_TOKEN_CREATE]);case 6:if(a=t.sent,"number"==typeof(o=a["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Ee(){return(Ee=c(i().mark((function t(e){var n,r,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,De({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",Te.v1_1.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Oe,Ne={generateTxns:function(t){return _e.apply(this,arguments)},signTxns:function(t){return be.apply(this,arguments)},execute:function(t){return Ee.apply(this,arguments)},getBootstrapFundingTxnAmount:we};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Oe||(Oe={}));var ke=5,Pe=6;function Le(t,e){return Fe.apply(this,arguments)}function Fe(){return(Fe=c(i().mark((function e(n,r){var s,a,o;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(a=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,a,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Re(t,e){return Be.apply(this,arguments)}function Be(){return(Be=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Le(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Me(){return(Me=c(i().mark((function e(n){var s,a,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,_,w,b;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.network,o=n.asset_1,u=n.asset_2,c=n.initiatorAddr,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=dt(a,S.V2),d=t.getApplicationAddress(l),f=B(o,u),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,Te.v2.getPoolInfo({client:s,network:a,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(u.unit_name," already exists"));case 12:return I=Wt.generateLogicSigAccountForPool({network:a,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[ut("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ve(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Ce(h),suggestedParams:p}),(_=[])[Oe.FUNDING_TXN]=y,_[Oe.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(b=[])[Oe.FUNDING_TXN]={txn:w[Oe.FUNDING_TXN],signers:[c]},b[Oe.VALIDATOR_APP_CALL]={txn:w[Oe.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ce(t){return function(t,e){var n=zt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ve(t)+1e5}function Ve(e){return((e?ke:Pe)+1)*t.ALGORAND_MIN_TX_FEE}function Qe(){return(Qe=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,u),f=v(d,2),m=f[0],g=f[1],x=Wt.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Oe.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),s=n.txID,a=n.blob;return I.push(s),a})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Le(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(re.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Te.v2.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var je,qe={generateTxns:function(t){return Me.apply(this,arguments)},signTxns:function(t){return Qe.apply(this,arguments)},execute:function(t){return Xe.apply(this,arguments)},getBootstrapFundingTxnAmount:Ce,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Ce(e)}};var Ue,Ge=(f(je={},S.V1_1,Ne),f(je,S.V2,qe),f(je,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Ne.generateTxns(t):qe.generateTxns(t)})),f(je,"signTxns",(function(t){return t.contractVersion===S.V1_1?Ne.signTxns(t):qe.signTxns(t)})),f(je,"execute",(function(t){return t.contractVersion===S.V1_1?Ne.execute(t):qe.execute(t)})),f(je,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Ne.getBootstrapFundingTxnAmount(n):qe.getBootstrapFundingTxnAmount(n)})),je);exports.V1_1AddLiquidityTxnIndices=void 0,(Ue=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ue.FEE_TXN=0]="FEE_TXN",Ue[Ue.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ue[Ue.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ue[Ue.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ue[Ue.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var We,ze,Je,Ye,Ke=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(Ye=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",Ye.FLEXIBLE="flexible",Ye.INITIAL="initial";var He,Ze,$e=(f(We={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(We,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(We,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),We),tn=(f(ze={},exports.V2AddLiquidityType.INITIAL,1),f(ze,exports.V2AddLiquidityType.SINGLE,2),f(ze,exports.V2AddLiquidityType.FLEXIBLE,2),ze),en=(f(Je={},exports.V2AddLiquidityType.INITIAL,3),f(Je,exports.V2AddLiquidityType.FLEXIBLE,3),f(Je,exports.V2AddLiquidityType.SINGLE,2),Je);function nn(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function rn(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,s=t.assetOut,a=nn({assetIn:r,assetOut:s}),o=Z(s.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(a/o-1))}function sn(t){var e,n,r,s=t.reserves,a=t.totalFeeShare,o=t.asset1,i=t.asset2,u=s.asset1*s.asset2,c=s.asset1+BigInt(o.amount),p=s.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*s.issuedLiquidity*s.issuedLiquidity/u))))),f=d-s.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(x>I){var A=x;r=an(A,a),e={id:o.id,amount:A+r,decimals:o.decimals,reserves:s.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:s.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=an(h,a),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:s.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:o.decimals,reserves:s.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:rn({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function an(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function on(e){return(tn[e]+1)*t.ALGORAND_MIN_TX_FEE}exports.SwapQuoteType=void 0,(He=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",He.Router="router";var un=ut("add_liquidity"),cn=(f(Ze={},S.V1_1,[ut("mint")]),f(Ze,S.V2,{INITIAL_LIQUIDITY:[ut("add_initial_liquidity")],SINGLE_ASSET_MODE:[un,ut("single")],FLEXIBLE_MODE:[un,ut("flexible")]}),Ze);function pn(){return(pn=c(i().mark((function t(e){var n,a,o,u,c,p,l,d,f,m,g,x,I,A,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,o=e.poolAddress,u=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,S.V1_1),appArgs:cn.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[u.id,p.id]:[u.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:s,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ln(){return(ln=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,o=n.account,t.next=4,a([s]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dn(){return(dn=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.signedTxns,o=e.initiatorAddr,t.prev=1,u=BigInt(s[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,te({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[a]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=at(s),x=ot(s),t.next=17,te({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var fn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,s=t.asset2In;if(0n===n.issuedLiquidity){var a=BigInt(Math.floor(Math.sqrt(Number(r)*Number(s))));if(a<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(a,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(s),poolTokenID:e.poolTokenID,poolTokenOut:a-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(s)*n.issuedLiquidity/n.asset2,u=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Rn(){return(Rn=c(i().mark((function t(e){var n,s,a,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Ft(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,s=e.initiatorAddr,a=e.slippage,u=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=C(f),h=u===exports.SwapType.FixedInput?d.assetInAmount:H("positive",a,d.assetInAmount),T=u===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",a,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:l.validatorAppID,appArgs:[yt,vt[u],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Bn(u),(_=[])[It.INPUT_TXN]=y,_[It.APP_CALL_TXN]=v,w=r.default.assignGroupID(_),t.abrupt("return",[{txn:w[It.INPUT_TXN],signers:[s]},{txn:w[It.APP_CALL_TXN],signers:[s]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Bn(e){return(Tt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Mn(){return(Mn=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return s=e.client,a=e.quote,o=e.txGroup,u=e.signedTxns,t.next=3,rt(s,[u]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Re(s,o);case 10:return m=t.sent,g=a.type===exports.SwapQuoteType.Direct?{id:a.quoteWithPool.quote.assetInID,amount:a.quoteWithPool.quote.assetInAmount}:{id:Number(a.asset_in_id),amount:Number(a.route[0].quote.amount_in.amount)},x=a.type===exports.SwapQuoteType.Direct?a.quoteWithPool.quote.assetOutID:Number(a.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:a,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Cn(){return(Cn=c(i().mark((function t(e){var n,r,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.pool,s=e.asset,a=e.decimals,o=e.network,u=e.isSwapRouterEnabled,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,Vn({pool:r,assetIn:s,decimals:a,isSwapRouterEnabled:u,network:o});case 4:c=t.sent,t.next=10;break;case 7:return t.next=9,Xn({pool:r,assetOut:s,decimals:a,isSwapRouterEnabled:u,network:o});case 9:c=t.sent;case 10:return t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Vn(t){return Qn.apply(this,arguments)}function Qn(){return(Qn=c(i().mark((function t(e){var n,r,s,a,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,s=e.pool,a=e.isSwapRouterEnabled,u=e.network,s.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:s,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=s.totalFeeShare,n.id!==s.asset1ID){t.next=11;break}l=s.asset2ID,d=s.asset1Reserves,f=s.asset2Reserves,t.next=18;break;case 11:if(n.id!==s.asset2ID){t.next=17;break}l=s.asset1ID,d=s.asset2Reserves,f=s.asset1Reserves,t.next=18;break;case 17:throw new F({pool:s,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(s.account.address(),"."));case 18:if(m=qn({inputSupply:d,outputSupply:f,swapInputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapOutputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(g>f)){t.next=21;break}throw new Fn;case 21:if(A={assetInID:n.id,assetInAmount:c,assetOutID:l,assetOutAmount:g,swapFee:Number(x),rate:Z(r.assetOut,Number(g))/Z(r.assetIn,Number(c)),priceImpact:I},!a){t.next=28;break}return t.next=25,Ct({amount:n.amount,assetInID:Number(n.id),assetOutID:l,swapType:exports.SwapType.FixedInput,network:u});case 25:if(!((h=t.sent).route.length>1&&BigInt(Ot(h.route).amount)>A.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},h),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:s,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xn(t){return jn.apply(this,arguments)}function jn(){return(jn=c(i().mark((function t(e){var n,r,s,a,u,c,p,l,d,f,m,g,x,I,A,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,s=e.pool,a=e.isSwapRouterEnabled,u=e.network,s.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:s,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=s.totalFeeShare,n.id!==s.asset1ID){t.next=11;break}l=s.asset2ID,d=s.asset2Reserves,f=s.asset1Reserves,t.next=18;break;case 11:if(n.id!==s.asset2ID){t.next=17;break}l=s.asset1ID,d=s.asset1Reserves,f=s.asset2Reserves,t.next=18;break;case 17:throw new F({pool:s,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(s.account.address(),"."));case 18:if(m=Un({inputSupply:d,outputSupply:f,swapOutputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapInputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(c>f)){t.next=21;break}throw new Fn;case 21:if(A={assetInID:l,assetInAmount:g,assetOutID:n.id,assetOutAmount:c,swapFee:Number(x),rate:Z(r.assetOut,Number(c))/Z(r.assetIn,Number(g)),priceImpact:I},!a){t.next=28;break}return t.next=25,Ct({amount:n.amount,assetInID:l,assetOutID:n.id,swapType:exports.SwapType.FixedOutput,network:u});case 25:if(!((h=t.sent).route.length>1&&BigInt(Ot(h.route).amount)s+BigInt(1e3)?(n=s*e.asset1/a,r=s*e.asset2/a):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function zn(){return(zn=c(i().mark((function e(n){var s,a,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.poolTokenIn,u=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.account.address(),m=a.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[On,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[Sn.ASSET_TRANSFER_TXN]=g,I[Sn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[Sn.ASSET_TRANSFER_TXN],signers:[u]},{txn:A[Sn.APP_CALL_TXN],signers:[u]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jn(){return(Jn=c(i().mark((function e(n){var s,a,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.initiatorAddr,u=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.asset1ID,m=a.asset2ID,g=a.account.address(),x=a.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:u,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[On,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[Sn.ASSET_TRANSFER_TXN]=T,v[Sn.APP_CALL_TXN]=y,_=r.default.assignGroupID(v),e.abrupt("return",[{txn:_[Sn.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[Sn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Yn(){return(Yn=c(i().mark((function t(e){var n,r,s,a,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,s=e.txGroup,a=e.signedTxns,t.next=3,rt(r,[a]);case 3:return o=t.sent,u=v(o,1),c=u[0].txnID,t.next=8,Re(r,s);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Hn,Zn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,s=BigInt(r),a=Wn(s,n),o=a.asset1OutputAmount,i=a.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:s}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,s=t.poolTokenIn,a=t.assetOutID,o=t.decimals,i=BigInt(s),u=Wn(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(a===n.asset1ID){var d=Gn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(a!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Gn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return zn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Jn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Yn.apply(this,arguments)}},$n=(f(Kn={},S.V1_1,Ln),f(Kn,S.V2,Zn),Kn),tr=3n,er=1000n;function nr(){return(nr=c(i().mark((function t(e){var n,s,a,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,t.next=3,a([s]);case 3:return o=t.sent,u=v(o,2),c=u[0],p=u[1],l=s.map((function(t,e){return e===Hn.FEE_TXN_INDEX?c:e===Hn.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=c(i().mark((function t(e){var n,a,o,u,c,p,l,d,f,m,g,x,I,A,h,T,y,v,_,w;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,a=e.quote,o=e.swapType,u=e.slippage,c=e.initiatorAddr,p=a.pool,l=a.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:a},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[ut("swap"),o===exports.SwapType.FixedInput?ut("fi"):ut("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",u,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",u,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),_=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:s,suggestedParams:x}),w=r.default.assignGroupID([_,A,T,v]),t.abrupt("return",[{txn:w[0],signers:[c]},{txn:w[1],signers:[m]},{txn:w[2],signers:[c]},{txn:w[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sr(t){var e=t.pool,n=t.reserves,r=t.assetIn,s=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var a,i,u,c=BigInt(r.amount);if(r.id===e.asset1ID)a=e.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));a=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*tr/er,l=u-i*u/(i+(c-p));if(l>u)throw new Fn;var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:a,assetOutAmount:l,swapFee:Number(p),rate:nn(d),priceImpact:rn(o({inputSupply:i,outputSupply:u},d))};return{type:exports.SwapQuoteType.Direct,quoteWithPool:{pool:e,quote:f}}}function ar(t){return or.apply(this,arguments)}function or(){return(or=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,te({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,rt(n,[s]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,te({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ir(t){var e=t.pool,n=t.reserves,r=t.assetOut,s=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var a,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)a=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));a=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new Fn;var c=o*i/(i-u)-o,p=c*er/(er-tr),l=p-c,d=Z(s.assetOut,Number(u))/Z(s.assetIn,Number(p)),f=Z(s.assetOut,Number(i))/Z(s.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:a,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,quoteWithPool:{pool:e,quote:g}}}function ur(t){return cr.apply(this,arguments)}function cr(){return(cr=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,x,I,A;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,te({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,rt(n,[s]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,te({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,a.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:a.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pr(){return(pr=c(i().mark((function t(e){var n,r,s,a,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,s=e.swapType,a=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:s,txGroup:a},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:a[Hn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:a[Hn.ASSET_IN_TXN_INDEX].txn.amount},l={id:a[Hn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:a[Hn.ASSET_OUT_TXN_INDEX].txn.amount},s!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,ar({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,ur({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(a),fees:at(a)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Hn||(Hn={}));var lr,dr={getQuote:function(t,e,n,r,s){return t===exports.SwapType.FixedInput?sr({pool:e,reserves:n,assetIn:r,decimals:s}):ir({pool:e,reserves:n,assetOut:r,decimals:s})},getFixedInputSwapQuote:sr,getFixedOutputSwapQuote:ir,generateTxns:function(t){return rr.apply(this,arguments)},signTxns:function(t){return nr.apply(this,arguments)},execute:function(t){return pr.apply(this,arguments)},executeFixedOutputSwap:ur},fr=4*t.ALGORAND_MIN_TX_FEE;function mr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Fn})))throw new Fn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function gr(){return gr=c(i().mark((function t(e){var n,r,s,a,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,a=e.pools,o=e.isSwapRouterEnabled,u=a.map((function(t){return new Promise(function(){var a=c(i().mark((function a(u,c){var p,l;return i().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},a.prev=1,t.info.contractVersion!==S.V1_1){a.next=6;break}p=dr.getFixedInputSwapQuote(l),a.next=9;break;case 6:return a.next=8,Gn.getFixedInputSwapQuote(l);case 8:p=a.sent;case 9:u(p),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(1),c(a.t0);case 15:case"end":return a.stop()}}),a,null,[[1,12]])})));return function(t,e){return a.apply(this,arguments)}}())})),t.next=4,mr(u);case 4:return p=t.sent,t.abrupt("return",Ar(p));case 6:case"end":return t.stop()}}),t)}))),gr.apply(this,arguments)}function xr(){return xr=c(i().mark((function t(e){var n,r,s,a,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,a=e.pools,o=e.isSwapRouterEnabled,u=a.map((function(t){return new Promise(function(){var a=c(i().mark((function a(u,c){var p,l;return i().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(s.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},a.prev=1,t.info.contractVersion!==S.V1_1){a.next=6;break}p=dr.getFixedOutputSwapQuote(l),a.next=9;break;case 6:return a.next=8,Gn.getFixedOutputSwapQuote(l);case 8:p=a.sent;case 9:u(p),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(1),c(a.t0);case 15:case"end":return a.stop()}}),a,null,[[1,12]])})));return function(t,e){return a.apply(this,arguments)}}())})),t.next=4,mr(u);case 4:return p=t.sent,t.abrupt("return",Ar(p));case 6:case"end":return t.stop()}}),t)}))),xr.apply(this,arguments)}function Ir(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:St(t.route)}function Ar(t){var e,n=t[0],r=Ir(n),s=D(t);try{for(s.s();!(e=s.n()).done;){var a=e.value,o=Ir(a);o>r&&(n=a,r=o)}}catch(t){s.e(t)}finally{s.f()}return n}function hr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var Tr=(f(lr={},S.V1_1,dr),f(lr,S.V2,Gn),f(lr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return xe(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return gr.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return xr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(lr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&hr(t.quote)===S.V1_1?dr.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):Gn.generateTxns(t)})),f(lr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&hr(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return dr.signTxns(o({pool:e},t))}return Gn.signTxns(t)})),f(lr,"execute",(function(t){return t.contractVersion===S.V1_1?dr.execute(t):Gn.execute(t)})),lr);function yr(){return(yr=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.initiatorSigner,t.prev=1,t.next=4,vr({txGroup:s,pool:r,initiatorSigner:a});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return u=t.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:at(s),confirmedRound:d,txnID:l,groupID:ot(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function vr(t){return _r.apply(this,arguments)}function _r(){return(_r=c(i().mark((function t(e){var n,s,a,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.pool,a=e.initiatorSigner,t.next=3,a([n]);case 3:return o=t.sent,u=v(o,1),c=u[0],p=s.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function wr(){return wr=c(i().mark((function t(e){var n,s,a,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.data,a=e.initiatorSigner,t.prev=1,o=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:at(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,a(o.map((function(t){return t.txns})));case 5:return u=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var s=c(i().mark((function s(a,o){var c,p,l,d,f,m;return i().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:return s.prev=0,c=t.txns.map((function(n,s){return 0===s?u[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),s.next=4,rt(n,[c]);case 4:p=s.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,a({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(0),o(s.t0);case 15:case"end":return s.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),wr.apply(this,arguments)}function br(){return(br=c(i().mark((function t(e){var n,a,o,u,c,p,l,d,f,m,g;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.pool,o=e.assetID,u=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=a.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:a.validatorAppID,appArgs:[ut("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[c],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(u),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(u),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:s,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Dr(e){var n=e.suggestedParams,r=e.stakingAppID,s=e.initiatorAddr,a=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:s,suggestedParams:n,foreignAssets:[a],accounts:[o.accountAddress],appArgs:[ut("commit"),u],note:X([ut("tinymanStaking/v1:b"),c,t.encodeUint64(a),u])})}function Sr(){return(Sr=c(i().mark((function e(n){var r,s,a,o,u,c,p,l,d,f,m;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,a=n.program,o=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Dr({suggestedParams:l,stakingAppID:s,program:a,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[o],accounts:[a.accountAddress],appArgs:[ut("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Dn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Ge,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=$n,exports.Swap=Tr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=En,exports.V2AddLiquidityTxnIndices=$e,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=Kt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=Te,exports.prepareCommitTransactions=function(t){return Sr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return yr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=at,exports.tinymanContract_v1_1=qt,exports.tinymanContract_v2=Wt,exports.tinymanJSSDKConfig=pt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},s=r.iterator||"@@iterator",a=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var s=e&&e.prototype instanceof d?e:d,a=Object.create(s.prototype),o=new _(r||[]);return a._invoke=function(t,e,n){var r="suspendedStart";return function(s,a){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===s)throw a;return D()}for(n.method=s,n.arg=a;;){var o=n.delegate;if(o){var u=y(o,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,o),a}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,s,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,s)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(s,a,o,u){var i=p(t[s],t,a);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,u)}),(function(t){r("throw",t,o,u)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,u)}))}u(i.arg)}var s;this._invoke=function(t,n){function a(){return new e((function(e,s){r(t,n,e,s)}))}return s=s?s.then(a,a):a()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var s=r.arg;return s?s.done?(e[t.resultName]=s.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):s:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function w(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function _(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[s];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,a=function e(){for(;++r=0;--s){var a=this.tryEntries[s],o=a.completion;if("root"===a.tryLoc)return r("end");if(a.tryLoc<=this.prev){var u=n.call(a,"catchLoc"),i=n.call(a,"finallyLoc");if(u&&i){if(this.prev=0;--r){var s=this.tryEntries[r];if(s.tryLoc<=this.prev&&n.call(s,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),w(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var s=r.arg;w(n)}return s}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,s,a,o){try{var u=t[a](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,s)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,s){var a=t.apply(e,n);function o(t){i(a,r,s,o,u,"next",t)}function u(t){i(a,r,s,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:s}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,a=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw a}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var s;p(this,n);for(var a=arguments.length,o=new Array(a>2?a-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function V(t){return 0===Number(t)}function C(t){return Number(t.id)}function Q(t){var e,n=t.stateArray,r=void 0===n?[]:n,s=t.shouldDecodeKeys,a=void 0!==s&&s,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[a?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function q(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),s=0,a=D(t);try{for(a.s();!(e=a.n()).done;){var o=e.value;r.set(o,s),s+=o.length}}catch(t){a.e(t)}finally{a.f()}return r}var X=100000n,j=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var s=t["apps-local-state"]||[],a=t["created-apps"]||[],o=t.assets||[];return X+j*BigInt(o.length)+U*BigInt(a.length+s.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var s="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*s))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var s=v(nt(e),2),a=s[0],o=s[1],u=v(nt(Math.round(Number(et(a,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return st.apply(this,arguments)}function st(){return(st=c(u().mark((function t(e,n){var r,s,a,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=D(n),t.prev=3,s.s();case 5:if((a=s.n()).done){t.next=18;break}return o=a.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function at(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(s,a),note:pt.getAppCallTxnNoteWithClientName(a),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(s,a),note:pt.getAppCallTxnNoteWithClientName(a),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht,Tt,yt=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),vt=it("swap"),wt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),_t={ASSET_OPT_IN:it("asset_opt_in")},bt={testnet:159521633,mainnet:0},Dt=(f(ht={},exports.SwapType.FixedInput,7),f(ht,exports.SwapType.FixedOutput,8),ht),St={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function Et(t){var e=kt(t),n=e.assetIn,r=e.assetOut;return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Ot(t){if(!bt[t])throw new Error("Unknown network or network not supported: ".concat(t));return bt[t]}function Nt(t){return t[t.length-1].quote.amount_out}function Pt(t){return t[0].quote.amount_in}function kt(t){return{assetIn:Pt(t),assetOut:Nt(t)}}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function e(n){var s,a,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,s.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:a,appArgs:[_t.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Rt(t){return Bt.apply(this,arguments)}function Bt(){return(Bt=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_,b,D,E,O;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.initiatorAddr,a=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=[C(c[0].quote.amount_in.asset),C(c[0].quote.amount_out.asset),C(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Pt(c).amount),Number(Nt(c).amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=V(d),v=Ot(o),w=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(_=r.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:v,appArgs:[vt,wt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Dt[i]+1),b=[w,_],e.next=15,Mt({client:a,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((D=e.sent).length>0)){e.next=21;break}return e.next=19,Lt({client:a,accountAddress:s,assetIDs:D,routerAppID:v});case 19:E=e.sent,b.unshift(E);case 21:return O=r.default.assignGroupID(b),e.abrupt("return",O.map((function(t){return{txn:t,signers:[s]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mt(t){return Vt.apply(this,arguments)}function Vt(){return(Vt=c(u().mark((function e(n){var r,s,a,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,a=n.assetIDs,o=t.getApplicationAddress(s),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=a.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ct(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function t(e){var n,r,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,a=e.swapType,o=e.network,i={asset_in_id:String(r),asset_out_id:String(s),swap_type:a,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(St[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}function qt(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function Xt(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,s=t.assetOut,a=qt({assetIn:r,assetOut:s}),o=Z(s.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(a/o-1))}function jt(t){if(t.type===exports.SwapQuoteType.Router){var e=Pt(t.route);return{id:C(e.asset),amount:Number(e.amount)}}return{id:t.quoteWithPool.quote.assetInID,amount:t.quoteWithPool.quote.assetInAmount}}function Ut(t){if(t.type===exports.SwapQuoteType.Router){var e=Nt(t.route);return{id:C(e.asset),amount:Number(e.amount)}}return{id:t.quoteWithPool.quote.assetOutID,amount:t.quoteWithPool.quote.assetOutAmount}}exports.SwapQuoteType=void 0,(Tt=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",Tt.Router="router";var Gt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Wt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},zt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Jt=new(function(n){m(s,zt);var r=y(s);function s(t,e){var n;return p(this,s),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(s,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),s=v(M(n.asset1ID,n.asset2ID),2),a=s[0],o=s[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:a,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(s,a){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,s(o(o({},i),{},{minimum_required_balance:ee(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),a(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ee(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ne,re,se=it("e");function ae(t){return oe.apply(this,arguments)}function oe(){return(oe=c(u().mark((function n(s){var a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.pool,i=s.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=Q({stateArray:A}),T=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.poolTokenID)])),w=h[T],_=h[y],b=h[v],"bigint"==typeof w&&(l=w),"bigint"==typeof _&&(d=_),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ue(){return(ue=c(u().mark((function n(s){var a,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.accountAddr,i=s.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=Q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mpe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(pe,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function me(){return me=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(a=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",a[r]);case 4:return t.next=6,$t(n,r);case 6:return o=t.sent,i=te(o,dt(s,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[ie[S.V1_1].asset1],asset2ID:i[ie[S.V1_1].asset2],poolTokenID:p},a[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),me.apply(this,arguments)}var ge=Object.freeze({__proto__:null,getPoolInfo:le,getPoolReserves:function(t,e){return fe.apply(this,arguments)},getPoolAssets:function(t){return me.apply(this,arguments)}});function xe(t){return Ie.apply(this,arguments)}function Ie(){return(Ie=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,a=e.asset2ID,o=Zt(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(s,a),t.next=8,$t(n,p);case 8:return d=t.sent,f=te(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[ie.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ie.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ie.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ie.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ie.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ie.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ie.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ie.v2.totalFeeShare]),m.poolTokenID=Number(f[ie.v2.poolTokenID]),m.asset1ID=Number(f[ie.v2.asset1]),m.asset2ID=Number(f[ie.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ae(){return(Ae=c(u().mark((function t(e,n){var r,s,a;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,$t(e,n.account.address());case 2:return r=t.sent,s=te(r,n.validatorAppID),a={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(a.asset1=BigInt(s[ie.v2.asset1Reserves]),a.asset2=BigInt(s[ie.v2.asset2Reserves]),a.issuedLiquidity=BigInt(s[ie.v2.issuedPoolTokens])),t.abrupt("return",a);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function he(){return(he=c(u().mark((function t(e){var n,r,s,a,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,$t(n,r);case 3:return a=t.sent,o=te(a,dt(s,S.V2)),i=null,o&&(i={asset1ID:o[ie[S.V2].asset1],asset2ID:o[ie[S.V2].asset2],poolTokenID:o[ie[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Te=Object.freeze({__proto__:null,getPoolInfo:xe,getPoolReserves:function(t,e){return Ae.apply(this,arguments)},getPoolAssets:function(t){return he.apply(this,arguments)}});function ye(t){return Boolean(t&&!(t.asset1+t.asset2))}var ve,we,_e=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ye(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:ye,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([le(t),xe(t)])}}),be=o((f(ve={},S.V1_1,o(o({},ge),_e)),f(ve,S.V2,o(o({},Te),_e)),ve),_e);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(we||(we={}));var De=96e4,Se=859e3;function Ee(){return(Ee=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,b,D,E,N,P;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(a,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=V(x),h=dt(s,S.V1_1),T=Jt.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:x}),y=T.address(),w=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),_=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Oe(A),suggestedParams:c}),(E=[])[we.FUNDING_TXN]=D,E[we.VALIDATOR_APP_CALL]=w,E[we.POOL_TOKEN_CREATE]=_,E[we.ASSET1_OPT_IN]=b,A||(E[we.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),P=[{txn:N[we.FUNDING_TXN],signers:[i]},{txn:N[we.VALIDATOR_APP_CALL],signers:[y]},{txn:N[we.POOL_TOKEN_CREATE],signers:[y]},{txn:N[we.ASSET1_OPT_IN],signers:[y]}],N[we.ASSET2_OPT_IN]&&P.push({txn:N[we.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",P);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Oe(t){return t?De:Se}function Ne(){return(Ne=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Jt.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===we.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),s=n.txID,a=n.blob;return I.push(s),a})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pe(t){return ke.apply(this,arguments)}function ke(){return(ke=c(u().mark((function t(e){var n,r,s,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,s[we.POOL_TOKEN_CREATE]);case 6:if(a=t.sent,"number"==typeof(o=a["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Le(){return(Le=c(u().mark((function t(e){var n,r,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Pe({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",be.v1_1.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Fe,Re={generateTxns:function(t){return Ee.apply(this,arguments)},signTxns:function(t){return Ne.apply(this,arguments)},execute:function(t){return Le.apply(this,arguments)},getBootstrapFundingTxnAmount:Oe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Fe||(Fe={}));var Be=5,Me=6;function Ve(t,e){return Ce.apply(this,arguments)}function Ce(){return(Ce=c(u().mark((function e(n,r){var s,a,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(a=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,a,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Qe(t,e){return qe.apply(this,arguments)}function qe(){return(qe=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Ve(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=dt(a,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,be.v2.getPoolInfo({client:s,network:a,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Ht.generateLogicSigAccountForPool({network:a,asset1ID:g,asset2ID:x}),A=I.address(),h=V(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ue(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:je(h),suggestedParams:p}),(w=[])[Fe.FUNDING_TXN]=y,w[Fe.VALIDATOR_APP_CALL]=T,_=r.default.assignGroupID(w),(b=[])[Fe.FUNDING_TXN]={txn:_[Fe.FUNDING_TXN],signers:[c]},b[Fe.VALIDATOR_APP_CALL]={txn:_[Fe.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function je(t){return function(t,e){var n=Zt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ue(t)+1e5}function Ue(e){return((e?Be:Me)+1)*t.ALGORAND_MIN_TX_FEE}function Ge(){return(Ge=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Ht.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Fe.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),s=n.txID,a=n.blob;return I.push(s),a})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function We(){return(We=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Ve(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ie.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",be.v2.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var ze,Je={generateTxns:function(t){return Xe.apply(this,arguments)},signTxns:function(t){return Ge.apply(this,arguments)},execute:function(t){return We.apply(this,arguments)},getBootstrapFundingTxnAmount:je,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+je(e)}};var Ye,Ke=(f(ze={},S.V1_1,Re),f(ze,S.V2,Je),f(ze,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Re.generateTxns(t):Je.generateTxns(t)})),f(ze,"signTxns",(function(t){return t.contractVersion===S.V1_1?Re.signTxns(t):Je.signTxns(t)})),f(ze,"execute",(function(t){return t.contractVersion===S.V1_1?Re.execute(t):Je.execute(t)})),f(ze,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Re.getBootstrapFundingTxnAmount(n):Je.getBootstrapFundingTxnAmount(n)})),ze);exports.V1_1AddLiquidityTxnIndices=void 0,(Ye=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ye.FEE_TXN=0]="FEE_TXN",Ye[Ye.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ye[Ye.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ye[Ye.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ye[Ye.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var He,Ze,$e,tn,en=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(tn=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",tn.FLEXIBLE="flexible",tn.INITIAL="initial";var nn,rn=(f(He={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(He,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(He,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),He),sn=(f(Ze={},exports.V2AddLiquidityType.INITIAL,1),f(Ze,exports.V2AddLiquidityType.SINGLE,2),f(Ze,exports.V2AddLiquidityType.FLEXIBLE,2),Ze),an=(f($e={},exports.V2AddLiquidityType.INITIAL,3),f($e,exports.V2AddLiquidityType.FLEXIBLE,3),f($e,exports.V2AddLiquidityType.SINGLE,2),$e);function on(t){var e,n,r,s=t.reserves,a=t.totalFeeShare,o=t.asset1,u=t.asset2,i=s.asset1*s.asset2,c=s.asset1+BigInt(o.amount),p=s.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*s.issuedLiquidity*s.issuedLiquidity/i))))),f=d-s.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(o.amount)-m,I=BigInt(u.amount)-g;if(x>I){var A=x;r=un(A,a),e={id:o.id,amount:A+r,decimals:o.decimals,reserves:s.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:u.decimals,reserves:s.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=un(h,a),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:s.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:o.decimals,reserves:s.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Xt({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function un(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function cn(e){return(sn[e]+1)*t.ALGORAND_MIN_TX_FEE}var pn=it("add_liquidity"),ln=(f(nn={},S.V1_1,[it("mint")]),f(nn,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[pn,it("single")],FLEXIBLE_MODE:[pn,it("flexible")]}),nn);function dn(){return(dn=c(u().mark((function t(e){var n,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,S.V1_1),appArgs:ln.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:s,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fn(){return(fn=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,o=n.account,t.next=4,a([s]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=s.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(){return(mn=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(s[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,ae({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[a]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=at(s),x=ot(s),t.next=17,ae({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var gn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,s=t.asset2In;if(0n===n.issuedLiquidity){var a=BigInt(Math.floor(Math.sqrt(Number(r)*Number(s))));if(a<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(a,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(s),poolTokenID:e.poolTokenID,poolTokenOut:a-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(s)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Mn(){return(Mn=c(u().mark((function t(e){var n,s,a,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Rt(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,s=e.initiatorAddr,a=e.slippage,i=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=V(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:H("positive",a,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",a,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:l.validatorAppID,appArgs:[vt,wt[i],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Vn(i),(w=[])[It.INPUT_TXN]=y,w[It.APP_CALL_TXN]=v,_=r.default.assignGroupID(w),t.abrupt("return",[{txn:_[It.INPUT_TXN],signers:[s]},{txn:_[It.APP_CALL_TXN],signers:[s]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Vn(e){return(yt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Cn(){return(Cn=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return s=e.client,a=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,rt(s,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Qe(s,o);case 10:return m=t.sent,g=a.type===exports.SwapQuoteType.Direct?{id:a.quoteWithPool.quote.assetInID,amount:a.quoteWithPool.quote.assetInAmount}:{id:Number(a.asset_in_id),amount:Number(a.route[0].quote.amount_in.amount)},x=a.type===exports.SwapQuoteType.Direct?a.quoteWithPool.quote.assetOutID:Number(a.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:a,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Qn(){return(Qn=c(u().mark((function t(e){var n,r,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.pool,s=e.asset,a=e.decimals,o=e.network,i=e.isSwapRouterEnabled,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,qn({pool:r,assetIn:s,decimals:a,isSwapRouterEnabled:i,network:o});case 4:c=t.sent,t.next=10;break;case 7:return t.next=9,jn({pool:r,assetOut:s,decimals:a,isSwapRouterEnabled:i,network:o});case 9:c=t.sent;case 10:return t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function qn(t){return Xn.apply(this,arguments)}function Xn(){return(Xn=c(u().mark((function t(e){var n,r,s,a,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,s=e.pool,a=e.isSwapRouterEnabled,i=e.network,s.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:s,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=s.totalFeeShare,n.id!==s.asset1ID){t.next=11;break}l=s.asset2ID,d=s.asset1Reserves,f=s.asset2Reserves,t.next=18;break;case 11:if(n.id!==s.asset2ID){t.next=17;break}l=s.asset1ID,d=s.asset2Reserves,f=s.asset1Reserves,t.next=18;break;case 17:throw new F({pool:s,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(s.account.address(),"."));case 18:if(m=Gn({inputSupply:d,outputSupply:f,swapInputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapOutputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(g>f)){t.next=21;break}throw new Bn;case 21:if(A={assetInID:n.id,assetInAmount:c,assetOutID:l,assetOutAmount:g,swapFee:Number(x),rate:Z(r.assetOut,Number(g))/Z(r.assetIn,Number(c)),priceImpact:I},!a){t.next=28;break}return t.next=25,Ct({amount:n.amount,assetInID:Number(n.id),assetOutID:l,swapType:exports.SwapType.FixedInput,network:i});case 25:if(!((h=t.sent).route.length>1&&BigInt(Nt(h.route).amount)>A.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},h),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:s,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function jn(t){return Un.apply(this,arguments)}function Un(){return(Un=c(u().mark((function t(e){var n,r,s,a,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,s=e.pool,a=e.isSwapRouterEnabled,i=e.network,s.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:s,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=s.totalFeeShare,n.id!==s.asset1ID){t.next=11;break}l=s.asset2ID,d=s.asset2Reserves,f=s.asset1Reserves,t.next=18;break;case 11:if(n.id!==s.asset2ID){t.next=17;break}l=s.asset1ID,d=s.asset1Reserves,f=s.asset2Reserves,t.next=18;break;case 17:throw new F({pool:s,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(s.account.address(),"."));case 18:if(m=Wn({inputSupply:d,outputSupply:f,swapOutputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapInputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(c>f)){t.next=21;break}throw new Bn;case 21:if(A={assetInID:l,assetInAmount:g,assetOutID:n.id,assetOutAmount:c,swapFee:Number(x),rate:Z(r.assetOut,Number(c))/Z(r.assetIn,Number(g)),priceImpact:I},!a){t.next=28;break}return t.next=25,Ct({amount:n.amount,assetInID:l,assetOutID:n.id,swapType:exports.SwapType.FixedOutput,network:i});case 25:if(!((h=t.sent).route.length>1&&BigInt(Nt(h.route).amount)s+BigInt(1e3)?(n=s*e.asset1/a,r=s*e.asset2/a):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Yn(){return(Yn=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.account.address(),m=a.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Pn,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[On.ASSET_TRANSFER_TXN]=g,I[On.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[On.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[On.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kn(){return(Kn=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.asset1ID,m=a.asset2ID,g=a.account.address(),x=a.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Pn,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[On.ASSET_TRANSFER_TXN]=T,v[On.APP_CALL_TXN]=y,w=r.default.assignGroupID(v),e.abrupt("return",[{txn:w[On.ASSET_TRANSFER_TXN],signers:[o]},{txn:w[On.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function t(e){var n,r,s,a,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,s=e.txGroup,a=e.signedTxns,t.next=3,rt(r,[a]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Qe(r,s);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Zn,$n,tr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,s=BigInt(r),a=Jn(s,n),o=a.asset1OutputAmount,u=a.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:s}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,s=t.poolTokenIn,a=t.assetOutID,o=t.decimals,u=BigInt(s),i=Jn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(a===n.asset1ID){var d=zn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(a!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=zn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Yn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Kn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Hn.apply(this,arguments)}},er=(f(Zn={},S.V1_1,Rn),f(Zn,S.V2,tr),Zn),nr=3n,rr=1000n;function sr(){return(sr=c(u().mark((function t(e){var n,s,a,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,t.next=3,a([s]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=s.map((function(t,e){return e===$n.FEE_TXN_INDEX?c:e===$n.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,a=e.quote,o=e.swapType,i=e.slippage,c=e.initiatorAddr,p=a.pool,l=a.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:a},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",i,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",i,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),w=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:s,suggestedParams:x}),_=r.default.assignGroupID([w,A,T,v]),t.abrupt("return",[{txn:_[0],signers:[c]},{txn:_[1],signers:[m]},{txn:_[2],signers:[c]},{txn:_[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(t){var e=t.pool,n=t.reserves,r=t.assetIn,s=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var a,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)a=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));a=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*nr/rr,l=i-u*i/(u+(c-p));if(l>i)throw new Bn;var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:a,assetOutAmount:l,swapFee:Number(p),rate:qt(d),priceImpact:Xt(o({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,quoteWithPool:{pool:e,quote:f}}}function ur(t){return ir.apply(this,arguments)}function ir(){return(ir=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,ae({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[s]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,ae({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cr(t){var e=t.pool,n=t.reserves,r=t.assetOut,s=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var a,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)a=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));a=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new Bn;var c=o*u/(u-i)-o,p=c*rr/(rr-nr),l=p-c,d=Z(s.assetOut,Number(i))/Z(s.assetIn,Number(p)),f=Z(s.assetOut,Number(u))/Z(s.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:a,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,quoteWithPool:{pool:e,quote:g}}}function pr(t){return lr.apply(this,arguments)}function lr(){return(lr=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,ae({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[s]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,ae({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,a.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:a.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dr(){return(dr=c(u().mark((function t(e){var n,r,s,a,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,s=e.swapType,a=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:s,txGroup:a},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:a[$n.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:a[$n.ASSET_IN_TXN_INDEX].txn.amount},l={id:a[$n.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:a[$n.ASSET_OUT_TXN_INDEX].txn.amount},s!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,ur({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,pr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(a),fees:at(a)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}($n||($n={}));var fr,mr={getQuote:function(t,e,n,r,s){return t===exports.SwapType.FixedInput?or({pool:e,reserves:n,assetIn:r,decimals:s}):cr({pool:e,reserves:n,assetOut:r,decimals:s})},getFixedInputSwapQuote:or,getFixedOutputSwapQuote:cr,generateTxns:function(t){return ar.apply(this,arguments)},signTxns:function(t){return sr.apply(this,arguments)},execute:function(t){return dr.apply(this,arguments)},executeFixedOutputSwap:pr},gr=4*t.ALGORAND_MIN_TX_FEE;function xr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Bn})))throw new Bn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function Ir(){return Ir=c(u().mark((function t(e){var n,r,s,a,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,a=e.pools,o=e.isSwapRouterEnabled,i=a.map((function(t){return new Promise(function(){var a=c(u().mark((function a(i,c){var p,l;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},a.prev=1,t.info.contractVersion!==S.V1_1){a.next=6;break}p=mr.getFixedInputSwapQuote(l),a.next=9;break;case 6:return a.next=8,zn.getFixedInputSwapQuote(l);case 8:p=a.sent;case 9:i(p),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(1),c(a.t0);case 15:case"end":return a.stop()}}),a,null,[[1,12]])})));return function(t,e){return a.apply(this,arguments)}}())})),t.next=4,xr(i);case 4:return p=t.sent,t.abrupt("return",Tr(p));case 6:case"end":return t.stop()}}),t)}))),Ir.apply(this,arguments)}function Ar(){return Ar=c(u().mark((function t(e){var n,r,s,a,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,a=e.pools,o=e.isSwapRouterEnabled,i=a.map((function(t){return new Promise(function(){var a=c(u().mark((function a(i,c){var p,l;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(s.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},a.prev=1,t.info.contractVersion!==S.V1_1){a.next=6;break}p=mr.getFixedOutputSwapQuote(l),a.next=9;break;case 6:return a.next=8,zn.getFixedOutputSwapQuote(l);case 8:p=a.sent;case 9:i(p),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(1),c(a.t0);case 15:case"end":return a.stop()}}),a,null,[[1,12]])})));return function(t,e){return a.apply(this,arguments)}}())})),t.next=4,xr(i);case 4:return p=t.sent,t.abrupt("return",Tr(p));case 6:case"end":return t.stop()}}),t)}))),Ar.apply(this,arguments)}function hr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:Et(t.route)}function Tr(t){var e,n=t[0],r=hr(n),s=D(t);try{for(s.s();!(e=s.n()).done;){var a=e.value,o=hr(a);o>r&&(n=a,r=o)}}catch(t){s.e(t)}finally{s.f()}return n}function yr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var vr=(f(fr={},S.V1_1,mr),f(fr,S.V2,zn),f(fr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ye(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return Ir.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return Ar.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(fr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&yr(t.quote)===S.V1_1?mr.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):zn.generateTxns(t)})),f(fr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&yr(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return mr.signTxns(o({pool:e},t))}return zn.signTxns(t)})),f(fr,"execute",(function(t){return t.contractVersion===S.V1_1?mr.execute(t):zn.execute(t)})),fr);function wr(){return(wr=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.initiatorSigner,t.prev=1,t.next=4,_r({txGroup:s,pool:r,initiatorSigner:a});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:at(s),confirmedRound:d,txnID:l,groupID:ot(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function _r(t){return br.apply(this,arguments)}function br(){return(br=c(u().mark((function t(e){var n,s,a,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.pool,a=e.initiatorSigner,t.next=3,a([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=s.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Dr(){return Dr=c(u().mark((function t(e){var n,s,a,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.data,a=e.initiatorSigner,t.prev=1,o=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:at(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,a(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var s=c(u().mark((function s(a,o){var c,p,l,d,f,m;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:return s.prev=0,c=t.txns.map((function(n,s){return 0===s?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),s.next=4,rt(n,[c]);case 4:p=s.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,a({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(0),o(s.t0);case 15:case"end":return s.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Dr.apply(this,arguments)}function Sr(){return(Sr=c(u().mark((function t(e){var n,a,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=a.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:a.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[c],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:s,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Er(e){var n=e.suggestedParams,r=e.stakingAppID,s=e.initiatorAddr,a=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:s,suggestedParams:n,foreignAssets:[a],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:q([it("tinymanStaking/v1:b"),c,t.encodeUint64(a),i])})}function Or(){return(Or=c(u().mark((function e(n){var r,s,a,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,a=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Er({suggestedParams:l,stakingAppID:s,program:a,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[o],accounts:[a.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=En,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Ke,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=er,exports.Swap=vr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Nn,exports.V2AddLiquidityTxnIndices=rn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=ee,exports.calculatePriceImpact=Xt,exports.calculateSwapRate=qt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=be,exports.prepareCommitTransactions=function(t){return Or.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Dr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return wr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=at,exports.tinymanContract_v1_1=Jt,exports.tinymanContract_v2=Ht,exports.tinymanJSSDKConfig=pt; diff --git a/src/index.ts b/src/index.ts index b5f06a97..88c1ab44 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,8 @@ export { export * from "./swap/v2/router"; +export * from "./swap/common/utils"; + export { applySlippageToAmount, ASSET_OPT_IN_PROCESS_TXN_COUNT, From d0e10da4b5109b3d3b7507a6f94ef07c40725c8b Mon Sep 17 00:00:00 2001 From: anlerkan Date: Tue, 14 Mar 2023 13:44:26 +0300 Subject: [PATCH 20/39] fix(swap): improve errors, fix getBestQuote --- dist/index.js | 2 +- src/swap/utils.ts | 3 ++- src/swap/v2/index.ts | 20 ++++++++++---------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/dist/index.js b/dist/index.js index b7bd2da5..6f8366c4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),s=Uint8Array.from([1]);function a(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--s){var a=this.tryEntries[s],o=a.completion;if("root"===a.tryLoc)return r("end");if(a.tryLoc<=this.prev){var u=n.call(a,"catchLoc"),i=n.call(a,"finallyLoc");if(u&&i){if(this.prev=0;--r){var s=this.tryEntries[r];if(s.tryLoc<=this.prev&&n.call(s,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),w(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var s=r.arg;w(n)}return s}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,s,a,o){try{var u=t[a](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,s)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,s){var a=t.apply(e,n);function o(t){i(a,r,s,o,u,"next",t)}function u(t){i(a,r,s,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:s}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,a=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw a}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var s;p(this,n);for(var a=arguments.length,o=new Array(a>2?a-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function V(t){return 0===Number(t)}function C(t){return Number(t.id)}function Q(t){var e,n=t.stateArray,r=void 0===n?[]:n,s=t.shouldDecodeKeys,a=void 0!==s&&s,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[a?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function q(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),s=0,a=D(t);try{for(a.s();!(e=a.n()).done;){var o=e.value;r.set(o,s),s+=o.length}}catch(t){a.e(t)}finally{a.f()}return r}var X=100000n,j=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var s=t["apps-local-state"]||[],a=t["created-apps"]||[],o=t.assets||[];return X+j*BigInt(o.length)+U*BigInt(a.length+s.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var s="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*s))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var s=v(nt(e),2),a=s[0],o=s[1],u=v(nt(Math.round(Number(et(a,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return st.apply(this,arguments)}function st(){return(st=c(u().mark((function t(e,n){var r,s,a,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=D(n),t.prev=3,s.s();case 5:if((a=s.n()).done){t.next=18;break}return o=a.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function at(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(s,a),note:pt.getAppCallTxnNoteWithClientName(a),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(s,a),note:pt.getAppCallTxnNoteWithClientName(a),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht,Tt,yt=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),vt=it("swap"),wt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),_t={ASSET_OPT_IN:it("asset_opt_in")},bt={testnet:159521633,mainnet:0},Dt=(f(ht={},exports.SwapType.FixedInput,7),f(ht,exports.SwapType.FixedOutput,8),ht),St={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function Et(t){var e=kt(t),n=e.assetIn,r=e.assetOut;return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Ot(t){if(!bt[t])throw new Error("Unknown network or network not supported: ".concat(t));return bt[t]}function Nt(t){return t[t.length-1].quote.amount_out}function Pt(t){return t[0].quote.amount_in}function kt(t){return{assetIn:Pt(t),assetOut:Nt(t)}}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function e(n){var s,a,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,s.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:a,appArgs:[_t.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Rt(t){return Bt.apply(this,arguments)}function Bt(){return(Bt=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_,b,D,E,O;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.initiatorAddr,a=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=[C(c[0].quote.amount_in.asset),C(c[0].quote.amount_out.asset),C(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Pt(c).amount),Number(Nt(c).amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=V(d),v=Ot(o),w=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(_=r.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:v,appArgs:[vt,wt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Dt[i]+1),b=[w,_],e.next=15,Mt({client:a,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((D=e.sent).length>0)){e.next=21;break}return e.next=19,Lt({client:a,accountAddress:s,assetIDs:D,routerAppID:v});case 19:E=e.sent,b.unshift(E);case 21:return O=r.default.assignGroupID(b),e.abrupt("return",O.map((function(t){return{txn:t,signers:[s]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mt(t){return Vt.apply(this,arguments)}function Vt(){return(Vt=c(u().mark((function e(n){var r,s,a,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,a=n.assetIDs,o=t.getApplicationAddress(s),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=a.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ct(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function t(e){var n,r,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,a=e.swapType,o=e.network,i={asset_in_id:String(r),asset_out_id:String(s),swap_type:a,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(St[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}function qt(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function Xt(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,s=t.assetOut,a=qt({assetIn:r,assetOut:s}),o=Z(s.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(a/o-1))}function jt(t){if(t.type===exports.SwapQuoteType.Router){var e=Pt(t.route);return{id:C(e.asset),amount:Number(e.amount)}}return{id:t.quoteWithPool.quote.assetInID,amount:t.quoteWithPool.quote.assetInAmount}}function Ut(t){if(t.type===exports.SwapQuoteType.Router){var e=Nt(t.route);return{id:C(e.asset),amount:Number(e.amount)}}return{id:t.quoteWithPool.quote.assetOutID,amount:t.quoteWithPool.quote.assetOutAmount}}exports.SwapQuoteType=void 0,(Tt=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",Tt.Router="router";var Gt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Wt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},zt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Jt=new(function(n){m(s,zt);var r=y(s);function s(t,e){var n;return p(this,s),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(s,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),s=v(M(n.asset1ID,n.asset2ID),2),a=s[0],o=s[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:a,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(s,a){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,s(o(o({},i),{},{minimum_required_balance:ee(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),a(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ee(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ne,re,se=it("e");function ae(t){return oe.apply(this,arguments)}function oe(){return(oe=c(u().mark((function n(s){var a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.pool,i=s.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=Q({stateArray:A}),T=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.poolTokenID)])),w=h[T],_=h[y],b=h[v],"bigint"==typeof w&&(l=w),"bigint"==typeof _&&(d=_),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ue(){return(ue=c(u().mark((function n(s){var a,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.accountAddr,i=s.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=Q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mpe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(pe,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function me(){return me=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(a=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",a[r]);case 4:return t.next=6,$t(n,r);case 6:return o=t.sent,i=te(o,dt(s,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[ie[S.V1_1].asset1],asset2ID:i[ie[S.V1_1].asset2],poolTokenID:p},a[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),me.apply(this,arguments)}var ge=Object.freeze({__proto__:null,getPoolInfo:le,getPoolReserves:function(t,e){return fe.apply(this,arguments)},getPoolAssets:function(t){return me.apply(this,arguments)}});function xe(t){return Ie.apply(this,arguments)}function Ie(){return(Ie=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,a=e.asset2ID,o=Zt(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(s,a),t.next=8,$t(n,p);case 8:return d=t.sent,f=te(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[ie.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ie.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ie.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ie.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ie.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ie.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ie.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ie.v2.totalFeeShare]),m.poolTokenID=Number(f[ie.v2.poolTokenID]),m.asset1ID=Number(f[ie.v2.asset1]),m.asset2ID=Number(f[ie.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ae(){return(Ae=c(u().mark((function t(e,n){var r,s,a;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,$t(e,n.account.address());case 2:return r=t.sent,s=te(r,n.validatorAppID),a={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(a.asset1=BigInt(s[ie.v2.asset1Reserves]),a.asset2=BigInt(s[ie.v2.asset2Reserves]),a.issuedLiquidity=BigInt(s[ie.v2.issuedPoolTokens])),t.abrupt("return",a);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function he(){return(he=c(u().mark((function t(e){var n,r,s,a,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,$t(n,r);case 3:return a=t.sent,o=te(a,dt(s,S.V2)),i=null,o&&(i={asset1ID:o[ie[S.V2].asset1],asset2ID:o[ie[S.V2].asset2],poolTokenID:o[ie[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Te=Object.freeze({__proto__:null,getPoolInfo:xe,getPoolReserves:function(t,e){return Ae.apply(this,arguments)},getPoolAssets:function(t){return he.apply(this,arguments)}});function ye(t){return Boolean(t&&!(t.asset1+t.asset2))}var ve,we,_e=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ye(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:ye,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([le(t),xe(t)])}}),be=o((f(ve={},S.V1_1,o(o({},ge),_e)),f(ve,S.V2,o(o({},Te),_e)),ve),_e);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(we||(we={}));var De=96e4,Se=859e3;function Ee(){return(Ee=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,b,D,E,N,P;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(a,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=V(x),h=dt(s,S.V1_1),T=Jt.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:x}),y=T.address(),w=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),_=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Oe(A),suggestedParams:c}),(E=[])[we.FUNDING_TXN]=D,E[we.VALIDATOR_APP_CALL]=w,E[we.POOL_TOKEN_CREATE]=_,E[we.ASSET1_OPT_IN]=b,A||(E[we.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),P=[{txn:N[we.FUNDING_TXN],signers:[i]},{txn:N[we.VALIDATOR_APP_CALL],signers:[y]},{txn:N[we.POOL_TOKEN_CREATE],signers:[y]},{txn:N[we.ASSET1_OPT_IN],signers:[y]}],N[we.ASSET2_OPT_IN]&&P.push({txn:N[we.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",P);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Oe(t){return t?De:Se}function Ne(){return(Ne=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Jt.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===we.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),s=n.txID,a=n.blob;return I.push(s),a})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pe(t){return ke.apply(this,arguments)}function ke(){return(ke=c(u().mark((function t(e){var n,r,s,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,s[we.POOL_TOKEN_CREATE]);case 6:if(a=t.sent,"number"==typeof(o=a["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Le(){return(Le=c(u().mark((function t(e){var n,r,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Pe({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",be.v1_1.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Fe,Re={generateTxns:function(t){return Ee.apply(this,arguments)},signTxns:function(t){return Ne.apply(this,arguments)},execute:function(t){return Le.apply(this,arguments)},getBootstrapFundingTxnAmount:Oe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Fe||(Fe={}));var Be=5,Me=6;function Ve(t,e){return Ce.apply(this,arguments)}function Ce(){return(Ce=c(u().mark((function e(n,r){var s,a,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(a=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,a,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Qe(t,e){return qe.apply(this,arguments)}function qe(){return(qe=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Ve(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=dt(a,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,be.v2.getPoolInfo({client:s,network:a,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Ht.generateLogicSigAccountForPool({network:a,asset1ID:g,asset2ID:x}),A=I.address(),h=V(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ue(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:je(h),suggestedParams:p}),(w=[])[Fe.FUNDING_TXN]=y,w[Fe.VALIDATOR_APP_CALL]=T,_=r.default.assignGroupID(w),(b=[])[Fe.FUNDING_TXN]={txn:_[Fe.FUNDING_TXN],signers:[c]},b[Fe.VALIDATOR_APP_CALL]={txn:_[Fe.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function je(t){return function(t,e){var n=Zt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ue(t)+1e5}function Ue(e){return((e?Be:Me)+1)*t.ALGORAND_MIN_TX_FEE}function Ge(){return(Ge=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Ht.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Fe.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),s=n.txID,a=n.blob;return I.push(s),a})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function We(){return(We=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Ve(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ie.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",be.v2.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var ze,Je={generateTxns:function(t){return Xe.apply(this,arguments)},signTxns:function(t){return Ge.apply(this,arguments)},execute:function(t){return We.apply(this,arguments)},getBootstrapFundingTxnAmount:je,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+je(e)}};var Ye,Ke=(f(ze={},S.V1_1,Re),f(ze,S.V2,Je),f(ze,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Re.generateTxns(t):Je.generateTxns(t)})),f(ze,"signTxns",(function(t){return t.contractVersion===S.V1_1?Re.signTxns(t):Je.signTxns(t)})),f(ze,"execute",(function(t){return t.contractVersion===S.V1_1?Re.execute(t):Je.execute(t)})),f(ze,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Re.getBootstrapFundingTxnAmount(n):Je.getBootstrapFundingTxnAmount(n)})),ze);exports.V1_1AddLiquidityTxnIndices=void 0,(Ye=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ye.FEE_TXN=0]="FEE_TXN",Ye[Ye.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ye[Ye.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ye[Ye.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ye[Ye.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var He,Ze,$e,tn,en=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(tn=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",tn.FLEXIBLE="flexible",tn.INITIAL="initial";var nn,rn=(f(He={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(He,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(He,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),He),sn=(f(Ze={},exports.V2AddLiquidityType.INITIAL,1),f(Ze,exports.V2AddLiquidityType.SINGLE,2),f(Ze,exports.V2AddLiquidityType.FLEXIBLE,2),Ze),an=(f($e={},exports.V2AddLiquidityType.INITIAL,3),f($e,exports.V2AddLiquidityType.FLEXIBLE,3),f($e,exports.V2AddLiquidityType.SINGLE,2),$e);function on(t){var e,n,r,s=t.reserves,a=t.totalFeeShare,o=t.asset1,u=t.asset2,i=s.asset1*s.asset2,c=s.asset1+BigInt(o.amount),p=s.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*s.issuedLiquidity*s.issuedLiquidity/i))))),f=d-s.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(o.amount)-m,I=BigInt(u.amount)-g;if(x>I){var A=x;r=un(A,a),e={id:o.id,amount:A+r,decimals:o.decimals,reserves:s.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:u.decimals,reserves:s.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=un(h,a),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:s.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:o.decimals,reserves:s.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Xt({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function un(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function cn(e){return(sn[e]+1)*t.ALGORAND_MIN_TX_FEE}var pn=it("add_liquidity"),ln=(f(nn={},S.V1_1,[it("mint")]),f(nn,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[pn,it("single")],FLEXIBLE_MODE:[pn,it("flexible")]}),nn);function dn(){return(dn=c(u().mark((function t(e){var n,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,S.V1_1),appArgs:ln.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:s,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fn(){return(fn=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,o=n.account,t.next=4,a([s]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=s.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(){return(mn=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(s[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,ae({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[a]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=at(s),x=ot(s),t.next=17,ae({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var gn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,s=t.asset2In;if(0n===n.issuedLiquidity){var a=BigInt(Math.floor(Math.sqrt(Number(r)*Number(s))));if(a<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(a,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(s),poolTokenID:e.poolTokenID,poolTokenOut:a-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(s)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Mn(){return(Mn=c(u().mark((function t(e){var n,s,a,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Rt(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,s=e.initiatorAddr,a=e.slippage,i=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=V(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:H("positive",a,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",a,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:l.validatorAppID,appArgs:[vt,wt[i],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Vn(i),(w=[])[It.INPUT_TXN]=y,w[It.APP_CALL_TXN]=v,_=r.default.assignGroupID(w),t.abrupt("return",[{txn:_[It.INPUT_TXN],signers:[s]},{txn:_[It.APP_CALL_TXN],signers:[s]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Vn(e){return(yt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Cn(){return(Cn=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return s=e.client,a=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,rt(s,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Qe(s,o);case 10:return m=t.sent,g=a.type===exports.SwapQuoteType.Direct?{id:a.quoteWithPool.quote.assetInID,amount:a.quoteWithPool.quote.assetInAmount}:{id:Number(a.asset_in_id),amount:Number(a.route[0].quote.amount_in.amount)},x=a.type===exports.SwapQuoteType.Direct?a.quoteWithPool.quote.assetOutID:Number(a.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:a,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Qn(){return(Qn=c(u().mark((function t(e){var n,r,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.pool,s=e.asset,a=e.decimals,o=e.network,i=e.isSwapRouterEnabled,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,qn({pool:r,assetIn:s,decimals:a,isSwapRouterEnabled:i,network:o});case 4:c=t.sent,t.next=10;break;case 7:return t.next=9,jn({pool:r,assetOut:s,decimals:a,isSwapRouterEnabled:i,network:o});case 9:c=t.sent;case 10:return t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function qn(t){return Xn.apply(this,arguments)}function Xn(){return(Xn=c(u().mark((function t(e){var n,r,s,a,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,s=e.pool,a=e.isSwapRouterEnabled,i=e.network,s.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:s,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=s.totalFeeShare,n.id!==s.asset1ID){t.next=11;break}l=s.asset2ID,d=s.asset1Reserves,f=s.asset2Reserves,t.next=18;break;case 11:if(n.id!==s.asset2ID){t.next=17;break}l=s.asset1ID,d=s.asset2Reserves,f=s.asset1Reserves,t.next=18;break;case 17:throw new F({pool:s,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(s.account.address(),"."));case 18:if(m=Gn({inputSupply:d,outputSupply:f,swapInputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapOutputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(g>f)){t.next=21;break}throw new Bn;case 21:if(A={assetInID:n.id,assetInAmount:c,assetOutID:l,assetOutAmount:g,swapFee:Number(x),rate:Z(r.assetOut,Number(g))/Z(r.assetIn,Number(c)),priceImpact:I},!a){t.next=28;break}return t.next=25,Ct({amount:n.amount,assetInID:Number(n.id),assetOutID:l,swapType:exports.SwapType.FixedInput,network:i});case 25:if(!((h=t.sent).route.length>1&&BigInt(Nt(h.route).amount)>A.assetOutAmount)){t.next=28;break}return t.abrupt("return",o(o({},h),{},{type:exports.SwapQuoteType.Router}));case 28:return t.abrupt("return",{quoteWithPool:{pool:s,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function jn(t){return Un.apply(this,arguments)}function Un(){return(Un=c(u().mark((function t(e){var n,r,s,a,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,s=e.pool,a=e.isSwapRouterEnabled,i=e.network,s.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:s,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=s.totalFeeShare,n.id!==s.asset1ID){t.next=11;break}l=s.asset2ID,d=s.asset2Reserves,f=s.asset1Reserves,t.next=18;break;case 11:if(n.id!==s.asset2ID){t.next=17;break}l=s.asset1ID,d=s.asset1Reserves,f=s.asset2Reserves,t.next=18;break;case 17:throw new F({pool:s,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(s.account.address(),"."));case 18:if(m=Wn({inputSupply:d,outputSupply:f,swapOutputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapInputAmount,x=m.totalFeeAmount,I=m.priceImpact,!(c>f)){t.next=21;break}throw new Bn;case 21:if(A={assetInID:l,assetInAmount:g,assetOutID:n.id,assetOutAmount:c,swapFee:Number(x),rate:Z(r.assetOut,Number(c))/Z(r.assetIn,Number(g)),priceImpact:I},!a){t.next=28;break}return t.next=25,Ct({amount:n.amount,assetInID:l,assetOutID:n.id,swapType:exports.SwapType.FixedOutput,network:i});case 25:if(!((h=t.sent).route.length>1&&BigInt(Nt(h.route).amount)s+BigInt(1e3)?(n=s*e.asset1/a,r=s*e.asset2/a):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Yn(){return(Yn=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.account.address(),m=a.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Pn,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[On.ASSET_TRANSFER_TXN]=g,I[On.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[On.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[On.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kn(){return(Kn=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.asset1ID,m=a.asset2ID,g=a.account.address(),x=a.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Pn,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[On.ASSET_TRANSFER_TXN]=T,v[On.APP_CALL_TXN]=y,w=r.default.assignGroupID(v),e.abrupt("return",[{txn:w[On.ASSET_TRANSFER_TXN],signers:[o]},{txn:w[On.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function t(e){var n,r,s,a,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,s=e.txGroup,a=e.signedTxns,t.next=3,rt(r,[a]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Qe(r,s);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Zn,$n,tr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,s=BigInt(r),a=Jn(s,n),o=a.asset1OutputAmount,u=a.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:s}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,s=t.poolTokenIn,a=t.assetOutID,o=t.decimals,u=BigInt(s),i=Jn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(a===n.asset1ID){var d=zn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(a!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=zn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Yn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Kn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Hn.apply(this,arguments)}},er=(f(Zn={},S.V1_1,Rn),f(Zn,S.V2,tr),Zn),nr=3n,rr=1000n;function sr(){return(sr=c(u().mark((function t(e){var n,s,a,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,t.next=3,a([s]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=s.map((function(t,e){return e===$n.FEE_TXN_INDEX?c:e===$n.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,a=e.quote,o=e.swapType,i=e.slippage,c=e.initiatorAddr,p=a.pool,l=a.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:a},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",i,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",i,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),w=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:s,suggestedParams:x}),_=r.default.assignGroupID([w,A,T,v]),t.abrupt("return",[{txn:_[0],signers:[c]},{txn:_[1],signers:[m]},{txn:_[2],signers:[c]},{txn:_[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(t){var e=t.pool,n=t.reserves,r=t.assetIn,s=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var a,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)a=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));a=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*nr/rr,l=i-u*i/(u+(c-p));if(l>i)throw new Bn;var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:a,assetOutAmount:l,swapFee:Number(p),rate:qt(d),priceImpact:Xt(o({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,quoteWithPool:{pool:e,quote:f}}}function ur(t){return ir.apply(this,arguments)}function ir(){return(ir=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,ae({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[s]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,ae({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cr(t){var e=t.pool,n=t.reserves,r=t.assetOut,s=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var a,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)a=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));a=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new Bn;var c=o*u/(u-i)-o,p=c*rr/(rr-nr),l=p-c,d=Z(s.assetOut,Number(i))/Z(s.assetIn,Number(p)),f=Z(s.assetOut,Number(u))/Z(s.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:a,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,quoteWithPool:{pool:e,quote:g}}}function pr(t){return lr.apply(this,arguments)}function lr(){return(lr=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,ae({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[s]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,ae({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,a.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:a.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dr(){return(dr=c(u().mark((function t(e){var n,r,s,a,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,s=e.swapType,a=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:s,txGroup:a},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:a[$n.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:a[$n.ASSET_IN_TXN_INDEX].txn.amount},l={id:a[$n.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:a[$n.ASSET_OUT_TXN_INDEX].txn.amount},s!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,ur({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,pr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(a),fees:at(a)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}($n||($n={}));var fr,mr={getQuote:function(t,e,n,r,s){return t===exports.SwapType.FixedInput?or({pool:e,reserves:n,assetIn:r,decimals:s}):cr({pool:e,reserves:n,assetOut:r,decimals:s})},getFixedInputSwapQuote:or,getFixedOutputSwapQuote:cr,generateTxns:function(t){return ar.apply(this,arguments)},signTxns:function(t){return sr.apply(this,arguments)},execute:function(t){return dr.apply(this,arguments)},executeFixedOutputSwap:pr},gr=4*t.ALGORAND_MIN_TX_FEE;function xr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Bn})))throw new Bn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function Ir(){return Ir=c(u().mark((function t(e){var n,r,s,a,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,a=e.pools,o=e.isSwapRouterEnabled,i=a.map((function(t){return new Promise(function(){var a=c(u().mark((function a(i,c){var p,l;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},a.prev=1,t.info.contractVersion!==S.V1_1){a.next=6;break}p=mr.getFixedInputSwapQuote(l),a.next=9;break;case 6:return a.next=8,zn.getFixedInputSwapQuote(l);case 8:p=a.sent;case 9:i(p),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(1),c(a.t0);case 15:case"end":return a.stop()}}),a,null,[[1,12]])})));return function(t,e){return a.apply(this,arguments)}}())})),t.next=4,xr(i);case 4:return p=t.sent,t.abrupt("return",Tr(p));case 6:case"end":return t.stop()}}),t)}))),Ir.apply(this,arguments)}function Ar(){return Ar=c(u().mark((function t(e){var n,r,s,a,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,a=e.pools,o=e.isSwapRouterEnabled,i=a.map((function(t){return new Promise(function(){var a=c(u().mark((function a(i,c){var p,l;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(s.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},a.prev=1,t.info.contractVersion!==S.V1_1){a.next=6;break}p=mr.getFixedOutputSwapQuote(l),a.next=9;break;case 6:return a.next=8,zn.getFixedOutputSwapQuote(l);case 8:p=a.sent;case 9:i(p),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(1),c(a.t0);case 15:case"end":return a.stop()}}),a,null,[[1,12]])})));return function(t,e){return a.apply(this,arguments)}}())})),t.next=4,xr(i);case 4:return p=t.sent,t.abrupt("return",Tr(p));case 6:case"end":return t.stop()}}),t)}))),Ar.apply(this,arguments)}function hr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:Et(t.route)}function Tr(t){var e,n=t[0],r=hr(n),s=D(t);try{for(s.s();!(e=s.n()).done;){var a=e.value,o=hr(a);o>r&&(n=a,r=o)}}catch(t){s.e(t)}finally{s.f()}return n}function yr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var vr=(f(fr={},S.V1_1,mr),f(fr,S.V2,zn),f(fr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ye(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return Ir.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return Ar.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(fr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&yr(t.quote)===S.V1_1?mr.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):zn.generateTxns(t)})),f(fr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&yr(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return mr.signTxns(o({pool:e},t))}return zn.signTxns(t)})),f(fr,"execute",(function(t){return t.contractVersion===S.V1_1?mr.execute(t):zn.execute(t)})),fr);function wr(){return(wr=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.initiatorSigner,t.prev=1,t.next=4,_r({txGroup:s,pool:r,initiatorSigner:a});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:at(s),confirmedRound:d,txnID:l,groupID:ot(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function _r(t){return br.apply(this,arguments)}function br(){return(br=c(u().mark((function t(e){var n,s,a,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.pool,a=e.initiatorSigner,t.next=3,a([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=s.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Dr(){return Dr=c(u().mark((function t(e){var n,s,a,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.data,a=e.initiatorSigner,t.prev=1,o=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:at(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,a(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var s=c(u().mark((function s(a,o){var c,p,l,d,f,m;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:return s.prev=0,c=t.txns.map((function(n,s){return 0===s?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),s.next=4,rt(n,[c]);case 4:p=s.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,a({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(0),o(s.t0);case 15:case"end":return s.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Dr.apply(this,arguments)}function Sr(){return(Sr=c(u().mark((function t(e){var n,a,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=a.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:a.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[c],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:s,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Er(e){var n=e.suggestedParams,r=e.stakingAppID,s=e.initiatorAddr,a=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:s,suggestedParams:n,foreignAssets:[a],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:q([it("tinymanStaking/v1:b"),c,t.encodeUint64(a),i])})}function Or(){return(Or=c(u().mark((function e(n){var r,s,a,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,a=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Er({suggestedParams:l,stakingAppID:s,program:a,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[o],accounts:[a.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=En,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Ke,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=er,exports.Swap=vr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Nn,exports.V2AddLiquidityTxnIndices=rn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=ee,exports.calculatePriceImpact=Xt,exports.calculateSwapRate=qt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=be,exports.prepareCommitTransactions=function(t){return Or.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Dr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return wr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=at,exports.tinymanContract_v1_1=Jt,exports.tinymanContract_v2=Ht,exports.tinymanJSSDKConfig=pt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},s=r.iterator||"@@iterator",a=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var s=e&&e.prototype instanceof d?e:d,a=Object.create(s.prototype),o=new _(r||[]);return a._invoke=function(t,e,n){var r="suspendedStart";return function(s,a){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===s)throw a;return D()}for(n.method=s,n.arg=a;;){var o=n.delegate;if(o){var u=y(o,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,o),a}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,s,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,s)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(s,a,o,u){var i=p(t[s],t,a);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,u)}),(function(t){r("throw",t,o,u)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,u)}))}u(i.arg)}var s;this._invoke=function(t,n){function a(){return new e((function(e,s){r(t,n,e,s)}))}return s=s?s.then(a,a):a()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var s=r.arg;return s?s.done?(e[t.resultName]=s.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):s:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function w(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function _(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[s];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,a=function e(){for(;++r=0;--s){var a=this.tryEntries[s],o=a.completion;if("root"===a.tryLoc)return r("end");if(a.tryLoc<=this.prev){var u=n.call(a,"catchLoc"),i=n.call(a,"finallyLoc");if(u&&i){if(this.prev=0;--r){var s=this.tryEntries[r];if(s.tryLoc<=this.prev&&n.call(s,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),w(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var s=r.arg;w(n)}return s}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,s,a,o){try{var u=t[a](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,s)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,s){var a=t.apply(e,n);function o(t){i(a,r,s,o,u,"next",t)}function u(t){i(a,r,s,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:s}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,a=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw a}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var s;p(this,n);for(var a=arguments.length,o=new Array(a>2?a-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function V(t){return 0===Number(t)}function C(t){return Number(t.id)}function Q(t){var e,n=t.stateArray,r=void 0===n?[]:n,s=t.shouldDecodeKeys,a=void 0!==s&&s,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[a?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function q(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),s=0,a=D(t);try{for(a.s();!(e=a.n()).done;){var o=e.value;r.set(o,s),s+=o.length}}catch(t){a.e(t)}finally{a.f()}return r}var X=100000n,j=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var s=t["apps-local-state"]||[],a=t["created-apps"]||[],o=t.assets||[];return X+j*BigInt(o.length)+U*BigInt(a.length+s.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var s="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*s))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var s=v(nt(e),2),a=s[0],o=s[1],u=v(nt(Math.round(Number(et(a,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return st.apply(this,arguments)}function st(){return(st=c(u().mark((function t(e,n){var r,s,a,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=D(n),t.prev=3,s.s();case 5:if((a=s.n()).done){t.next=18;break}return o=a.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function at(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(s,a),note:pt.getAppCallTxnNoteWithClientName(a),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(s,a),note:pt.getAppCallTxnNoteWithClientName(a),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht,Tt,yt=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),vt=it("swap"),wt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),_t={ASSET_OPT_IN:it("asset_opt_in")},bt={testnet:159521633,mainnet:0},Dt=(f(ht={},exports.SwapType.FixedInput,7),f(ht,exports.SwapType.FixedOutput,8),ht),St={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function Et(t){var e=kt(t),n=e.assetIn,r=e.assetOut;return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Ot(t){if(!bt[t])throw new Error("Unknown network or network not supported: ".concat(t));return bt[t]}function Nt(t){return t[t.length-1].quote.amount_out}function Pt(t){return t[0].quote.amount_in}function kt(t){return{assetIn:Pt(t),assetOut:Nt(t)}}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function e(n){var s,a,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,s.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:a,appArgs:[_t.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Rt(t){return Bt.apply(this,arguments)}function Bt(){return(Bt=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_,b,D,E,O;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.initiatorAddr,a=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=[C(c[0].quote.amount_in.asset),C(c[0].quote.amount_out.asset),C(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Pt(c).amount),Number(Nt(c).amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=V(d),v=Ot(o),w=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(_=r.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:v,appArgs:[vt,wt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Dt[i]+1),b=[w,_],e.next=15,Mt({client:a,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((D=e.sent).length>0)){e.next=21;break}return e.next=19,Lt({client:a,accountAddress:s,assetIDs:D,routerAppID:v});case 19:E=e.sent,b.unshift(E);case 21:return O=r.default.assignGroupID(b),e.abrupt("return",O.map((function(t){return{txn:t,signers:[s]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mt(t){return Vt.apply(this,arguments)}function Vt(){return(Vt=c(u().mark((function e(n){var r,s,a,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,a=n.assetIDs,o=t.getApplicationAddress(s),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=a.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ct(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function t(e){var n,r,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,a=e.swapType,o=e.network,i={asset_in_id:String(r),asset_out_id:String(s),swap_type:a,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(St[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}function qt(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function Xt(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,s=t.assetOut,a=qt({assetIn:r,assetOut:s}),o=Z(s.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(a/o-1))}function jt(t){if(t.type===exports.SwapQuoteType.Router){var e=Pt(t.route);return{id:C(e.asset),amount:Number(e.amount)}}return{id:t.quoteWithPool.quote.assetInID,amount:t.quoteWithPool.quote.assetInAmount}}function Ut(t){if(t.type===exports.SwapQuoteType.Router){var e=Nt(t.route);return{id:C(e.asset),amount:Number(e.amount)}}return{id:t.quoteWithPool.quote.assetOutID,amount:t.quoteWithPool.quote.assetOutAmount}}exports.SwapQuoteType=void 0,(Tt=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",Tt.Router="router";var Gt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Wt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},zt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Jt=new(function(n){m(s,zt);var r=y(s);function s(t,e){var n;return p(this,s),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(s,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),s=v(M(n.asset1ID,n.asset2ID),2),a=s[0],o=s[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:a,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(s,a){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,s(o(o({},i),{},{minimum_required_balance:ee(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),a(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ee(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ne,re,se=it("e");function ae(t){return oe.apply(this,arguments)}function oe(){return(oe=c(u().mark((function n(s){var a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.pool,i=s.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=Q({stateArray:A}),T=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.poolTokenID)])),w=h[T],_=h[y],b=h[v],"bigint"==typeof w&&(l=w),"bigint"==typeof _&&(d=_),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ue(){return(ue=c(u().mark((function n(s){var a,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.accountAddr,i=s.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=Q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mpe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(pe,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function me(){return me=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(a=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",a[r]);case 4:return t.next=6,$t(n,r);case 6:return o=t.sent,i=te(o,dt(s,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[ie[S.V1_1].asset1],asset2ID:i[ie[S.V1_1].asset2],poolTokenID:p},a[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),me.apply(this,arguments)}var ge=Object.freeze({__proto__:null,getPoolInfo:le,getPoolReserves:function(t,e){return fe.apply(this,arguments)},getPoolAssets:function(t){return me.apply(this,arguments)}});function xe(t){return Ie.apply(this,arguments)}function Ie(){return(Ie=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,a=e.asset2ID,o=Zt(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(s,a),t.next=8,$t(n,p);case 8:return d=t.sent,f=te(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[ie.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ie.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ie.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ie.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ie.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ie.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ie.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ie.v2.totalFeeShare]),m.poolTokenID=Number(f[ie.v2.poolTokenID]),m.asset1ID=Number(f[ie.v2.asset1]),m.asset2ID=Number(f[ie.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ae(){return(Ae=c(u().mark((function t(e,n){var r,s,a;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,$t(e,n.account.address());case 2:return r=t.sent,s=te(r,n.validatorAppID),a={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(a.asset1=BigInt(s[ie.v2.asset1Reserves]),a.asset2=BigInt(s[ie.v2.asset2Reserves]),a.issuedLiquidity=BigInt(s[ie.v2.issuedPoolTokens])),t.abrupt("return",a);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function he(){return(he=c(u().mark((function t(e){var n,r,s,a,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,$t(n,r);case 3:return a=t.sent,o=te(a,dt(s,S.V2)),i=null,o&&(i={asset1ID:o[ie[S.V2].asset1],asset2ID:o[ie[S.V2].asset2],poolTokenID:o[ie[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Te=Object.freeze({__proto__:null,getPoolInfo:xe,getPoolReserves:function(t,e){return Ae.apply(this,arguments)},getPoolAssets:function(t){return he.apply(this,arguments)}});function ye(t){return Boolean(t&&!(t.asset1+t.asset2))}var ve,we,_e=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ye(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:ye,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([le(t),xe(t)])}}),be=o((f(ve={},S.V1_1,o(o({},ge),_e)),f(ve,S.V2,o(o({},Te),_e)),ve),_e);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(we||(we={}));var De=96e4,Se=859e3;function Ee(){return(Ee=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,b,D,E,N,P;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(a,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=V(x),h=dt(s,S.V1_1),T=Jt.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:x}),y=T.address(),w=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),_=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Oe(A),suggestedParams:c}),(E=[])[we.FUNDING_TXN]=D,E[we.VALIDATOR_APP_CALL]=w,E[we.POOL_TOKEN_CREATE]=_,E[we.ASSET1_OPT_IN]=b,A||(E[we.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),P=[{txn:N[we.FUNDING_TXN],signers:[i]},{txn:N[we.VALIDATOR_APP_CALL],signers:[y]},{txn:N[we.POOL_TOKEN_CREATE],signers:[y]},{txn:N[we.ASSET1_OPT_IN],signers:[y]}],N[we.ASSET2_OPT_IN]&&P.push({txn:N[we.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",P);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Oe(t){return t?De:Se}function Ne(){return(Ne=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Jt.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===we.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),s=n.txID,a=n.blob;return I.push(s),a})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pe(t){return ke.apply(this,arguments)}function ke(){return(ke=c(u().mark((function t(e){var n,r,s,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,s[we.POOL_TOKEN_CREATE]);case 6:if(a=t.sent,"number"==typeof(o=a["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Le(){return(Le=c(u().mark((function t(e){var n,r,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Pe({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",be.v1_1.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Fe,Re={generateTxns:function(t){return Ee.apply(this,arguments)},signTxns:function(t){return Ne.apply(this,arguments)},execute:function(t){return Le.apply(this,arguments)},getBootstrapFundingTxnAmount:Oe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Fe||(Fe={}));var Be=5,Me=6;function Ve(t,e){return Ce.apply(this,arguments)}function Ce(){return(Ce=c(u().mark((function e(n,r){var s,a,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(a=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,a,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Qe(t,e){return qe.apply(this,arguments)}function qe(){return(qe=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Ve(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=dt(a,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,be.v2.getPoolInfo({client:s,network:a,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Ht.generateLogicSigAccountForPool({network:a,asset1ID:g,asset2ID:x}),A=I.address(),h=V(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ue(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:je(h),suggestedParams:p}),(w=[])[Fe.FUNDING_TXN]=y,w[Fe.VALIDATOR_APP_CALL]=T,_=r.default.assignGroupID(w),(b=[])[Fe.FUNDING_TXN]={txn:_[Fe.FUNDING_TXN],signers:[c]},b[Fe.VALIDATOR_APP_CALL]={txn:_[Fe.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function je(t){return function(t,e){var n=Zt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ue(t)+1e5}function Ue(e){return((e?Be:Me)+1)*t.ALGORAND_MIN_TX_FEE}function Ge(){return(Ge=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Ht.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Fe.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),s=n.txID,a=n.blob;return I.push(s),a})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function We(){return(We=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Ve(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ie.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",be.v2.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var ze,Je={generateTxns:function(t){return Xe.apply(this,arguments)},signTxns:function(t){return Ge.apply(this,arguments)},execute:function(t){return We.apply(this,arguments)},getBootstrapFundingTxnAmount:je,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+je(e)}};var Ye,Ke=(f(ze={},S.V1_1,Re),f(ze,S.V2,Je),f(ze,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Re.generateTxns(t):Je.generateTxns(t)})),f(ze,"signTxns",(function(t){return t.contractVersion===S.V1_1?Re.signTxns(t):Je.signTxns(t)})),f(ze,"execute",(function(t){return t.contractVersion===S.V1_1?Re.execute(t):Je.execute(t)})),f(ze,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Re.getBootstrapFundingTxnAmount(n):Je.getBootstrapFundingTxnAmount(n)})),ze);exports.V1_1AddLiquidityTxnIndices=void 0,(Ye=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ye.FEE_TXN=0]="FEE_TXN",Ye[Ye.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ye[Ye.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ye[Ye.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ye[Ye.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var He,Ze,$e,tn,en=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(tn=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",tn.FLEXIBLE="flexible",tn.INITIAL="initial";var nn,rn=(f(He={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(He,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(He,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),He),sn=(f(Ze={},exports.V2AddLiquidityType.INITIAL,1),f(Ze,exports.V2AddLiquidityType.SINGLE,2),f(Ze,exports.V2AddLiquidityType.FLEXIBLE,2),Ze),an=(f($e={},exports.V2AddLiquidityType.INITIAL,3),f($e,exports.V2AddLiquidityType.FLEXIBLE,3),f($e,exports.V2AddLiquidityType.SINGLE,2),$e);function on(t){var e,n,r,s=t.reserves,a=t.totalFeeShare,o=t.asset1,u=t.asset2,i=s.asset1*s.asset2,c=s.asset1+BigInt(o.amount),p=s.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*s.issuedLiquidity*s.issuedLiquidity/i))))),f=d-s.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(o.amount)-m,I=BigInt(u.amount)-g;if(x>I){var A=x;r=un(A,a),e={id:o.id,amount:A+r,decimals:o.decimals,reserves:s.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:u.decimals,reserves:s.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=un(h,a),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:s.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:o.decimals,reserves:s.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Xt({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function un(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function cn(e){return(sn[e]+1)*t.ALGORAND_MIN_TX_FEE}var pn=it("add_liquidity"),ln=(f(nn={},S.V1_1,[it("mint")]),f(nn,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[pn,it("single")],FLEXIBLE_MODE:[pn,it("flexible")]}),nn);function dn(){return(dn=c(u().mark((function t(e){var n,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,S.V1_1),appArgs:ln.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:s,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fn(){return(fn=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,o=n.account,t.next=4,a([s]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=s.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(){return(mn=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(s[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,ae({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[a]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=at(s),x=ot(s),t.next=17,ae({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var gn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,s=t.asset2In;if(0n===n.issuedLiquidity){var a=BigInt(Math.floor(Math.sqrt(Number(r)*Number(s))));if(a<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(a,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(s),poolTokenID:e.poolTokenID,poolTokenOut:a-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(s)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Mn(){return(Mn=c(u().mark((function t(e){var n,s,a,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Rt(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,s=e.initiatorAddr,a=e.slippage,i=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=V(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:H("positive",a,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",a,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:l.validatorAppID,appArgs:[vt,wt[i],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Vn(i),(w=[])[It.INPUT_TXN]=y,w[It.APP_CALL_TXN]=v,_=r.default.assignGroupID(w),t.abrupt("return",[{txn:_[It.INPUT_TXN],signers:[s]},{txn:_[It.APP_CALL_TXN],signers:[s]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Vn(e){return(yt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Cn(){return(Cn=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return s=e.client,a=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,rt(s,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Qe(s,o);case 10:return m=t.sent,g=a.type===exports.SwapQuoteType.Direct?{id:a.quoteWithPool.quote.assetInID,amount:a.quoteWithPool.quote.assetInAmount}:{id:Number(a.asset_in_id),amount:Number(a.route[0].quote.amount_in.amount)},x=a.type===exports.SwapQuoteType.Direct?a.quoteWithPool.quote.assetOutID:Number(a.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:a,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Qn(){return(Qn=c(u().mark((function t(e){var n,r,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.pool,s=e.asset,a=e.decimals,o=e.network,i=e.isSwapRouterEnabled,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,qn({pool:r,assetIn:s,decimals:a,isSwapRouterEnabled:i,network:o});case 4:c=t.sent,t.next=10;break;case 7:return t.next=9,jn({pool:r,assetOut:s,decimals:a,isSwapRouterEnabled:i,network:o});case 9:c=t.sent;case 10:return t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function qn(t){return Xn.apply(this,arguments)}function Xn(){return(Xn=c(u().mark((function t(e){var n,r,s,a,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,s=e.pool,a=e.isSwapRouterEnabled,i=e.network,s.status===exports.PoolStatus.READY||a){t.next=3;break}throw new F({pool:s,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=s.totalFeeShare,n.id!==s.asset1ID){t.next=11;break}l=s.asset2ID,d=s.asset1Reserves,f=s.asset2Reserves,t.next=18;break;case 11:if(n.id!==s.asset2ID){t.next=17;break}l=s.asset1ID,d=s.asset2Reserves,f=s.asset1Reserves,t.next=18;break;case 17:throw new F({pool:s,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(s.account.address(),"."));case 18:if(m=Gn({inputSupply:d,outputSupply:f,swapInputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapOutputAmount,x=m.totalFeeAmount,I=m.priceImpact,A={assetInID:n.id,assetInAmount:c,assetOutID:l,assetOutAmount:g,swapFee:Number(x),rate:Z(r.assetOut,Number(g))/Z(r.assetIn,Number(c)),priceImpact:I},!a){t.next=26;break}return t.next=23,Ct({amount:n.amount,assetInID:Number(n.id),assetOutID:l,swapType:exports.SwapType.FixedInput,network:i});case 23:if(!((h=t.sent).route.length>1&&BigInt(Nt(h.route).amount)>A.assetOutAmount)){t.next=26;break}return t.abrupt("return",o(o({},h),{},{type:exports.SwapQuoteType.Router}));case 26:if(!(g>f)){t.next=28;break}throw new Bn;case 28:return t.abrupt("return",{quoteWithPool:{pool:s,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function jn(t){return Un.apply(this,arguments)}function Un(){return(Un=c(u().mark((function t(e){var n,r,s,a,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,s=e.pool,a=e.isSwapRouterEnabled,i=e.network,s.status===exports.PoolStatus.READY||a){t.next=3;break}throw new F({pool:s,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=s.totalFeeShare,n.id!==s.asset1ID){t.next=11;break}l=s.asset2ID,d=s.asset2Reserves,f=s.asset1Reserves,t.next=18;break;case 11:if(n.id!==s.asset2ID){t.next=17;break}l=s.asset1ID,d=s.asset1Reserves,f=s.asset2Reserves,t.next=18;break;case 17:throw new F({pool:s,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(s.account.address(),"."));case 18:if(m=Wn({inputSupply:d,outputSupply:f,swapOutputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapInputAmount,x=m.totalFeeAmount,I=m.priceImpact,A={assetInID:l,assetInAmount:g,assetOutID:n.id,assetOutAmount:c,swapFee:Number(x),rate:Z(r.assetOut,Number(c))/Z(r.assetIn,Number(g)),priceImpact:I},!a){t.next=26;break}return t.next=23,Ct({amount:n.amount,assetInID:l,assetOutID:n.id,swapType:exports.SwapType.FixedOutput,network:i});case 23:if(!((h=t.sent).route.length>1&&BigInt(Nt(h.route).amount)f)){t.next=28;break}throw new Bn;case 28:return t.abrupt("return",{quoteWithPool:{pool:s,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Gn(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,s=t.totalFeeShare,a=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:s})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,s=n-BigInt(e*n/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:u,totalFeeAmount:o,priceImpact:Xt({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:a.assetIn},assetOut:{amount:u,decimals:a.assetOut}})}}function Wn(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,s=t.totalFeeShare,a=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,s=BigInt(e*n/(n-r))-e;return s+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:o,totalFeeShare:s}),i=o+u;return{swapInputAmount:i,totalFeeAmount:u,priceImpact:Xt({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:a.assetIn},assetOut:{amount:r,decimals:a.assetOut}})}}var zn={getQuote:function(t){return Qn.apply(this,arguments)},getFixedInputSwapQuote:qn,getFixedOutputSwapQuote:jn,generateTxns:function(t){return Mn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Cn.apply(this,arguments)},calculateFixedInputSwap:Gn};function Jn(t,e){var n,r,s=BigInt(t),a=e.issuedLiquidity;return a>s+BigInt(1e3)?(n=s*e.asset1/a,r=s*e.asset2/a):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Yn(){return(Yn=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.account.address(),m=a.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Pn,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[On.ASSET_TRANSFER_TXN]=g,I[On.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[On.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[On.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kn(){return(Kn=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.asset1ID,m=a.asset2ID,g=a.account.address(),x=a.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Pn,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[On.ASSET_TRANSFER_TXN]=T,v[On.APP_CALL_TXN]=y,w=r.default.assignGroupID(v),e.abrupt("return",[{txn:w[On.ASSET_TRANSFER_TXN],signers:[o]},{txn:w[On.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function t(e){var n,r,s,a,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,s=e.txGroup,a=e.signedTxns,t.next=3,rt(r,[a]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Qe(r,s);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Zn,$n,tr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,s=BigInt(r),a=Jn(s,n),o=a.asset1OutputAmount,u=a.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:s}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,s=t.poolTokenIn,a=t.assetOutID,o=t.decimals,u=BigInt(s),i=Jn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(a===n.asset1ID){var d=zn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(a!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=zn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Yn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Kn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Hn.apply(this,arguments)}},er=(f(Zn={},S.V1_1,Rn),f(Zn,S.V2,tr),Zn),nr=3n,rr=1000n;function sr(){return(sr=c(u().mark((function t(e){var n,s,a,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,t.next=3,a([s]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=s.map((function(t,e){return e===$n.FEE_TXN_INDEX?c:e===$n.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,a=e.quote,o=e.swapType,i=e.slippage,c=e.initiatorAddr,p=a.pool,l=a.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:a},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",i,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",i,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),w=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:s,suggestedParams:x}),_=r.default.assignGroupID([w,A,T,v]),t.abrupt("return",[{txn:_[0],signers:[c]},{txn:_[1],signers:[m]},{txn:_[2],signers:[c]},{txn:_[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(t){var e=t.pool,n=t.reserves,r=t.assetIn,s=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var a,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)a=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));a=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*nr/rr,l=i-u*i/(u+(c-p));if(l>i)throw new Bn;var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:a,assetOutAmount:l,swapFee:Number(p),rate:qt(d),priceImpact:Xt(o({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,quoteWithPool:{pool:e,quote:f}}}function ur(t){return ir.apply(this,arguments)}function ir(){return(ir=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,ae({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[s]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,ae({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cr(t){var e=t.pool,n=t.reserves,r=t.assetOut,s=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var a,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)a=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));a=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new Bn;var c=o*u/(u-i)-o,p=c*rr/(rr-nr),l=p-c,d=Z(s.assetOut,Number(i))/Z(s.assetIn,Number(p)),f=Z(s.assetOut,Number(u))/Z(s.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:a,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,quoteWithPool:{pool:e,quote:g}}}function pr(t){return lr.apply(this,arguments)}function lr(){return(lr=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,ae({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[s]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,ae({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,a.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:a.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dr(){return(dr=c(u().mark((function t(e){var n,r,s,a,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,s=e.swapType,a=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:s,txGroup:a},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:a[$n.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:a[$n.ASSET_IN_TXN_INDEX].txn.amount},l={id:a[$n.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:a[$n.ASSET_OUT_TXN_INDEX].txn.amount},s!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,ur({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,pr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(a),fees:at(a)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}($n||($n={}));var fr,mr={getQuote:function(t,e,n,r,s){return t===exports.SwapType.FixedInput?or({pool:e,reserves:n,assetIn:r,decimals:s}):cr({pool:e,reserves:n,assetOut:r,decimals:s})},getFixedInputSwapQuote:or,getFixedOutputSwapQuote:cr,generateTxns:function(t){return ar.apply(this,arguments)},signTxns:function(t){return sr.apply(this,arguments)},execute:function(t){return dr.apply(this,arguments)},executeFixedOutputSwap:pr},gr=4*t.ALGORAND_MIN_TX_FEE;function xr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Bn})))throw new Bn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function Ir(){return Ir=c(u().mark((function t(e){var n,r,s,a,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,a=e.pools,o=e.isSwapRouterEnabled,i=a.map((function(t){return new Promise(function(){var a=c(u().mark((function a(i,c){var p,l;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},a.prev=1,t.info.contractVersion!==S.V1_1){a.next=6;break}p=mr.getFixedInputSwapQuote(l),a.next=9;break;case 6:return a.next=8,zn.getFixedInputSwapQuote(l);case 8:p=a.sent;case 9:i(p),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(1),c(a.t0);case 15:case"end":return a.stop()}}),a,null,[[1,12]])})));return function(t,e){return a.apply(this,arguments)}}())})),t.next=4,xr(i);case 4:return p=t.sent,t.abrupt("return",Tr(p));case 6:case"end":return t.stop()}}),t)}))),Ir.apply(this,arguments)}function Ar(){return Ar=c(u().mark((function t(e){var n,r,s,a,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,a=e.pools,o=e.isSwapRouterEnabled,i=a.map((function(t){return new Promise(function(){var a=c(u().mark((function a(i,c){var p,l;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(s.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},a.prev=1,t.info.contractVersion!==S.V1_1){a.next=6;break}p=mr.getFixedOutputSwapQuote(l),a.next=9;break;case 6:return a.next=8,zn.getFixedOutputSwapQuote(l);case 8:p=a.sent;case 9:i(p),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(1),c(a.t0);case 15:case"end":return a.stop()}}),a,null,[[1,12]])})));return function(t,e){return a.apply(this,arguments)}}())})),t.next=4,xr(i);case 4:return p=t.sent,t.abrupt("return",Tr(p));case 6:case"end":return t.stop()}}),t)}))),Ar.apply(this,arguments)}function hr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:Et(t.route)}function Tr(t){for(var e=t[0],n=hr(e),r=1;rn&&(e=s,n=a)}return e}function yr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var vr=(f(fr={},S.V1_1,mr),f(fr,S.V2,zn),f(fr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ye(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return Ir.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return Ar.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(fr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&yr(t.quote)===S.V1_1?mr.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):zn.generateTxns(t)})),f(fr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&yr(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return mr.signTxns(o({pool:e},t))}return zn.signTxns(t)})),f(fr,"execute",(function(t){return t.contractVersion===S.V1_1?mr.execute(t):zn.execute(t)})),fr);function wr(){return(wr=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.initiatorSigner,t.prev=1,t.next=4,_r({txGroup:s,pool:r,initiatorSigner:a});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:at(s),confirmedRound:d,txnID:l,groupID:ot(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function _r(t){return br.apply(this,arguments)}function br(){return(br=c(u().mark((function t(e){var n,s,a,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.pool,a=e.initiatorSigner,t.next=3,a([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=s.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Dr(){return Dr=c(u().mark((function t(e){var n,s,a,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.data,a=e.initiatorSigner,t.prev=1,o=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:at(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,a(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var s=c(u().mark((function s(a,o){var c,p,l,d,f,m;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:return s.prev=0,c=t.txns.map((function(n,s){return 0===s?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),s.next=4,rt(n,[c]);case 4:p=s.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,a({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(0),o(s.t0);case 15:case"end":return s.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Dr.apply(this,arguments)}function Sr(){return(Sr=c(u().mark((function t(e){var n,a,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=a.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:a.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[c],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:s,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Er(e){var n=e.suggestedParams,r=e.stakingAppID,s=e.initiatorAddr,a=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:s,suggestedParams:n,foreignAssets:[a],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:q([it("tinymanStaking/v1:b"),c,t.encodeUint64(a),i])})}function Or(){return(Or=c(u().mark((function e(n){var r,s,a,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,a=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Er({suggestedParams:l,stakingAppID:s,program:a,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[o],accounts:[a.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=En,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Ke,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=er,exports.Swap=vr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Nn,exports.V2AddLiquidityTxnIndices=rn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=ee,exports.calculatePriceImpact=Xt,exports.calculateSwapRate=qt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=be,exports.prepareCommitTransactions=function(t){return Or.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Dr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return wr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=at,exports.tinymanContract_v1_1=Jt,exports.tinymanContract_v2=Ht,exports.tinymanJSSDKConfig=pt; diff --git a/src/swap/utils.ts b/src/swap/utils.ts index 9b566d1a..4339f15c 100644 --- a/src/swap/utils.ts +++ b/src/swap/utils.ts @@ -164,7 +164,8 @@ function getBestQuote(quotes: SwapQuote[]): SwapQuote { let bestQuote: SwapQuote = quotes[0]; let bestQuoteRate = getSwapQuoteRate(bestQuote); - for (const quote of quotes) { + for (let index = 1; index < quotes.length; index++) { + const quote = quotes[index]; const currentRate = getSwapQuoteRate(quote); if (currentRate > bestQuoteRate) { diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index 29dc6c3c..488acb1e 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -267,7 +267,7 @@ async function getFixedInputSwapQuote({ network: SupportedNetwork; isSwapRouterEnabled?: boolean; }): Promise { - if (pool.status !== PoolStatus.READY) { + if (pool.status !== PoolStatus.READY && !isSwapRouterEnabled) { throw new TinymanError({pool, assetIn}, "Trying to swap on a non-existent pool"); } @@ -301,10 +301,6 @@ async function getFixedInputSwapQuote({ decimals }); - if (swapOutputAmount > outputSupply) { - throw new OutputAmountExceedsAvailableLiquidityError(); - } - const directSwapQuote: DirectSwapQuote = { assetInID: assetIn.id, assetInAmount, @@ -338,6 +334,10 @@ async function getFixedInputSwapQuote({ } } + if (swapOutputAmount > outputSupply) { + throw new OutputAmountExceedsAvailableLiquidityError(); + } + return { quoteWithPool: { pool, @@ -363,7 +363,7 @@ async function getFixedOutputSwapQuote({ network: SupportedNetwork; isSwapRouterEnabled?: boolean; }): Promise { - if (pool.status !== PoolStatus.READY) { + if (pool.status !== PoolStatus.READY && !isSwapRouterEnabled) { throw new TinymanError({pool, assetOut}, "Trying to swap on a non-existent pool"); } @@ -398,10 +398,6 @@ async function getFixedOutputSwapQuote({ decimals }); - if (assetOutAmount > outputSupply) { - throw new OutputAmountExceedsAvailableLiquidityError(); - } - const directSwapQuote = { assetInID, assetInAmount: swapInputAmount, @@ -435,6 +431,10 @@ async function getFixedOutputSwapQuote({ } } + if (assetOutAmount > outputSupply) { + throw new OutputAmountExceedsAvailableLiquidityError(); + } + return { quoteWithPool: { pool, From e4a30f4d370a5680730340bd74371bb5be763838 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Tue, 14 Mar 2023 14:00:14 +0300 Subject: [PATCH 21/39] chore(swap): re-organize types, naming changes --- dist/index.js | 4 +- dist/swap/common/utils.d.ts | 3 +- dist/swap/types.d.ts | 171 ++++++++++++------------ dist/swap/utils.d.ts | 2 - dist/swap/v2/router/swap-router.d.ts | 4 +- src/swap/common/utils.ts | 21 +-- src/swap/types.ts | 193 ++++++++++++++------------- src/swap/utils.ts | 22 +-- src/swap/v1_1/index.ts | 4 +- src/swap/v2/index.ts | 34 ++--- src/swap/v2/router/swap-router.ts | 4 +- 11 files changed, 224 insertions(+), 238 deletions(-) diff --git a/dist/index.js b/dist/index.js index 6f8366c4..479d49f9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),s=Uint8Array.from([1]);function a(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--s){var a=this.tryEntries[s],o=a.completion;if("root"===a.tryLoc)return r("end");if(a.tryLoc<=this.prev){var u=n.call(a,"catchLoc"),i=n.call(a,"finallyLoc");if(u&&i){if(this.prev=0;--r){var s=this.tryEntries[r];if(s.tryLoc<=this.prev&&n.call(s,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),w(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var s=r.arg;w(n)}return s}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,s,a,o){try{var u=t[a](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,s)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,s){var a=t.apply(e,n);function o(t){i(a,r,s,o,u,"next",t)}function u(t){i(a,r,s,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:s}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,a=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw a}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",P="logic eval error:",k="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var s;p(this,n);for(var a=arguments.length,o=new Array(a>2?a-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function V(t){return 0===Number(t)}function C(t){return Number(t.id)}function Q(t){var e,n=t.stateArray,r=void 0===n?[]:n,s=t.shouldDecodeKeys,a=void 0!==s&&s,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[a?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function q(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),s=0,a=D(t);try{for(a.s();!(e=a.n()).done;){var o=e.value;r.set(o,s),s+=o.length}}catch(t){a.e(t)}finally{a.f()}return r}var X=100000n,j=100000n,U=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var s=t["apps-local-state"]||[],a=t["created-apps"]||[],o=t.assets||[];return X+j*BigInt(o.length)+U*BigInt(a.length+s.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var s="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*s))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var s=v(nt(e),2),a=s[0],o=s[1],u=v(nt(Math.round(Number(et(a,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return st.apply(this,arguments)}function st(){return(st=c(u().mark((function t(e,n){var r,s,a,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=D(n),t.prev=3,s.s();case 5:if((a=s.n()).done){t.next=18;break}return o=a.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function at(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(s,a),note:pt.getAppCallTxnNoteWithClientName(a),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(s,a),note:pt.getAppCallTxnNoteWithClientName(a),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht,Tt,yt=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),vt=it("swap"),wt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),_t={ASSET_OPT_IN:it("asset_opt_in")},bt={testnet:159521633,mainnet:0},Dt=(f(ht={},exports.SwapType.FixedInput,7),f(ht,exports.SwapType.FixedOutput,8),ht),St={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function Et(t){var e=kt(t),n=e.assetIn,r=e.assetOut;return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Ot(t){if(!bt[t])throw new Error("Unknown network or network not supported: ".concat(t));return bt[t]}function Nt(t){return t[t.length-1].quote.amount_out}function Pt(t){return t[0].quote.amount_in}function kt(t){return{assetIn:Pt(t),assetOut:Nt(t)}}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function e(n){var s,a,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,s.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:a,appArgs:[_t.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Rt(t){return Bt.apply(this,arguments)}function Bt(){return(Bt=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_,b,D,E,O;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.initiatorAddr,a=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=[C(c[0].quote.amount_in.asset),C(c[0].quote.amount_out.asset),C(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Pt(c).amount),Number(Nt(c).amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=V(d),v=Ot(o),w=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(_=r.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:v,appArgs:[vt,wt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Dt[i]+1),b=[w,_],e.next=15,Mt({client:a,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((D=e.sent).length>0)){e.next=21;break}return e.next=19,Lt({client:a,accountAddress:s,assetIDs:D,routerAppID:v});case 19:E=e.sent,b.unshift(E);case 21:return O=r.default.assignGroupID(b),e.abrupt("return",O.map((function(t){return{txn:t,signers:[s]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mt(t){return Vt.apply(this,arguments)}function Vt(){return(Vt=c(u().mark((function e(n){var r,s,a,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,a=n.assetIDs,o=t.getApplicationAddress(s),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=a.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ct(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function t(e){var n,r,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,a=e.swapType,o=e.network,i={asset_in_id:String(r),asset_out_id:String(s),swap_type:a,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(St[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}function qt(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function Xt(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,s=t.assetOut,a=qt({assetIn:r,assetOut:s}),o=Z(s.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(a/o-1))}function jt(t){if(t.type===exports.SwapQuoteType.Router){var e=Pt(t.route);return{id:C(e.asset),amount:Number(e.amount)}}return{id:t.quoteWithPool.quote.assetInID,amount:t.quoteWithPool.quote.assetInAmount}}function Ut(t){if(t.type===exports.SwapQuoteType.Router){var e=Nt(t.route);return{id:C(e.asset),amount:Number(e.amount)}}return{id:t.quoteWithPool.quote.assetOutID,amount:t.quoteWithPool.quote.assetOutAmount}}exports.SwapQuoteType=void 0,(Tt=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",Tt.Router="router";var Gt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Wt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},zt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Jt=new(function(n){m(s,zt);var r=y(s);function s(t,e){var n;return p(this,s),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(s,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),s=v(M(n.asset1ID,n.asset2ID),2),a=s[0],o=s[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:a,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(s,a){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,s(o(o({},i),{},{minimum_required_balance:ee(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),a(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ee(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ne,re,se=it("e");function ae(t){return oe.apply(this,arguments)}function oe(){return(oe=c(u().mark((function n(s){var a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.pool,i=s.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=Q({stateArray:A}),T=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(q([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.poolTokenID)])),w=h[T],_=h[y],b=h[v],"bigint"==typeof w&&(l=w),"bigint"==typeof _&&(d=_),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ue(){return(ue=c(u().mark((function n(s){var a,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.accountAddr,i=s.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=Q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mpe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(pe,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function me(){return me=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(a=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",a[r]);case 4:return t.next=6,$t(n,r);case 6:return o=t.sent,i=te(o,dt(s,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[ie[S.V1_1].asset1],asset2ID:i[ie[S.V1_1].asset2],poolTokenID:p},a[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),me.apply(this,arguments)}var ge=Object.freeze({__proto__:null,getPoolInfo:le,getPoolReserves:function(t,e){return fe.apply(this,arguments)},getPoolAssets:function(t){return me.apply(this,arguments)}});function xe(t){return Ie.apply(this,arguments)}function Ie(){return(Ie=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,a=e.asset2ID,o=Zt(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(s,a),t.next=8,$t(n,p);case 8:return d=t.sent,f=te(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[ie.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ie.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ie.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ie.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ie.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ie.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ie.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ie.v2.totalFeeShare]),m.poolTokenID=Number(f[ie.v2.poolTokenID]),m.asset1ID=Number(f[ie.v2.asset1]),m.asset2ID=Number(f[ie.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ae(){return(Ae=c(u().mark((function t(e,n){var r,s,a;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,$t(e,n.account.address());case 2:return r=t.sent,s=te(r,n.validatorAppID),a={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(a.asset1=BigInt(s[ie.v2.asset1Reserves]),a.asset2=BigInt(s[ie.v2.asset2Reserves]),a.issuedLiquidity=BigInt(s[ie.v2.issuedPoolTokens])),t.abrupt("return",a);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function he(){return(he=c(u().mark((function t(e){var n,r,s,a,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,$t(n,r);case 3:return a=t.sent,o=te(a,dt(s,S.V2)),i=null,o&&(i={asset1ID:o[ie[S.V2].asset1],asset2ID:o[ie[S.V2].asset2],poolTokenID:o[ie[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Te=Object.freeze({__proto__:null,getPoolInfo:xe,getPoolReserves:function(t,e){return Ae.apply(this,arguments)},getPoolAssets:function(t){return he.apply(this,arguments)}});function ye(t){return Boolean(t&&!(t.asset1+t.asset2))}var ve,we,_e=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ye(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:ye,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([le(t),xe(t)])}}),be=o((f(ve={},S.V1_1,o(o({},ge),_e)),f(ve,S.V2,o(o({},Te),_e)),ve),_e);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(we||(we={}));var De=96e4,Se=859e3;function Ee(){return(Ee=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,b,D,E,N,P;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(a,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=V(x),h=dt(s,S.V1_1),T=Jt.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:x}),y=T.address(),w=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),_=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Oe(A),suggestedParams:c}),(E=[])[we.FUNDING_TXN]=D,E[we.VALIDATOR_APP_CALL]=w,E[we.POOL_TOKEN_CREATE]=_,E[we.ASSET1_OPT_IN]=b,A||(E[we.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),P=[{txn:N[we.FUNDING_TXN],signers:[i]},{txn:N[we.VALIDATOR_APP_CALL],signers:[y]},{txn:N[we.POOL_TOKEN_CREATE],signers:[y]},{txn:N[we.ASSET1_OPT_IN],signers:[y]}],N[we.ASSET2_OPT_IN]&&P.push({txn:N[we.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",P);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Oe(t){return t?De:Se}function Ne(){return(Ne=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Jt.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===we.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),s=n.txID,a=n.blob;return I.push(s),a})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pe(t){return ke.apply(this,arguments)}function ke(){return(ke=c(u().mark((function t(e){var n,r,s,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,s[we.POOL_TOKEN_CREATE]);case 6:if(a=t.sent,"number"==typeof(o=a["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Le(){return(Le=c(u().mark((function t(e){var n,r,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Pe({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",be.v1_1.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Fe,Re={generateTxns:function(t){return Ee.apply(this,arguments)},signTxns:function(t){return Ne.apply(this,arguments)},execute:function(t){return Le.apply(this,arguments)},getBootstrapFundingTxnAmount:Oe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Fe||(Fe={}));var Be=5,Me=6;function Ve(t,e){return Ce.apply(this,arguments)}function Ce(){return(Ce=c(u().mark((function e(n,r){var s,a,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(a=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,a,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Qe(t,e){return qe.apply(this,arguments)}function qe(){return(qe=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Ve(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=dt(a,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,be.v2.getPoolInfo({client:s,network:a,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Ht.generateLogicSigAccountForPool({network:a,asset1ID:g,asset2ID:x}),A=I.address(),h=V(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ue(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:je(h),suggestedParams:p}),(w=[])[Fe.FUNDING_TXN]=y,w[Fe.VALIDATOR_APP_CALL]=T,_=r.default.assignGroupID(w),(b=[])[Fe.FUNDING_TXN]={txn:_[Fe.FUNDING_TXN],signers:[c]},b[Fe.VALIDATOR_APP_CALL]={txn:_[Fe.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function je(t){return function(t,e){var n=Zt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ue(t)+1e5}function Ue(e){return((e?Be:Me)+1)*t.ALGORAND_MIN_TX_FEE}function Ge(){return(Ge=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Ht.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Fe.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),s=n.txID,a=n.blob;return I.push(s),a})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function We(){return(We=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Ve(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ie.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",be.v2.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var ze,Je={generateTxns:function(t){return Xe.apply(this,arguments)},signTxns:function(t){return Ge.apply(this,arguments)},execute:function(t){return We.apply(this,arguments)},getBootstrapFundingTxnAmount:je,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+je(e)}};var Ye,Ke=(f(ze={},S.V1_1,Re),f(ze,S.V2,Je),f(ze,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Re.generateTxns(t):Je.generateTxns(t)})),f(ze,"signTxns",(function(t){return t.contractVersion===S.V1_1?Re.signTxns(t):Je.signTxns(t)})),f(ze,"execute",(function(t){return t.contractVersion===S.V1_1?Re.execute(t):Je.execute(t)})),f(ze,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Re.getBootstrapFundingTxnAmount(n):Je.getBootstrapFundingTxnAmount(n)})),ze);exports.V1_1AddLiquidityTxnIndices=void 0,(Ye=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ye.FEE_TXN=0]="FEE_TXN",Ye[Ye.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ye[Ye.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ye[Ye.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ye[Ye.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var He,Ze,$e,tn,en=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(tn=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",tn.FLEXIBLE="flexible",tn.INITIAL="initial";var nn,rn=(f(He={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(He,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(He,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),He),sn=(f(Ze={},exports.V2AddLiquidityType.INITIAL,1),f(Ze,exports.V2AddLiquidityType.SINGLE,2),f(Ze,exports.V2AddLiquidityType.FLEXIBLE,2),Ze),an=(f($e={},exports.V2AddLiquidityType.INITIAL,3),f($e,exports.V2AddLiquidityType.FLEXIBLE,3),f($e,exports.V2AddLiquidityType.SINGLE,2),$e);function on(t){var e,n,r,s=t.reserves,a=t.totalFeeShare,o=t.asset1,u=t.asset2,i=s.asset1*s.asset2,c=s.asset1+BigInt(o.amount),p=s.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*s.issuedLiquidity*s.issuedLiquidity/i))))),f=d-s.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(o.amount)-m,I=BigInt(u.amount)-g;if(x>I){var A=x;r=un(A,a),e={id:o.id,amount:A+r,decimals:o.decimals,reserves:s.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:u.decimals,reserves:s.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=un(h,a),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:s.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:o.decimals,reserves:s.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Xt({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function un(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function cn(e){return(sn[e]+1)*t.ALGORAND_MIN_TX_FEE}var pn=it("add_liquidity"),ln=(f(nn={},S.V1_1,[it("mint")]),f(nn,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[pn,it("single")],FLEXIBLE_MODE:[pn,it("flexible")]}),nn);function dn(){return(dn=c(u().mark((function t(e){var n,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,S.V1_1),appArgs:ln.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:s,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fn(){return(fn=c(u().mark((function t(e){var n,s,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,o=n.account,t.next=4,a([s]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=s.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(){return(mn=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(s[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,ae({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[a]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=at(s),x=ot(s),t.next=17,ae({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var gn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,s=t.asset2In;if(0n===n.issuedLiquidity){var a=BigInt(Math.floor(Math.sqrt(Number(r)*Number(s))));if(a<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(a,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(s),poolTokenID:e.poolTokenID,poolTokenOut:a-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(s)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Mn(){return(Mn=c(u().mark((function t(e){var n,s,a,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Rt(o(o({},e),{},{route:e.quote.route})));case 2:if(n=e.client,s=e.initiatorAddr,a=e.slippage,i=e.swapType,c=e.quote,p=c.quoteWithPool,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=V(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:H("positive",a,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",a,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:l.validatorAppID,appArgs:[vt,wt[i],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Vn(i),(w=[])[It.INPUT_TXN]=y,w[It.APP_CALL_TXN]=v,_=r.default.assignGroupID(w),t.abrupt("return",[{txn:_[It.INPUT_TXN],signers:[s]},{txn:_[It.APP_CALL_TXN],signers:[s]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Vn(e){return(yt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Cn(){return(Cn=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return s=e.client,a=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,rt(s,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Qe(s,o);case 10:return m=t.sent,g=a.type===exports.SwapQuoteType.Direct?{id:a.quoteWithPool.quote.assetInID,amount:a.quoteWithPool.quote.assetInAmount}:{id:Number(a.asset_in_id),amount:Number(a.route[0].quote.amount_in.amount)},x=a.type===exports.SwapQuoteType.Direct?a.quoteWithPool.quote.assetOutID:Number(a.asset_out_id),I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:a,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Qn(){return(Qn=c(u().mark((function t(e){var n,r,s,a,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.pool,s=e.asset,a=e.decimals,o=e.network,i=e.isSwapRouterEnabled,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,qn({pool:r,assetIn:s,decimals:a,isSwapRouterEnabled:i,network:o});case 4:c=t.sent,t.next=10;break;case 7:return t.next=9,jn({pool:r,assetOut:s,decimals:a,isSwapRouterEnabled:i,network:o});case 9:c=t.sent;case 10:return t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function qn(t){return Xn.apply(this,arguments)}function Xn(){return(Xn=c(u().mark((function t(e){var n,r,s,a,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,s=e.pool,a=e.isSwapRouterEnabled,i=e.network,s.status===exports.PoolStatus.READY||a){t.next=3;break}throw new F({pool:s,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=s.totalFeeShare,n.id!==s.asset1ID){t.next=11;break}l=s.asset2ID,d=s.asset1Reserves,f=s.asset2Reserves,t.next=18;break;case 11:if(n.id!==s.asset2ID){t.next=17;break}l=s.asset1ID,d=s.asset2Reserves,f=s.asset1Reserves,t.next=18;break;case 17:throw new F({pool:s,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(s.account.address(),"."));case 18:if(m=Gn({inputSupply:d,outputSupply:f,swapInputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapOutputAmount,x=m.totalFeeAmount,I=m.priceImpact,A={assetInID:n.id,assetInAmount:c,assetOutID:l,assetOutAmount:g,swapFee:Number(x),rate:Z(r.assetOut,Number(g))/Z(r.assetIn,Number(c)),priceImpact:I},!a){t.next=26;break}return t.next=23,Ct({amount:n.amount,assetInID:Number(n.id),assetOutID:l,swapType:exports.SwapType.FixedInput,network:i});case 23:if(!((h=t.sent).route.length>1&&BigInt(Nt(h.route).amount)>A.assetOutAmount)){t.next=26;break}return t.abrupt("return",o(o({},h),{},{type:exports.SwapQuoteType.Router}));case 26:if(!(g>f)){t.next=28;break}throw new Bn;case 28:return t.abrupt("return",{quoteWithPool:{pool:s,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function jn(t){return Un.apply(this,arguments)}function Un(){return(Un=c(u().mark((function t(e){var n,r,s,a,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,s=e.pool,a=e.isSwapRouterEnabled,i=e.network,s.status===exports.PoolStatus.READY||a){t.next=3;break}throw new F({pool:s,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(c=BigInt(n.amount),p=s.totalFeeShare,n.id!==s.asset1ID){t.next=11;break}l=s.asset2ID,d=s.asset2Reserves,f=s.asset1Reserves,t.next=18;break;case 11:if(n.id!==s.asset2ID){t.next=17;break}l=s.asset1ID,d=s.asset1Reserves,f=s.asset2Reserves,t.next=18;break;case 17:throw new F({pool:s,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(s.account.address(),"."));case 18:if(m=Wn({inputSupply:d,outputSupply:f,swapOutputAmount:c,totalFeeShare:p,decimals:r}),g=m.swapInputAmount,x=m.totalFeeAmount,I=m.priceImpact,A={assetInID:l,assetInAmount:g,assetOutID:n.id,assetOutAmount:c,swapFee:Number(x),rate:Z(r.assetOut,Number(c))/Z(r.assetIn,Number(g)),priceImpact:I},!a){t.next=26;break}return t.next=23,Ct({amount:n.amount,assetInID:l,assetOutID:n.id,swapType:exports.SwapType.FixedOutput,network:i});case 23:if(!((h=t.sent).route.length>1&&BigInt(Nt(h.route).amount)f)){t.next=28;break}throw new Bn;case 28:return t.abrupt("return",{quoteWithPool:{pool:s,quote:A},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Gn(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,s=t.totalFeeShare,a=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:s})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,s=n-BigInt(e*n/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:u,totalFeeAmount:o,priceImpact:Xt({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:a.assetIn},assetOut:{amount:u,decimals:a.assetOut}})}}function Wn(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,s=t.totalFeeShare,a=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,s=BigInt(e*n/(n-r))-e;return s+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:o,totalFeeShare:s}),i=o+u;return{swapInputAmount:i,totalFeeAmount:u,priceImpact:Xt({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:a.assetIn},assetOut:{amount:r,decimals:a.assetOut}})}}var zn={getQuote:function(t){return Qn.apply(this,arguments)},getFixedInputSwapQuote:qn,getFixedOutputSwapQuote:jn,generateTxns:function(t){return Mn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Cn.apply(this,arguments)},calculateFixedInputSwap:Gn};function Jn(t,e){var n,r,s=BigInt(t),a=e.issuedLiquidity;return a>s+BigInt(1e3)?(n=s*e.asset1/a,r=s*e.asset2/a):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Yn(){return(Yn=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.account.address(),m=a.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Pn,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[On.ASSET_TRANSFER_TXN]=g,I[On.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[On.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[On.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kn(){return(Kn=c(u().mark((function e(n){var s,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.asset1ID,m=a.asset2ID,g=a.account.address(),x=a.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Pn,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[On.ASSET_TRANSFER_TXN]=T,v[On.APP_CALL_TXN]=y,w=r.default.assignGroupID(v),e.abrupt("return",[{txn:w[On.ASSET_TRANSFER_TXN],signers:[o]},{txn:w[On.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function t(e){var n,r,s,a,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,s=e.txGroup,a=e.signedTxns,t.next=3,rt(r,[a]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Qe(r,s);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Zn,$n,tr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,s=BigInt(r),a=Jn(s,n),o=a.asset1OutputAmount,u=a.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:s}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,s=t.poolTokenIn,a=t.assetOutID,o=t.decimals,u=BigInt(s),i=Jn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(a===n.asset1ID){var d=zn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(a!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=zn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Yn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Kn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Hn.apply(this,arguments)}},er=(f(Zn={},S.V1_1,Rn),f(Zn,S.V2,tr),Zn),nr=3n,rr=1000n;function sr(){return(sr=c(u().mark((function t(e){var n,s,a,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,t.next=3,a([s]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=s.map((function(t,e){return e===$n.FEE_TXN_INDEX?c:e===$n.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,a,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,a=e.quote,o=e.swapType,i=e.slippage,c=e.initiatorAddr,p=a.pool,l=a.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:a},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",i,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",i,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),w=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:s,suggestedParams:x}),_=r.default.assignGroupID([w,A,T,v]),t.abrupt("return",[{txn:_[0],signers:[c]},{txn:_[1],signers:[m]},{txn:_[2],signers:[c]},{txn:_[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(t){var e=t.pool,n=t.reserves,r=t.assetIn,s=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var a,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)a=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));a=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*nr/rr,l=i-u*i/(u+(c-p));if(l>i)throw new Bn;var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:a,assetOutAmount:l,swapFee:Number(p),rate:qt(d),priceImpact:Xt(o({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,quoteWithPool:{pool:e,quote:f}}}function ur(t){return ir.apply(this,arguments)}function ir(){return(ir=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,ae({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[s]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,ae({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cr(t){var e=t.pool,n=t.reserves,r=t.assetOut,s=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var a,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)a=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));a=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new Bn;var c=o*u/(u-i)-o,p=c*rr/(rr-nr),l=p-c,d=Z(s.assetOut,Number(i))/Z(s.assetIn,Number(p)),f=Z(s.assetOut,Number(u))/Z(s.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:a,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,quoteWithPool:{pool:e,quote:g}}}function pr(t){return lr.apply(this,arguments)}function lr(){return(lr=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,ae({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[s]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,ae({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,a.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:a.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dr(){return(dr=c(u().mark((function t(e){var n,r,s,a,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,s=e.swapType,a=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:s,txGroup:a},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:a[$n.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:a[$n.ASSET_IN_TXN_INDEX].txn.amount},l={id:a[$n.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:a[$n.ASSET_OUT_TXN_INDEX].txn.amount},s!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,ur({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,pr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(a),fees:at(a)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}($n||($n={}));var fr,mr={getQuote:function(t,e,n,r,s){return t===exports.SwapType.FixedInput?or({pool:e,reserves:n,assetIn:r,decimals:s}):cr({pool:e,reserves:n,assetOut:r,decimals:s})},getFixedInputSwapQuote:or,getFixedOutputSwapQuote:cr,generateTxns:function(t){return ar.apply(this,arguments)},signTxns:function(t){return sr.apply(this,arguments)},execute:function(t){return dr.apply(this,arguments)},executeFixedOutputSwap:pr},gr=4*t.ALGORAND_MIN_TX_FEE;function xr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Bn})))throw new Bn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function Ir(){return Ir=c(u().mark((function t(e){var n,r,s,a,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,a=e.pools,o=e.isSwapRouterEnabled,i=a.map((function(t){return new Promise(function(){var a=c(u().mark((function a(i,c){var p,l;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},a.prev=1,t.info.contractVersion!==S.V1_1){a.next=6;break}p=mr.getFixedInputSwapQuote(l),a.next=9;break;case 6:return a.next=8,zn.getFixedInputSwapQuote(l);case 8:p=a.sent;case 9:i(p),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(1),c(a.t0);case 15:case"end":return a.stop()}}),a,null,[[1,12]])})));return function(t,e){return a.apply(this,arguments)}}())})),t.next=4,xr(i);case 4:return p=t.sent,t.abrupt("return",Tr(p));case 6:case"end":return t.stop()}}),t)}))),Ir.apply(this,arguments)}function Ar(){return Ar=c(u().mark((function t(e){var n,r,s,a,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,a=e.pools,o=e.isSwapRouterEnabled,i=a.map((function(t){return new Promise(function(){var a=c(u().mark((function a(i,c){var p,l;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(s.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},a.prev=1,t.info.contractVersion!==S.V1_1){a.next=6;break}p=mr.getFixedOutputSwapQuote(l),a.next=9;break;case 6:return a.next=8,zn.getFixedOutputSwapQuote(l);case 8:p=a.sent;case 9:i(p),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(1),c(a.t0);case 15:case"end":return a.stop()}}),a,null,[[1,12]])})));return function(t,e){return a.apply(this,arguments)}}())})),t.next=4,xr(i);case 4:return p=t.sent,t.abrupt("return",Tr(p));case 6:case"end":return t.stop()}}),t)}))),Ar.apply(this,arguments)}function hr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.quote.rate:Et(t.route)}function Tr(t){for(var e=t[0],n=hr(e),r=1;rn&&(e=s,n=a)}return e}function yr(t){return t.type===exports.SwapQuoteType.Direct?t.quoteWithPool.pool.contractVersion:S.V2}var vr=(f(fr={},S.V1_1,mr),f(fr,S.V2,zn),f(fr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ye(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return Ir.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return Ar.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(fr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&yr(t.quote)===S.V1_1?mr.generateTxns(o(o({},t),{},{quote:t.quote.quoteWithPool})):zn.generateTxns(t)})),f(fr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&yr(t.quote)===S.V1_1){var e=t.quote.quoteWithPool.pool;return mr.signTxns(o({pool:e},t))}return zn.signTxns(t)})),f(fr,"execute",(function(t){return t.contractVersion===S.V1_1?mr.execute(t):zn.execute(t)})),fr);function wr(){return(wr=c(u().mark((function t(e){var n,r,s,a,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.initiatorSigner,t.prev=1,t.next=4,_r({txGroup:s,pool:r,initiatorSigner:a});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:at(s),confirmedRound:d,txnID:l,groupID:ot(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function _r(t){return br.apply(this,arguments)}function br(){return(br=c(u().mark((function t(e){var n,s,a,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.pool,a=e.initiatorSigner,t.next=3,a([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=s.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Dr(){return Dr=c(u().mark((function t(e){var n,s,a,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.data,a=e.initiatorSigner,t.prev=1,o=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:at(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,a(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var s=c(u().mark((function s(a,o){var c,p,l,d,f,m;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:return s.prev=0,c=t.txns.map((function(n,s){return 0===s?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),s.next=4,rt(n,[c]);case 4:p=s.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,a({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(0),o(s.t0);case 15:case"end":return s.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Dr.apply(this,arguments)}function Sr(){return(Sr=c(u().mark((function t(e){var n,a,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=a.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:a.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[c],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:s,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Er(e){var n=e.suggestedParams,r=e.stakingAppID,s=e.initiatorAddr,a=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:s,suggestedParams:n,foreignAssets:[a],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:q([it("tinymanStaking/v1:b"),c,t.encodeUint64(a),i])})}function Or(){return(Or=c(u().mark((function e(n){var r,s,a,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,a=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Er({suggestedParams:l,stakingAppID:s,program:a,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[o],accounts:[a.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=En,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=Ke,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=er,exports.Swap=vr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Nn,exports.V2AddLiquidityTxnIndices=rn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=ee,exports.calculatePriceImpact=Xt,exports.calculateSwapRate=qt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=be,exports.prepareCommitTransactions=function(t){return Or.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Dr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return wr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=at,exports.tinymanContract_v1_1=Jt,exports.tinymanContract_v2=Ht,exports.tinymanJSSDKConfig=pt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",s=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,s=Object.create(a.prototype),o=new _(r||[]);return s._invoke=function(t,e,n){var r="suspendedStart";return function(a,s){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw s;return D()}for(n.method=a,n.arg=s;;){var o=n.delegate;if(o){var u=y(o,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,o),s}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(b([])));I&&I!==e&&n.call(I,a)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,s,o,u){var i=p(t[a],t,s);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,u)}),(function(t){r("throw",t,o,u)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function s(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(s,s):s()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function w(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function _(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function b(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,s=function e(){for(;++r=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),w(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;w(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",k="logic eval error:",P="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function Q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[s?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function X(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=D(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var j=100000n,U=100000n,q=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return j+U*BigInt(o.length)+q*BigInt(s.length+a.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(nt(e),2),s=a[0],o=a[1],u=v(nt(Math.round(Number(et(s,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return at.apply(this,arguments)}function at(){return(at=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function st(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht,Tt,yt=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),vt=it("swap"),wt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),_t={ASSET_OPT_IN:it("asset_opt_in")},bt={testnet:159521633,mainnet:0},Dt=(f(ht={},exports.SwapType.FixedInput,7),f(ht,exports.SwapType.FixedOutput,8),ht),St={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function Et(t){var e=Pt(t),n=e.assetIn,r=e.assetOut;return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Ot(t){if(!bt[t])throw new Error("Unknown network or network not supported: ".concat(t));return bt[t]}function Nt(t){return t[t.length-1].quote.amount_out}function kt(t){return t[0].quote.amount_in}function Pt(t){return{assetIn:kt(t),assetOut:Nt(t)}}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function e(n){var a,s,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[_t.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Rt(t){return Bt.apply(this,arguments)}function Bt(){return(Bt=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_,b,D,E,O;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(kt(c).amount),Number(Nt(c).amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Ot(o),w=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(_=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[vt,wt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Dt[i]+1),b=[w,_],e.next=15,Mt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((D=e.sent).length>0)){e.next=21;break}return e.next=19,Lt({client:s,accountAddress:a,assetIDs:D,routerAppID:v});case 19:E=e.sent,b.unshift(E);case 21:return O=r.default.assignGroupID(b),e.abrupt("return",O.map((function(t){return{txn:t,signers:[a]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(St[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}function Xt(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function jt(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=Xt({assetIn:r,assetOut:a}),o=Z(a.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(s/o-1))}function Ut(t){if(t.type===exports.SwapQuoteType.Router){var e=kt(t.data.route);return{id:V(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function qt(t){if(t.type===exports.SwapQuoteType.Router){var e=Nt(t.data.route);return{id:V(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function Gt(t){return t.type===exports.SwapQuoteType.Direct?t.data.pool.contractVersion:S.V2}exports.SwapQuoteType=void 0,(Tt=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",Tt.Router="router";var Wt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},zt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Jt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Yt=new(function(n){m(a,Jt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:ne(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function ee(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ne(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var re,ae,se=it("e");function oe(t){return ue.apply(this,arguments)}function ue(){return(ue=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=Q({stateArray:A}),T=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.poolTokenID)])),w=h[T],_=h[y],b=h[v],"bigint"==typeof w&&(l=w),"bigint"==typeof _&&(d=_),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ie(){return(ie=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=Q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mle)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(le,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ge(){return ge=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,te(n,r);case 6:return o=t.sent,i=ee(o,dt(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[ce[S.V1_1].asset1],asset2ID:i[ce[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ge.apply(this,arguments)}var xe=Object.freeze({__proto__:null,getPoolInfo:de,getPoolReserves:function(t,e){return me.apply(this,arguments)},getPoolAssets:function(t){return ge.apply(this,arguments)}});function Ie(t){return Ae.apply(this,arguments)}function Ae(){return(Ae=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=$t(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(a,s),t.next=8,te(n,p);case 8:return d=t.sent,f=ee(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[ce.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ce.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ce.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ce.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ce.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ce.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ce.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ce.v2.totalFeeShare]),m.poolTokenID=Number(f[ce.v2.poolTokenID]),m.asset1ID=Number(f[ce.v2.asset1]),m.asset2ID=Number(f[ce.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function he(){return(he=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,te(e,n.account.address());case 2:return r=t.sent,a=ee(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[ce.v2.asset1Reserves]),s.asset2=BigInt(a[ce.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[ce.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(){return(Te=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,te(n,r);case 3:return s=t.sent,o=ee(s,dt(a,S.V2)),i=null,o&&(i={asset1ID:o[ce[S.V2].asset1],asset2ID:o[ce[S.V2].asset2],poolTokenID:o[ce[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ye=Object.freeze({__proto__:null,getPoolInfo:Ie,getPoolReserves:function(t,e){return he.apply(this,arguments)},getPoolAssets:function(t){return Te.apply(this,arguments)}});function ve(t){return Boolean(t&&!(t.asset1+t.asset2))}var we,_e,be=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ve(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:ve,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([de(t),Ie(t)])}}),De=o((f(we={},S.V1_1,o(o({},xe),be)),f(we,S.V2,o(o({},ye),be)),we),be);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(_e||(_e={}));var Se=96e4,Ee=859e3;function Oe(){return(Oe=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,b,D,E,N,k;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(a,S.V1_1),T=Yt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),w=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),_=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Ne(A),suggestedParams:c}),(E=[])[_e.FUNDING_TXN]=D,E[_e.VALIDATOR_APP_CALL]=w,E[_e.POOL_TOKEN_CREATE]=_,E[_e.ASSET1_OPT_IN]=b,A||(E[_e.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),k=[{txn:N[_e.FUNDING_TXN],signers:[i]},{txn:N[_e.VALIDATOR_APP_CALL],signers:[y]},{txn:N[_e.POOL_TOKEN_CREATE],signers:[y]},{txn:N[_e.ASSET1_OPT_IN],signers:[y]}],N[_e.ASSET2_OPT_IN]&&k.push({txn:N[_e.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",k);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ne(t){return t?Se:Ee}function ke(){return(ke=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Yt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===_e.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pe(t){return Le.apply(this,arguments)}function Le(){return(Le=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,a[_e.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Fe(){return(Fe=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Pe({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",De.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Re,Be={generateTxns:function(t){return Oe.apply(this,arguments)},signTxns:function(t){return ke.apply(this,arguments)},execute:function(t){return Fe.apply(this,arguments)},getBootstrapFundingTxnAmount:Ne};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Re||(Re={}));var Me=5,Ce=6;function Ve(t,e){return Qe.apply(this,arguments)}function Qe(){return(Qe=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xe(t,e){return je.apply(this,arguments)}function je(){return(je=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Ve(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ue(){return(Ue=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=dt(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,De.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Zt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ge(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:qe(h),suggestedParams:p}),(w=[])[Re.FUNDING_TXN]=y,w[Re.VALIDATOR_APP_CALL]=T,_=r.default.assignGroupID(w),(b=[])[Re.FUNDING_TXN]={txn:_[Re.FUNDING_TXN],signers:[c]},b[Re.VALIDATOR_APP_CALL]={txn:_[Re.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qe(t){return function(t,e){var n=$t(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ge(t)+1e5}function Ge(e){return((e?Me:Ce)+1)*t.ALGORAND_MIN_TX_FEE}function We(){return(We=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Zt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Re.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ze(){return(ze=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Ve(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ce.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",De.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Je,Ye={generateTxns:function(t){return Ue.apply(this,arguments)},signTxns:function(t){return We.apply(this,arguments)},execute:function(t){return ze.apply(this,arguments)},getBootstrapFundingTxnAmount:qe,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+qe(e)}};var Ke,He=(f(Je={},S.V1_1,Be),f(Je,S.V2,Ye),f(Je,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Be.generateTxns(t):Ye.generateTxns(t)})),f(Je,"signTxns",(function(t){return t.contractVersion===S.V1_1?Be.signTxns(t):Ye.signTxns(t)})),f(Je,"execute",(function(t){return t.contractVersion===S.V1_1?Be.execute(t):Ye.execute(t)})),f(Je,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Be.getBootstrapFundingTxnAmount(n):Ye.getBootstrapFundingTxnAmount(n)})),Je);exports.V1_1AddLiquidityTxnIndices=void 0,(Ke=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ke.FEE_TXN=0]="FEE_TXN",Ke[Ke.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ke[Ke.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ke[Ke.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ke[Ke.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Ze,$e,tn,en,nn=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(en=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",en.FLEXIBLE="flexible",en.INITIAL="initial";var rn,an=(f(Ze={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Ze,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Ze,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Ze),sn=(f($e={},exports.V2AddLiquidityType.INITIAL,1),f($e,exports.V2AddLiquidityType.SINGLE,2),f($e,exports.V2AddLiquidityType.FLEXIBLE,2),$e),on=(f(tn={},exports.V2AddLiquidityType.INITIAL,3),f(tn,exports.V2AddLiquidityType.FLEXIBLE,3),f(tn,exports.V2AddLiquidityType.SINGLE,2),tn);function un(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(o.amount)-m,I=BigInt(u.amount)-g;if(x>I){var A=x;r=cn(A,s),e={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=cn(h,s),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:jt({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function cn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function pn(e){return(sn[e]+1)*t.ALGORAND_MIN_TX_FEE}var ln=it("add_liquidity"),dn=(f(rn={},S.V1_1,[it("mint")]),f(rn,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[ln,it("single")],FLEXIBLE_MODE:[ln,it("flexible")]}),rn);function fn(){return(fn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(s,S.V1_1),appArgs:dn.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(){return(mn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function gn(){return(gn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,oe({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=st(a),x=ot(a),t.next=17,oe({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var xn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Cn(){return(Cn=c(u().mark((function t(e){var n,a,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Rt(o(o({},e),{},{route:e.quote.data.route})));case 2:if(n=e.client,a=e.initiatorAddr,s=e.slippage,i=e.swapType,c=e.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=C(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:H("positive",s,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",s,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:l.validatorAppID,appArgs:[vt,wt[i],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Vn(i),(w=[])[It.INPUT_TXN]=y,w[It.APP_CALL_TXN]=v,_=r.default.assignGroupID(w),t.abrupt("return",[{txn:_[It.INPUT_TXN],signers:[a]},{txn:_[It.APP_CALL_TXN],signers:[a]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Vn(e){return(yt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Qn(){return(Qn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,rt(a,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Xe(a,o);case 10:return m=t.sent,g=Ut(s),x=qt(s).id,I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xn(){return(Xn=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.pool,a=e.asset,s=e.decimals,o=e.network,i=e.isSwapRouterEnabled,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,jn({pool:r,assetIn:a,decimals:s,isSwapRouterEnabled:i,network:o});case 4:c=t.sent,t.next=10;break;case 7:return t.next=9,qn({pool:r,assetOut:a,decimals:s,isSwapRouterEnabled:i,network:o});case 9:c=t.sent;case 10:return t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function jn(t){return Un.apply(this,arguments)}function Un(){return(Un=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,o=e.network,a.status===exports.PoolStatus.READY||s){t.next=3;break}throw new F({pool:a,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(n.amount),c=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}p=a.asset2ID,l=a.asset1Reserves,d=a.asset2Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}p=a.asset1ID,l=a.asset2Reserves,d=a.asset1Reserves,t.next=18;break;case 17:throw new F({pool:a,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(f=Wn({inputSupply:l,outputSupply:d,swapInputAmount:i,totalFeeShare:c,decimals:r}),m=f.swapOutputAmount,g=f.totalFeeAmount,x=f.priceImpact,I={assetInID:n.id,assetInAmount:i,assetOutID:p,assetOutAmount:m,swapFee:Number(g),rate:Z(r.assetOut,Number(m))/Z(r.assetIn,Number(i)),priceImpact:x},!s){t.next=26;break}return t.next=23,Vt({amount:n.amount,assetInID:Number(n.id),assetOutID:p,swapType:exports.SwapType.FixedInput,network:o});case 23:if(!((A=t.sent).route.length>1&&BigInt(Nt(A.route).amount)>I.assetOutAmount)){t.next=26;break}return t.abrupt("return",{data:A,type:exports.SwapQuoteType.Router});case 26:if(!(m>d)){t.next=28;break}throw new Mn;case 28:return t.abrupt("return",{data:{pool:a,quote:I},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function qn(t){return Gn.apply(this,arguments)}function Gn(){return(Gn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,o=e.network,a.status===exports.PoolStatus.READY||s){t.next=3;break}throw new F({pool:a,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(n.amount),c=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}p=a.asset2ID,l=a.asset2Reserves,d=a.asset1Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}p=a.asset1ID,l=a.asset1Reserves,d=a.asset2Reserves,t.next=18;break;case 17:throw new F({pool:a,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(f=zn({inputSupply:l,outputSupply:d,swapOutputAmount:i,totalFeeShare:c,decimals:r}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact,I={assetInID:p,assetInAmount:m,assetOutID:n.id,assetOutAmount:i,swapFee:Number(g),rate:Z(r.assetOut,Number(i))/Z(r.assetIn,Number(m)),priceImpact:x},!s){t.next=26;break}return t.next=23,Vt({amount:n.amount,assetInID:p,assetOutID:n.id,swapType:exports.SwapType.FixedOutput,network:o});case 23:if(!((A=t.sent).route.length>1&&BigInt(Nt(A.route).amount)d)){t.next=28;break}throw new Mn;case 28:return t.abrupt("return",{data:{pool:a,quote:I},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Wn(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:u,totalFeeAmount:o,priceImpact:jt({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:u,decimals:s.assetOut}})}}function zn(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:o,totalFeeShare:a}),i=o+u;return{swapInputAmount:i,totalFeeAmount:u,priceImpact:jt({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}})}}var Jn={getQuote:function(t){return Xn.apply(this,arguments)},getFixedInputSwapQuote:jn,getFixedOutputSwapQuote:qn,generateTxns:function(t){return Cn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Qn.apply(this,arguments)},calculateFixedInputSwap:Wn};function Yn(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Kn(){return(Kn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Pn,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[Nn.ASSET_TRANSFER_TXN]=g,I[Nn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[Nn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[Nn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Pn,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[Nn.ASSET_TRANSFER_TXN]=T,v[Nn.APP_CALL_TXN]=y,w=r.default.assignGroupID(v),e.abrupt("return",[{txn:w[Nn.ASSET_TRANSFER_TXN],signers:[o]},{txn:w[Nn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,rt(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Xe(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var $n,tr,er={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Yn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=Yn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Jn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Jn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Kn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Hn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Zn.apply(this,arguments)}},nr=(f($n={},S.V1_1,Bn),f($n,S.V2,er),$n),rr=3n,ar=1000n;function sr(){return(sr=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===tr.FEE_TXN_INDEX?c:e===tr.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.quote,o=e.swapType,i=e.slippage,c=e.initiatorAddr,p=s.pool,l=s.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:s},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",i,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",i,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),w=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:a,suggestedParams:x}),_=r.default.assignGroupID([w,A,T,v]),t.abrupt("return",[{txn:_[0],signers:[c]},{txn:_[1],signers:[m]},{txn:_[2],signers:[c]},{txn:_[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ur(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*rr/ar,l=i-u*i/(u+(c-p));if(l>i)throw new Mn;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:Xt(d),priceImpact:jt(o({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function ir(t){return cr.apply(this,arguments)}function cr(){return(cr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,oe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,oe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new Mn;var c=o*u/(u-i)-o,p=c*ar/(ar-rr),l=p-c,d=Z(a.assetOut,Number(i))/Z(a.assetIn,Number(p)),f=Z(a.assetOut,Number(u))/Z(a.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function lr(t){return dr.apply(this,arguments)}function dr(){return(dr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,oe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,oe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fr(){return(fr=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[tr.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[tr.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[tr.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[tr.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,ir({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,lr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(s),fees:st(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(tr||(tr={}));var mr,gr={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?ur({pool:e,reserves:n,assetIn:r,decimals:a}):pr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:ur,getFixedOutputSwapQuote:pr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){return sr.apply(this,arguments)},execute:function(t){return fr.apply(this,arguments)},executeFixedOutputSwap:lr},xr=4*t.ALGORAND_MIN_TX_FEE;function Ir(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Mn})))throw new Mn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function Ar(){return Ar=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var s=c(u().mark((function s(i,c){var p,l;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p=gr.getFixedInputSwapQuote(l),s.next=9;break;case 6:return s.next=8,Jn.getFixedInputSwapQuote(l);case 8:p=s.sent;case 9:i(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,Ir(i);case 4:return p=t.sent,t.abrupt("return",yr(p));case 6:case"end":return t.stop()}}),t)}))),Ar.apply(this,arguments)}function hr(){return hr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var s=c(u().mark((function s(i,c){var p,l;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p=gr.getFixedOutputSwapQuote(l),s.next=9;break;case 6:return s.next=8,Jn.getFixedOutputSwapQuote(l);case 8:p=s.sent;case 9:i(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,Ir(i);case 4:return p=t.sent,t.abrupt("return",yr(p));case 6:case"end":return t.stop()}}),t)}))),hr.apply(this,arguments)}function Tr(t){return t.type===exports.SwapQuoteType.Direct?t.data.quote.rate:Et(t.data.route)}function yr(t){for(var e=t[0],n=Tr(e),r=1;rn&&(e=a,n=s)}return e}var vr=(f(mr={},S.V1_1,gr),f(mr,S.V2,Jn),f(mr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ve(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return Ar.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return hr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(mr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&Gt(t.quote)===S.V1_1?gr.generateTxns(o(o({},t),{},{quote:t.quote.data})):Jn.generateTxns(t)})),f(mr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&Gt(t.quote)===S.V1_1){var e=t.quote.data.pool;return gr.signTxns(o({pool:e},t))}return Jn.signTxns(t)})),f(mr,"execute",(function(t){return t.contractVersion===S.V1_1?gr.execute(t):Jn.execute(t)})),mr);function wr(){return(wr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,_r({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:st(a),confirmedRound:d,txnID:l,groupID:ot(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function _r(t){return br.apply(this,arguments)}function br(){return(br=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Dr(){return Dr=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:st(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,rt(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Dr.apply(this,arguments)}function Sr(){return(Sr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Er(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:X([it("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function Or(){return(Or=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Er({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=On,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=He,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=nr,exports.Swap=vr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=kn,exports.V2AddLiquidityTxnIndices=an,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=ne,exports.calculatePriceImpact=jt,exports.calculateSwapRate=Xt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=De,exports.prepareCommitTransactions=function(t){return Or.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Dr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return wr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=st,exports.tinymanContract_v1_1=Yt,exports.tinymanContract_v2=Zt,exports.tinymanJSSDKConfig=pt; diff --git a/dist/swap/common/utils.d.ts b/dist/swap/common/utils.d.ts index b130eb48..726b7b3a 100644 --- a/dist/swap/common/utils.d.ts +++ b/dist/swap/common/utils.d.ts @@ -1,3 +1,4 @@ +import { ContractVersionValue } from "../../contract/types"; import { AssetWithAmountAndDecimals, AssetWithIdAndAmount } from "../../util/asset/assetModels"; import { SwapQuote } from "../types"; declare function calculateSwapRate({ assetIn, assetOut }: { @@ -17,5 +18,5 @@ declare function getAssetInAndAssetOutFromSwapQuote(quote: SwapQuote): { assetIn: AssetWithIdAndAmount; assetOut: AssetWithIdAndAmount; }; -declare function getSwapQuoteContractVersion(quote: SwapQuote): import("../..").ContractVersionValue; +declare function getSwapQuoteContractVersion(quote: SwapQuote): ContractVersionValue; export { calculateSwapRate, calculatePriceImpact, getSwapQuotePriceImpact, getAssetInFromSwapQuote, getAssetOutFromSwapQuote, getAssetInAndAssetOutFromSwapQuote, getSwapQuoteContractVersion }; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index 6ff9a4ff..e3621e94 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -3,6 +3,10 @@ import { AssetWithIdAndAmount, TinymanAnalyticsApiAsset } from "../util/asset/as import { SupportedNetwork } from "../util/commonTypes"; import { PoolReserves, V1PoolInfo, V2PoolInfo } from "../util/pool/poolTypes"; import { SwapType } from "./constants"; +export declare enum SwapQuoteType { + Direct = "direct", + Router = "router" +} export interface DirectSwapQuote { /** The ID of the input asset in this quote. */ assetInID: number; @@ -21,71 +25,10 @@ export interface DirectSwapQuote { /** The round that this quote is based on. */ round?: number; } -/** An object containing information about a successfully executed swap. */ -export interface V1SwapExecution { - /** The round that the swap occurred in. */ - round: number; - /** - * The total amount of transaction fees that were spent (in microAlgos) to execute the swap and, - * if applicable, redeem transactions. - */ - fees: number; - /** The ID of the swap's input asset. */ - assetInID: number; - /** The amount of the swap's input asset. */ - assetInAmount: bigint; - /** The ID of the swap's output asset. */ - assetOutID: number; - /** The amount of the swap's output asset. */ - assetOutAmount: bigint; - /** The ID of the transaction. */ - txnID: string; - excessAmount: { - /** Asset ID for which the excess amount can be redeemed with */ - assetID: number; - /** Excess amount for the current swap */ - excessAmountForSwap: bigint; - /** Total excess amount accumulated for the pool asset */ - totalExcessAmount: bigint; - }; - /** The group ID for the transaction group. */ - groupID: string; -} -export interface V2SwapExecution { - /** Can be `undefined` if the execution was successful, but there was an issue while - * extracting the input asset data fron the transaction response */ - assetIn: AssetWithIdAndAmount | undefined; - /** Can be `undefined` if the execution was successful, but there was an issue while - * extracting the output asset data fron the transaction response */ - assetOut: AssetWithIdAndAmount | undefined; - quote: SwapQuote; - txnID: string; - round: number; -} -export declare type GetSwapQuoteParams = { - assetIn: Pick; - assetOut: Pick; - pools: { - info: V1PoolInfo | V2PoolInfo; - reserves: PoolReserves; - }[]; - amount: number | bigint; - type: SwapType; - network: SupportedNetwork; - /** If `true`, the function will also check the quotes that use swap route */ - isSwapRouterEnabled?: boolean; -}; -export interface SwapQuoteWithPool { +export interface DirectSwapQuoteAndPool { quote: DirectSwapQuote; pool: V1PoolInfo | V2PoolInfo; } -export declare type SwapQuote = { - quoteWithPool: SwapQuoteWithPool; - type: SwapQuoteType.Direct; -} | (FetchSwapRouteQuotesResponse & { - type: SwapQuoteType.Router; -}); -export declare type GetSwapQuoteBySwapTypeParams = Omit; export interface SwapRouteAsset { id: string; name: string; @@ -102,28 +45,6 @@ export declare type SwapRoute = { quote: SwapRouterQuote; pool: SwapRoutePool; }[]; -export declare enum SwapQuoteType { - Direct = "direct", - Router = "router" -} -export interface GenerateSwapTxnsParams { - client: Algodv2; - network: SupportedNetwork; - quote: SwapQuote; - swapType: SwapType; - slippage: number; - initiatorAddr: string; -} -export interface GenerateSwapRouterTxnsParams { - client: Algodv2; - initiatorAddr: string; - swapType: SwapType; - route: SwapRoute; - network: SupportedNetwork; -} -export declare type GenerateV1_1SwapTxnsParams = Omit & { - quote: SwapQuoteWithPool; -}; export interface SwapRouterQuote { swap_type: SwapType; amount_in: { @@ -147,7 +68,7 @@ export interface FetchSwapRouteQuotesPayload { amount: string; swap_type: SwapType; } -export declare type FetchSwapRouteQuotesResponse = FetchSwapRouteQuotesPayload & { +export declare type SwapRouterResponse = FetchSwapRouteQuotesPayload & { route: SwapRoute; price_impact: string; status: { @@ -155,3 +76,83 @@ export declare type FetchSwapRouteQuotesResponse = FetchSwapRouteQuotesPayload & round_datetime: string; }; }; +export declare type GetSwapQuoteParams = { + assetIn: Pick; + assetOut: Pick; + pools: { + info: V1PoolInfo | V2PoolInfo; + reserves: PoolReserves; + }[]; + amount: number | bigint; + type: SwapType; + network: SupportedNetwork; + /** If `true`, the function will also check the quotes that use swap route */ + isSwapRouterEnabled?: boolean; +}; +export declare type SwapQuote = { + data: DirectSwapQuoteAndPool; + type: SwapQuoteType.Direct; +} | { + data: SwapRouterResponse; + type: SwapQuoteType.Router; +}; +export declare type GetSwapQuoteBySwapTypeParams = Omit; +export interface GenerateSwapTxnsParams { + client: Algodv2; + network: SupportedNetwork; + quote: SwapQuote; + swapType: SwapType; + slippage: number; + initiatorAddr: string; +} +export declare type GenerateV1_1SwapTxnsParams = Omit & { + quote: DirectSwapQuoteAndPool; +}; +export interface GenerateSwapRouterTxnsParams { + client: Algodv2; + initiatorAddr: string; + swapType: SwapType; + route: SwapRoute; + network: SupportedNetwork; +} +/** An object containing information about a successfully executed swap. */ +export interface V1SwapExecution { + /** The round that the swap occurred in. */ + round: number; + /** + * The total amount of transaction fees that were spent (in microAlgos) to execute the swap and, + * if applicable, redeem transactions. + */ + fees: number; + /** The ID of the swap's input asset. */ + assetInID: number; + /** The amount of the swap's input asset. */ + assetInAmount: bigint; + /** The ID of the swap's output asset. */ + assetOutID: number; + /** The amount of the swap's output asset. */ + assetOutAmount: bigint; + /** The ID of the transaction. */ + txnID: string; + excessAmount: { + /** Asset ID for which the excess amount can be redeemed with */ + assetID: number; + /** Excess amount for the current swap */ + excessAmountForSwap: bigint; + /** Total excess amount accumulated for the pool asset */ + totalExcessAmount: bigint; + }; + /** The group ID for the transaction group. */ + groupID: string; +} +export interface V2SwapExecution { + /** Can be `undefined` if the execution was successful, but there was an issue while + * extracting the input asset data fron the transaction response */ + assetIn: AssetWithIdAndAmount | undefined; + /** Can be `undefined` if the execution was successful, but there was an issue while + * extracting the output asset data fron the transaction response */ + assetOut: AssetWithIdAndAmount | undefined; + quote: SwapQuote; + txnID: string; + round: number; +} diff --git a/dist/swap/utils.d.ts b/dist/swap/utils.d.ts index c9d583c1..d1a64177 100644 --- a/dist/swap/utils.d.ts +++ b/dist/swap/utils.d.ts @@ -5,7 +5,6 @@ import { InitiatorSigner, SignerTransaction, SupportedNetwork } from "../util/co import { V1PoolInfo } from "../util/pool/poolTypes"; import { GetSwapQuoteBySwapTypeParams, GenerateSwapTxnsParams, GetSwapQuoteParams, SwapQuote } from "./types"; import { SwapType } from "./constants"; -import { ContractVersionValue } from "../contract/types"; /** * Gets the best quote for swap from the pools and swap router and returns the best option. */ @@ -52,5 +51,4 @@ export declare function getSwapTotalFee(params: { version: typeof CONTRACT_VERSION.V2; type: SwapType; }): number; -export declare function getContractVersionFromSwapQuote(quote: SwapQuote): ContractVersionValue; export {}; diff --git a/dist/swap/v2/router/swap-router.d.ts b/dist/swap/v2/router/swap-router.d.ts index cdbc9ec2..abb8f8e7 100644 --- a/dist/swap/v2/router/swap-router.d.ts +++ b/dist/swap/v2/router/swap-router.d.ts @@ -2,7 +2,7 @@ import algosdk, { Transaction } from "algosdk"; import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; import { SupportedNetwork } from "../../../util/commonTypes"; import { SwapType } from "../../constants"; -import { FetchSwapRouteQuotesResponse, GenerateSwapRouterTxnsParams } from "../../types"; +import { SwapRouterResponse, GenerateSwapRouterTxnsParams } from "../../types"; export declare function generateSwapRouterAssetOptInTransaction({ client, routerAppID, assetIDs, accountAddress }: { client: AlgodClient; routerAppID: number; @@ -24,4 +24,4 @@ export declare function getSwapRoute({ amount, assetInID, assetOutID, swapType, swapType: SwapType; amount: number | bigint; network: SupportedNetwork; -}): Promise; +}): Promise; diff --git a/src/swap/common/utils.ts b/src/swap/common/utils.ts index a86a205e..b6ac14fb 100644 --- a/src/swap/common/utils.ts +++ b/src/swap/common/utils.ts @@ -1,4 +1,5 @@ import {CONTRACT_VERSION} from "../../contract/constants"; +import {ContractVersionValue} from "../../contract/types"; import { AssetWithAmountAndDecimals, AssetWithIdAndAmount @@ -42,13 +43,13 @@ function calculatePriceImpact({ function getSwapQuotePriceImpact(quote: SwapQuote) { return quote.type === SwapQuoteType.Router - ? Number(quote.price_impact) - : quote.quoteWithPool.quote.priceImpact; + ? Number(quote.data.price_impact) + : quote.data.quote.priceImpact; } function getAssetInFromSwapQuote(quote: SwapQuote): AssetWithIdAndAmount { if (quote.type === SwapQuoteType.Router) { - const assetIn = getAssetInFromSwapRoute(quote.route); + const assetIn = getAssetInFromSwapRoute(quote.data.route); return { id: getAssetId(assetIn.asset), @@ -57,14 +58,14 @@ function getAssetInFromSwapQuote(quote: SwapQuote): AssetWithIdAndAmount { } return { - id: quote.quoteWithPool.quote.assetInID, - amount: quote.quoteWithPool.quote.assetInAmount + id: quote.data.quote.assetInID, + amount: quote.data.quote.assetInAmount }; } function getAssetOutFromSwapQuote(quote: SwapQuote): AssetWithIdAndAmount { if (quote.type === SwapQuoteType.Router) { - const assetOut = getAssetOutFromSwapRoute(quote.route); + const assetOut = getAssetOutFromSwapRoute(quote.data.route); return { id: getAssetId(assetOut.asset), @@ -73,8 +74,8 @@ function getAssetOutFromSwapQuote(quote: SwapQuote): AssetWithIdAndAmount { } return { - id: quote.quoteWithPool.quote.assetOutID, - amount: quote.quoteWithPool.quote.assetOutAmount + id: quote.data.quote.assetOutID, + amount: quote.data.quote.assetOutAmount }; } @@ -88,9 +89,9 @@ function getAssetInAndAssetOutFromSwapQuote(quote: SwapQuote): { }; } -function getSwapQuoteContractVersion(quote: SwapQuote) { +function getSwapQuoteContractVersion(quote: SwapQuote): ContractVersionValue { if (quote.type === SwapQuoteType.Direct) { - return quote.quoteWithPool.pool.contractVersion; + return quote.data.pool.contractVersion; } return CONTRACT_VERSION.V2; } diff --git a/src/swap/types.ts b/src/swap/types.ts index d7cbaf13..373c2927 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -5,6 +5,11 @@ import {SupportedNetwork} from "../util/commonTypes"; import {PoolReserves, V1PoolInfo, V2PoolInfo} from "../util/pool/poolTypes"; import {SwapType} from "./constants"; +export enum SwapQuoteType { + Direct = "direct", + Router = "router" +} + export interface DirectSwapQuote { /** The ID of the input asset in this quote. */ assetInID: number; @@ -24,76 +29,11 @@ export interface DirectSwapQuote { round?: number; } -/** An object containing information about a successfully executed swap. */ -export interface V1SwapExecution { - /** The round that the swap occurred in. */ - round: number; - /** - * The total amount of transaction fees that were spent (in microAlgos) to execute the swap and, - * if applicable, redeem transactions. - */ - fees: number; - /** The ID of the swap's input asset. */ - assetInID: number; - /** The amount of the swap's input asset. */ - assetInAmount: bigint; - /** The ID of the swap's output asset. */ - assetOutID: number; - /** The amount of the swap's output asset. */ - assetOutAmount: bigint; - /** The ID of the transaction. */ - txnID: string; - excessAmount: { - /** Asset ID for which the excess amount can be redeemed with */ - assetID: number; - /** Excess amount for the current swap */ - excessAmountForSwap: bigint; - /** Total excess amount accumulated for the pool asset */ - totalExcessAmount: bigint; - }; - /** The group ID for the transaction group. */ - groupID: string; -} - -export interface V2SwapExecution { - /** Can be `undefined` if the execution was successful, but there was an issue while - * extracting the input asset data fron the transaction response */ - assetIn: AssetWithIdAndAmount | undefined; - /** Can be `undefined` if the execution was successful, but there was an issue while - * extracting the output asset data fron the transaction response */ - assetOut: AssetWithIdAndAmount | undefined; - quote: SwapQuote; - txnID: string; - round: number; -} - -export type GetSwapQuoteParams = { - assetIn: Pick; - assetOut: Pick; - pools: {info: V1PoolInfo | V2PoolInfo; reserves: PoolReserves}[]; - amount: number | bigint; - type: SwapType; - network: SupportedNetwork; - /** If `true`, the function will also check the quotes that use swap route */ - isSwapRouterEnabled?: boolean; -}; - -export interface SwapQuoteWithPool { +export interface DirectSwapQuoteAndPool { quote: DirectSwapQuote; pool: V1PoolInfo | V2PoolInfo; } -export type SwapQuote = - | { - quoteWithPool: SwapQuoteWithPool; - type: SwapQuoteType.Direct; - } - | (FetchSwapRouteQuotesResponse & { - type: SwapQuoteType.Router; - }); - -export type GetSwapQuoteBySwapTypeParams = Omit; - export interface SwapRouteAsset { id: string; name: string; @@ -113,35 +53,6 @@ export type SwapRoute = { pool: SwapRoutePool; }[]; -export enum SwapQuoteType { - Direct = "direct", - Router = "router" -} - -export interface GenerateSwapTxnsParams { - client: Algodv2; - network: SupportedNetwork; - quote: SwapQuote; - swapType: SwapType; - slippage: number; - initiatorAddr: string; -} - -export interface GenerateSwapRouterTxnsParams { - client: Algodv2; - initiatorAddr: string; - swapType: SwapType; - route: SwapRoute; - network: SupportedNetwork; -} - -export type GenerateV1_1SwapTxnsParams = Omit< - GenerateSwapTxnsParams, - "quote" | "network" -> & { - quote: SwapQuoteWithPool; -}; - export interface SwapRouterQuote { swap_type: SwapType; amount_in: { @@ -167,7 +78,7 @@ export interface FetchSwapRouteQuotesPayload { swap_type: SwapType; } -export type FetchSwapRouteQuotesResponse = FetchSwapRouteQuotesPayload & { +export type SwapRouterResponse = FetchSwapRouteQuotesPayload & { route: SwapRoute; price_impact: string; status: { @@ -175,3 +86,93 @@ export type FetchSwapRouteQuotesResponse = FetchSwapRouteQuotesPayload & { round_datetime: string; }; }; + +export type GetSwapQuoteParams = { + assetIn: Pick; + assetOut: Pick; + pools: {info: V1PoolInfo | V2PoolInfo; reserves: PoolReserves}[]; + amount: number | bigint; + type: SwapType; + network: SupportedNetwork; + /** If `true`, the function will also check the quotes that use swap route */ + isSwapRouterEnabled?: boolean; +}; + +export type SwapQuote = + | { + data: DirectSwapQuoteAndPool; + type: SwapQuoteType.Direct; + } + | { + data: SwapRouterResponse; + type: SwapQuoteType.Router; + }; + +export type GetSwapQuoteBySwapTypeParams = Omit; + +export interface GenerateSwapTxnsParams { + client: Algodv2; + network: SupportedNetwork; + quote: SwapQuote; + swapType: SwapType; + slippage: number; + initiatorAddr: string; +} + +export type GenerateV1_1SwapTxnsParams = Omit< + GenerateSwapTxnsParams, + "quote" | "network" +> & { + quote: DirectSwapQuoteAndPool; +}; + +export interface GenerateSwapRouterTxnsParams { + client: Algodv2; + initiatorAddr: string; + swapType: SwapType; + route: SwapRoute; + network: SupportedNetwork; +} + +/** An object containing information about a successfully executed swap. */ +export interface V1SwapExecution { + /** The round that the swap occurred in. */ + round: number; + /** + * The total amount of transaction fees that were spent (in microAlgos) to execute the swap and, + * if applicable, redeem transactions. + */ + fees: number; + /** The ID of the swap's input asset. */ + assetInID: number; + /** The amount of the swap's input asset. */ + assetInAmount: bigint; + /** The ID of the swap's output asset. */ + assetOutID: number; + /** The amount of the swap's output asset. */ + assetOutAmount: bigint; + /** The ID of the transaction. */ + txnID: string; + excessAmount: { + /** Asset ID for which the excess amount can be redeemed with */ + assetID: number; + /** Excess amount for the current swap */ + excessAmountForSwap: bigint; + /** Total excess amount accumulated for the pool asset */ + totalExcessAmount: bigint; + }; + /** The group ID for the transaction group. */ + groupID: string; +} + +export interface V2SwapExecution { + /** Can be `undefined` if the execution was successful, but there was an issue while + * extracting the input asset data fron the transaction response */ + assetIn: AssetWithIdAndAmount | undefined; + /** Can be `undefined` if the execution was successful, but there was an issue while + * extracting the output asset data fron the transaction response */ + assetOut: AssetWithIdAndAmount | undefined; + quote: SwapQuote; + txnID: string; + round: number; +} diff --git a/src/swap/utils.ts b/src/swap/utils.ts index 4339f15c..24b0683d 100644 --- a/src/swap/utils.ts +++ b/src/swap/utils.ts @@ -17,9 +17,9 @@ import {SwapV2} from "./v2"; import {V1_1_SWAP_TOTAL_FEE} from "./v1_1/constants"; import {getV2SwapTotalFee} from "./v2/util"; import {isPoolEmpty} from "../util/pool/common"; -import {ContractVersionValue} from "../contract/types"; import {getSwapRouteRate} from "./v2/router/util"; import OutputAmountExceedsAvailableLiquidityError from "../util/error/OutputAmountExceedsAvailableLiquidityError"; +import {getSwapQuoteContractVersion} from "./common/utils"; /** * Gets the best quote for swap from the pools and swap router and returns the best option. @@ -151,10 +151,10 @@ export async function getFixedOutputSwapQuote( */ function getSwapQuoteRate(quote: SwapQuote): number { if (quote.type === SwapQuoteType.Direct) { - return quote.quoteWithPool.quote.rate; + return quote.data.quote.rate; } - return getSwapRouteRate(quote.route); + return getSwapRouteRate(quote.data.route); } /** @@ -182,9 +182,9 @@ export function generateTxns( ): Promise { if ( params.quote.type === SwapQuoteType.Direct && - getContractVersionFromSwapQuote(params.quote) === CONTRACT_VERSION.V1_1 + getSwapQuoteContractVersion(params.quote) === CONTRACT_VERSION.V1_1 ) { - return SwapV1_1.generateTxns({...params, quote: params.quote.quoteWithPool}); + return SwapV1_1.generateTxns({...params, quote: params.quote.data}); } return SwapV2.generateTxns(params); @@ -197,10 +197,10 @@ export function signTxns(params: { }): Promise { if ( params.quote.type === SwapQuoteType.Direct && - getContractVersionFromSwapQuote(params.quote) === CONTRACT_VERSION.V1_1 + getSwapQuoteContractVersion(params.quote) === CONTRACT_VERSION.V1_1 ) { const { - quoteWithPool: {pool} + data: {pool} } = params.quote; return SwapV1_1.signTxns({pool, ...params}); @@ -261,11 +261,3 @@ export function getSwapTotalFee( throw new Error("Provided contract version was not valid."); } } - -export function getContractVersionFromSwapQuote(quote: SwapQuote): ContractVersionValue { - if (quote.type === SwapQuoteType.Direct) { - return quote.quoteWithPool.pool.contractVersion; - } - - return CONTRACT_VERSION.V2; -} diff --git a/src/swap/v1_1/index.ts b/src/swap/v1_1/index.ts index 2fee44a9..8af53a72 100644 --- a/src/swap/v1_1/index.ts +++ b/src/swap/v1_1/index.ts @@ -284,7 +284,7 @@ function getFixedInputSwapQuote({ return { type: SwapQuoteType.Direct, - quoteWithPool: { + data: { pool, quote: directSwapQuote } @@ -447,7 +447,7 @@ function getFixedOutputSwapQuote({ return { type: SwapQuoteType.Direct, - quoteWithPool: { + data: { pool, quote: directSwapQuote } diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index 488acb1e..a64affb4 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -27,7 +27,11 @@ import { V2_SWAP_APP_CALL_INNER_TXN_COUNT } from "./constants"; import {isAlgo} from "../../util/asset/assetUtils"; -import {calculatePriceImpact} from "../common/utils"; +import { + calculatePriceImpact, + getAssetInFromSwapQuote, + getAssetOutFromSwapQuote +} from "../common/utils"; import {getAppCallInnerTxns} from "../../util/transaction/transactionUtils"; import OutputAmountExceedsAvailableLiquidityError from "../../util/error/OutputAmountExceedsAvailableLiquidityError"; import {AssetWithIdAndAmount} from "../../util/asset/assetModels"; @@ -40,13 +44,13 @@ async function generateTxns( params: GenerateSwapTxnsParams ): Promise { if (params.quote.type === SwapQuoteType.Router) { - return generateSwapRouterTxns({...params, route: params.quote.route}); + return generateSwapRouterTxns({...params, route: params.quote.data.route}); } const {client, initiatorAddr, slippage, swapType, quote} = params; const { - quoteWithPool: {pool, quote: swapQuote} + data: {pool, quote: swapQuote} } = quote; const {assetInID, assetOutID} = swapQuote; @@ -161,20 +165,8 @@ async function execute({ }): Promise { let [{confirmedRound, txnID}] = await sendAndWaitRawTransaction(client, [signedTxns]); const innerTxns = await getAppCallInnerTxns(client, txGroup); - const assetIn: AssetWithIdAndAmount = - quote.type === SwapQuoteType.Direct - ? { - id: quote.quoteWithPool.quote.assetInID, - amount: quote.quoteWithPool.quote.assetInAmount - } - : { - id: Number(quote.asset_in_id), - amount: Number(quote.route[0].quote.amount_in.amount) - }; - const assetOutId = - quote.type === SwapQuoteType.Direct - ? quote.quoteWithPool.quote.assetOutID - : Number(quote.asset_out_id); + const assetIn = getAssetInFromSwapQuote(quote); + const assetOutId = getAssetOutFromSwapQuote(quote).id; /** * If the swap type if Fixed Output, usually there will be a difference between * input amount and the actual used input amount. The change will be returned to the user @@ -328,7 +320,7 @@ async function getFixedInputSwapQuote({ directSwapQuote.assetOutAmount ) { return { - ...swapRoute, + data: swapRoute, type: SwapQuoteType.Router }; } @@ -339,7 +331,7 @@ async function getFixedInputSwapQuote({ } return { - quoteWithPool: { + data: { pool, quote: directSwapQuote }, @@ -425,7 +417,7 @@ async function getFixedOutputSwapQuote({ directSwapQuote.assetInAmount ) { return { - ...swapRoute, + data: swapRoute, type: SwapQuoteType.Router }; } @@ -436,7 +428,7 @@ async function getFixedOutputSwapQuote({ } return { - quoteWithPool: { + data: { pool, quote: directSwapQuote }, diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index 67cad0a7..cfe05b5e 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -9,7 +9,7 @@ import {getValidatorAppID} from "../../../validator"; import {SwapType} from "../../constants"; import { FetchSwapRouteQuotesPayload, - FetchSwapRouteQuotesResponse, + SwapRouterResponse, GenerateSwapRouterTxnsParams } from "../../types"; import { @@ -173,7 +173,7 @@ export async function getSwapRoute({ swapType: SwapType; amount: number | bigint; network: SupportedNetwork; -}): Promise { +}): Promise { const payload: FetchSwapRouteQuotesPayload = { asset_in_id: String(assetInID), asset_out_id: String(assetOutID), From 9b234c0304fb8535d361895c745b6d8f6faba84f Mon Sep 17 00:00:00 2001 From: anlerkan Date: Mon, 20 Mar 2023 13:35:36 +0300 Subject: [PATCH 22/39] feat(swap): implement error states --- dist/index.d.ts | 3 + dist/index.js | 4 +- dist/swap/index.d.ts | 42 +- dist/swap/utils.d.ts | 7 +- dist/swap/v2/index.d.ts | 49 ++- dist/util/commonTypes.d.ts | 8 + .../error/NoAvailablePoolOrRouteError.d.ts | 4 + dist/util/error/NonExistentPoolError.d.ts | 4 + dist/util/error/SwapError.d.ts | 15 + dist/util/error/SwapQuoteError.d.ts | 19 + dist/util/util.d.ts | 3 +- src/index.ts | 5 + src/swap/utils.ts | 194 ++++++--- src/swap/v1_1/index.ts | 30 +- src/swap/v2/index.ts | 406 ++++++++++++------ src/swap/v2/router/swap-router.ts | 46 +- src/util/commonTypes.ts | 10 + ...putAmountExceedsAvailableLiquidityError.ts | 7 - src/util/error/SwapQuoteError.ts | 31 ++ src/util/util.ts | 6 +- 20 files changed, 635 insertions(+), 258 deletions(-) create mode 100644 dist/util/error/NoAvailablePoolOrRouteError.d.ts create mode 100644 dist/util/error/NonExistentPoolError.d.ts create mode 100644 dist/util/error/SwapError.d.ts create mode 100644 dist/util/error/SwapQuoteError.d.ts delete mode 100644 src/util/error/OutputAmountExceedsAvailableLiquidityError.ts create mode 100644 src/util/error/SwapQuoteError.ts diff --git a/dist/index.d.ts b/dist/index.d.ts index e1df00d3..53030492 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,3 +1,4 @@ +import SwapQuoteError from "./util/error/SwapQuoteError"; export type { InitiatorSigner, SignerTransaction, SupportedNetwork } from "./util/commonTypes"; export { BASE_MINIMUM_BALANCE, MINIMUM_BALANCE_REQUIRED_PER_ASSET, MINIMUM_BALANCE_REQUIRED_PER_APP, MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA, MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE, MINIMUM_ADD_LIQUIDITY_AMOUNT } from "./util/constant"; export * from "./swap/v2/router"; @@ -33,6 +34,8 @@ export { getSwapTotalFee } from "./swap/utils"; export { SwapType } from "./swap/constants"; export { Swap } from "./swap"; export { SwapQuoteType } from "./swap/types"; +export { SwapQuoteError }; +export { SwapQuoteErrorType } from "./util/error/SwapQuoteError"; export { redeemExcessAsset, redeemAllExcessAsset, generateRedeemTxns, REDEEM_PROCESS_TXN_COUNT } from "./redeem"; export { prepareCommitTransactions, getStakingAppID } from "./stake"; export { tinymanJSSDKConfig } from "./config"; diff --git a/dist/index.js b/dist/index.js index 479d49f9..ee0b1a37 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),a=Uint8Array.from([1]);function s(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),w(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;w(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:b(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}var S={V1_1:"v1_1",V2:"v2"},E={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},O={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},N="- would result negative",k="logic eval error:",P="exceeds schema integer count",L=/transaction \w+:/,F=function(t){m(n,h(Error));var e=y(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function M(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function C(t){return 0===Number(t)}function V(t){return Number(t.id)}function Q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},u=D(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[s?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function X(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=D(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var j=100000n,U=100000n,q=100000n,G=25000n+25000n,W=25000n+3500n;function z(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return j+U*BigInt(o.length)+q*BigInt(s.length+a.length)+W*r+G*n}function J(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,J(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function H(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function Z(t,e){var n=Number(t);return tt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function $(t,e){return tt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function tt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(nt(e),2),s=a[0],o=a[1],u=v(nt(Math.round(Number(et(s,o+r)))),2),i=u[0],c=u[1];return Number(et(i,c-r))}return Math.round(e)}function et(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function nt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function rt(t,e){return at.apply(this,arguments)}function at(){return(at=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=D(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Y(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new F(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function st(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ot(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function ut(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function it(t){return(new TextEncoder).encode(t)}var ct,pt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return it("tinyman/".concat(t===S.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),lt=(f(ct={},S.V1_1,{testnet:62368684,mainnet:552635992}),f(ct,S.V2,{testnet:148607e3,mainnet:1002541853}),ct);function dt(t,e){var n=lt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function ft(){return(ft=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var mt,gt,xt,It;function At(){return(At=c(u().mark((function t(e){var n,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:dt(a,s),note:pt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(mt=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",mt.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(It||(It={}));var ht,Tt,yt=(f(gt={},exports.SwapType.FixedInput,1),f(gt,exports.SwapType.FixedOutput,2),gt),vt=it("swap"),wt=(f(xt={},exports.SwapType.FixedInput,it("fixed-input")),f(xt,exports.SwapType.FixedOutput,it("fixed-output")),xt),_t={ASSET_OPT_IN:it("asset_opt_in")},bt={testnet:159521633,mainnet:0},Dt=(f(ht={},exports.SwapType.FixedInput,7),f(ht,exports.SwapType.FixedOutput,8),ht),St={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function Et(t){var e=Pt(t),n=e.assetIn,r=e.assetOut;return Z(r.asset.decimals,Number(r.amount))/Z(n.asset.decimals,Number(n.amount))}function Ot(t){if(!bt[t])throw new Error("Unknown network or network not supported: ".concat(t));return bt[t]}function Nt(t){return t[t.length-1].quote.amount_out}function kt(t){return t[0].quote.amount_in}function Pt(t){return{assetIn:kt(t),assetOut:Nt(t)}}function Lt(t){return Ft.apply(this,arguments)}function Ft(){return(Ft=c(u().mark((function e(n){var a,s,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,a.getTransactionParams().do();case 3:return c=e.sent,p=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[_t.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Rt(t){return Bt.apply(this,arguments)}function Bt(){return(Bt=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_,b,D,E,O;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[V(c[0].quote.amount_in.asset),V(c[0].quote.amount_out.asset),V(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(kt(c).amount),Number(Nt(c).amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=C(d),v=Ot(o),w=y?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(_=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:v,appArgs:[vt,wt[i],r.default.encodeUint64(I)],foreignApps:[dt(o,S.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Dt[i]+1),b=[w,_],e.next=15,Mt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((D=e.sent).length>0)){e.next=21;break}return e.next=19,Lt({client:s,accountAddress:a,assetIDs:D,routerAppID:v});case 19:E=e.sent,b.unshift(E);case 21:return O=r.default.assignGroupID(b),e.abrupt("return",O.map((function(t){return{txn:t,signers:[a]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.prev=2,t.next=5,fetch("".concat(St[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)});case 5:return c=t.sent,t.abrupt("return",c.json());case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,null,[[2,9]])})))).apply(this,arguments)}function Xt(t){var e=t.assetIn,n=t.assetOut;return Z(n.decimals,Number(n.amount))/Z(e.decimals,Number(e.amount))}function jt(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=Xt({assetIn:r,assetOut:a}),o=Z(a.decimals,Number(n))/Z(r.decimals,Number(e));return tt({decimalPlaces:5},Math.abs(s/o-1))}function Ut(t){if(t.type===exports.SwapQuoteType.Router){var e=kt(t.data.route);return{id:V(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function qt(t){if(t.type===exports.SwapQuoteType.Router){var e=Nt(t.data.route);return{id:V(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function Gt(t){return t.type===exports.SwapQuoteType.Direct?t.data.pool.contractVersion:S.V2}exports.SwapQuoteType=void 0,(Tt=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",Tt.Router="router";var Wt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},zt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Jt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),Yt=new(function(n){m(a,Jt);var r=y(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,S.V1_1),a=v(M(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:ne(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function ee(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ne(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var re,ae,se=it("e");function oe(t){return ue.apply(this,arguments)}function ue(){return(ue=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_,b,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.pool,i=a.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=D(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=Q({stateArray:A}),T=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(X([r.default.decodeAddress(m).publicKey,se,r.default.encodeUint64(o.poolTokenID)])),w=h[T],_=h[y],b=h[v],"bigint"==typeof w&&(l=w),"bigint"==typeof _&&(d=_),"bigint"==typeof b&&(f=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function ie(){return(ie=c(u().mark((function n(a){var s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.client,o=a.accountAddr,i=a.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=Q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mle)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(le,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function ge(){return ge=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,te(n,r);case 6:return o=t.sent,i=ee(o,dt(a,S.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[ce[S.V1_1].asset1],asset2ID:i[ce[S.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),ge.apply(this,arguments)}var xe=Object.freeze({__proto__:null,getPoolInfo:de,getPoolReserves:function(t,e){return me.apply(this,arguments)},getPoolAssets:function(t){return ge.apply(this,arguments)}});function Ie(t){return Ae.apply(this,arguments)}function Ae(){return(Ae=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=$t(S.V2),i=o.generateLogicSigAccountForPool(e),c=dt(r,S.V2),p=i.address(),l=M(a,s),t.next=8,te(n,p);case 8:return d=t.sent,f=ee(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:S.V2},f&&(m.asset1ProtocolFees=BigInt(f[ce.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ce.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ce.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ce.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ce.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ce.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ce.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ce.v2.totalFeeShare]),m.poolTokenID=Number(f[ce.v2.poolTokenID]),m.asset1ID=Number(f[ce.v2.asset1]),m.asset2ID=Number(f[ce.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function he(){return(he=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,te(e,n.account.address());case 2:return r=t.sent,a=ee(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[ce.v2.asset1Reserves]),s.asset2=BigInt(a[ce.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[ce.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(){return(Te=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,te(n,r);case 3:return s=t.sent,o=ee(s,dt(a,S.V2)),i=null,o&&(i={asset1ID:o[ce[S.V2].asset1],asset2ID:o[ce[S.V2].asset2],poolTokenID:o[ce[S.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ye=Object.freeze({__proto__:null,getPoolInfo:Ie,getPoolReserves:function(t,e){return he.apply(this,arguments)},getPoolAssets:function(t){return Te.apply(this,arguments)}});function ve(t){return Boolean(t&&!(t.asset1+t.asset2))}var we,_e,be=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=ve(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=Z(t.asset1,e.asset1)/Z(t.asset2,e.asset2)),r},isPoolEmpty:ve,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([de(t),Ie(t)])}}),De=o((f(we={},S.V1_1,o(o({},xe),be)),f(we,S.V2,o(o({},ye),be)),we),be);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(_e||(_e={}));var Se=96e4,Ee=859e3;function Oe(){return(Oe=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,b,D,E,N,k;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=B(s,o),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=C(x),h=dt(a,S.V1_1),T=Yt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:x}),y=T.address(),w=r.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:pt.getAppCallTxnNoteWithClientName(S.V1_1),appArgs:[it("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),_=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:O.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),D=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Ne(A),suggestedParams:c}),(E=[])[_e.FUNDING_TXN]=D,E[_e.VALIDATOR_APP_CALL]=w,E[_e.POOL_TOKEN_CREATE]=_,E[_e.ASSET1_OPT_IN]=b,A||(E[_e.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),N=r.default.assignGroupID(E),k=[{txn:N[_e.FUNDING_TXN],signers:[i]},{txn:N[_e.VALIDATOR_APP_CALL],signers:[y]},{txn:N[_e.POOL_TOKEN_CREATE],signers:[y]},{txn:N[_e.ASSET1_OPT_IN],signers:[y]}],N[_e.ASSET2_OPT_IN]&&k.push({txn:N[_e.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",k);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ne(t){return t?Se:Ee}function ke(){return(ke=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Yt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===_e.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pe(t){return Le.apply(this,arguments)}function Le(){return(Le=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Y(n,a[_e.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Fe(){return(Fe=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Pe({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",De.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Re,Be={generateTxns:function(t){return Oe.apply(this,arguments)},signTxns:function(t){return ke.apply(this,arguments)},execute:function(t){return Fe.apply(this,arguments)},getBootstrapFundingTxnAmount:Ne};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Re||(Re={}));var Me=5,Ce=6;function Ve(t,e){return Qe.apply(this,arguments)}function Qe(){return(Qe=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xe(t,e){return je.apply(this,arguments)}function je(){return(je=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Ve(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ue(){return(Ue=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,b;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,a.getTransactionParams().do();case 3:return p=e.sent,l=dt(s,S.V2),d=t.getApplicationAddress(l),f=B(o,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,De.v2.getPoolInfo({client:a,network:s,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=Zt.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:x}),A=I.address(),h=C(x),(T=r.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[it("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(S.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=Ge(h),y=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:qe(h),suggestedParams:p}),(w=[])[Re.FUNDING_TXN]=y,w[Re.VALIDATOR_APP_CALL]=T,_=r.default.assignGroupID(w),(b=[])[Re.FUNDING_TXN]={txn:_[Re.FUNDING_TXN],signers:[c]},b[Re.VALIDATOR_APP_CALL]={txn:_[Re.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",b);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qe(t){return function(t,e){var n=$t(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(S.V2,t)+Ge(t)+1e5}function Ge(e){return((e?Me:Ce)+1)*t.ALGORAND_MIN_TX_FEE}function We(){return(We=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=M(o,i),f=v(d,2),m=f[0],g=f[1],x=Zt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Re.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,x),a=n.txID,s=n.blob;return I.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ze(){return(ze=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,Ve(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ce.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",De.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new F(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Je,Ye={generateTxns:function(t){return Ue.apply(this,arguments)},signTxns:function(t){return We.apply(this,arguments)},execute:function(t){return ze.apply(this,arguments)},getBootstrapFundingTxnAmount:qe,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+qe(e)}};var Ke,He=(f(Je={},S.V1_1,Be),f(Je,S.V2,Ye),f(Je,"generateTxns",(function(t){return t.contractVersion===S.V1_1?Be.generateTxns(t):Ye.generateTxns(t)})),f(Je,"signTxns",(function(t){return t.contractVersion===S.V1_1?Be.signTxns(t):Ye.signTxns(t)})),f(Je,"execute",(function(t){return t.contractVersion===S.V1_1?Be.execute(t):Ye.execute(t)})),f(Je,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===S.V1_1?Be.getBootstrapFundingTxnAmount(n):Ye.getBootstrapFundingTxnAmount(n)})),Je);exports.V1_1AddLiquidityTxnIndices=void 0,(Ke=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[Ke.FEE_TXN=0]="FEE_TXN",Ke[Ke.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",Ke[Ke.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",Ke[Ke.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",Ke[Ke.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Ze,$e,tn,en,nn=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(en=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",en.FLEXIBLE="flexible",en.INITIAL="initial";var rn,an=(f(Ze={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Ze,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Ze,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Ze),sn=(f($e={},exports.V2AddLiquidityType.INITIAL,1),f($e,exports.V2AddLiquidityType.SINGLE,2),f($e,exports.V2AddLiquidityType.FLEXIBLE,2),$e),on=(f(tn={},exports.V2AddLiquidityType.INITIAL,3),f(tn,exports.V2AddLiquidityType.FLEXIBLE,3),f(tn,exports.V2AddLiquidityType.SINGLE,2),tn);function un(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(o.amount)-m,I=BigInt(u.amount)-g;if(x>I){var A=x;r=cn(A,s),e={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=cn(h,s),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:jt({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function cn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function pn(e){return(sn[e]+1)*t.ALGORAND_MIN_TX_FEE}var ln=it("add_liquidity"),dn=(f(rn={},S.V1_1,[it("mint")]),f(rn,S.V2,{INITIAL_LIQUIDITY:[it("add_initial_liquidity")],SINGLE_ASSET_MODE:[ln,it("single")],FLEXIBLE_MODE:[ln,it("flexible")]}),rn);function fn(){return(fn=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,o=e.poolAddress,i=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=H("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(s,S.V1_1),appArgs:dn.v1_1,accounts:[d],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==c.id?[i.id,p.id]:[i.id,c.id,p.id],suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:m}),I=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),A=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+A.fee,note:a,suggestedParams:m}),T=r.default.assignGroupID([h,g,x,I,A]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(){return(mn=c(u().mark((function t(e){var n,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([a]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function gn(){return(gn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,oe({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,rt(n,[s]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=st(a),x=ot(a),t.next=17,oe({client:n,pool:r,accountAddr:o});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new F(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var xn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function Cn(){return(Cn=c(u().mark((function t(e){var n,a,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Rt(o(o({},e),{},{route:e.quote.data.route})));case 2:if(n=e.client,a=e.initiatorAddr,s=e.slippage,i=e.swapType,c=e.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(x=[l.asset1ID,l.asset2ID]).includes(f)&&x.includes(m)&&f!==m){t.next=9;break}throw new F({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return I=t.sent,A=C(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:H("positive",s,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:H("negative",s,d.assetOutAmount),y=A?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,suggestedParams:I}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:g,amount:h,assetIndex:f,suggestedParams:I}),(v=r.default.makeApplicationNoOpTxnFromObject({from:a,appIndex:l.validatorAppID,appArgs:[vt,wt[i],r.default.encodeUint64(T)],note:pt.getAppCallTxnNoteWithClientName(S.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:I})).fee=Vn(i),(w=[])[It.INPUT_TXN]=y,w[It.APP_CALL_TXN]=v,_=r.default.assignGroupID(w),t.abrupt("return",[{txn:_[It.INPUT_TXN],signers:[a]},{txn:_[It.APP_CALL_TXN],signers:[a]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Vn(e){return(yt[e]+1)*t.ALGORAND_MIN_TX_FEE}function Qn(){return(Qn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,rt(a,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Xe(a,o);case 10:return m=t.sent,g=Ut(s),x=qt(s).id,I=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===x})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:I&&{amount:BigInt(g.amount)-BigInt(I.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:x},quote:s,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xn(){return(Xn=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.pool,a=e.asset,s=e.decimals,o=e.network,i=e.isSwapRouterEnabled,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,jn({pool:r,assetIn:a,decimals:s,isSwapRouterEnabled:i,network:o});case 4:c=t.sent,t.next=10;break;case 7:return t.next=9,qn({pool:r,assetOut:a,decimals:s,isSwapRouterEnabled:i,network:o});case 9:c=t.sent;case 10:return t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function jn(t){return Un.apply(this,arguments)}function Un(){return(Un=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetIn,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,o=e.network,a.status===exports.PoolStatus.READY||s){t.next=3;break}throw new F({pool:a,assetIn:n},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(n.amount),c=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}p=a.asset2ID,l=a.asset1Reserves,d=a.asset2Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}p=a.asset1ID,l=a.asset2Reserves,d=a.asset1Reserves,t.next=18;break;case 17:throw new F({pool:a,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(f=Wn({inputSupply:l,outputSupply:d,swapInputAmount:i,totalFeeShare:c,decimals:r}),m=f.swapOutputAmount,g=f.totalFeeAmount,x=f.priceImpact,I={assetInID:n.id,assetInAmount:i,assetOutID:p,assetOutAmount:m,swapFee:Number(g),rate:Z(r.assetOut,Number(m))/Z(r.assetIn,Number(i)),priceImpact:x},!s){t.next=26;break}return t.next=23,Vt({amount:n.amount,assetInID:Number(n.id),assetOutID:p,swapType:exports.SwapType.FixedInput,network:o});case 23:if(!((A=t.sent).route.length>1&&BigInt(Nt(A.route).amount)>I.assetOutAmount)){t.next=26;break}return t.abrupt("return",{data:A,type:exports.SwapQuoteType.Router});case 26:if(!(m>d)){t.next=28;break}throw new Mn;case 28:return t.abrupt("return",{data:{pool:a,quote:I},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function qn(t){return Gn.apply(this,arguments)}function Gn(){return(Gn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.assetOut,r=e.decimals,a=e.pool,s=e.isSwapRouterEnabled,o=e.network,a.status===exports.PoolStatus.READY||s){t.next=3;break}throw new F({pool:a,assetOut:n},"Trying to swap on a non-existent pool");case 3:if(i=BigInt(n.amount),c=a.totalFeeShare,n.id!==a.asset1ID){t.next=11;break}p=a.asset2ID,l=a.asset2Reserves,d=a.asset1Reserves,t.next=18;break;case 11:if(n.id!==a.asset2ID){t.next=17;break}p=a.asset1ID,l=a.asset1Reserves,d=a.asset2Reserves,t.next=18;break;case 17:throw new F({pool:a,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(a.account.address(),"."));case 18:if(f=zn({inputSupply:l,outputSupply:d,swapOutputAmount:i,totalFeeShare:c,decimals:r}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact,I={assetInID:p,assetInAmount:m,assetOutID:n.id,assetOutAmount:i,swapFee:Number(g),rate:Z(r.assetOut,Number(i))/Z(r.assetIn,Number(m)),priceImpact:x},!s){t.next=26;break}return t.next=23,Vt({amount:n.amount,assetInID:p,assetOutID:n.id,swapType:exports.SwapType.FixedOutput,network:o});case 23:if(!((A=t.sent).route.length>1&&BigInt(Nt(A.route).amount)d)){t.next=28;break}throw new Mn;case 28:return t.abrupt("return",{data:{pool:a,quote:I},type:exports.SwapQuoteType.Direct});case 29:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Wn(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:u,totalFeeAmount:o,priceImpact:jt({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:u,decimals:s.assetOut}})}}function zn(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:o,totalFeeShare:a}),i=o+u;return{swapInputAmount:i,totalFeeAmount:u,priceImpact:jt({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}})}}var Jn={getQuote:function(t){return Xn.apply(this,arguments)},getFixedInputSwapQuote:jn,getFixedOutputSwapQuote:qn,generateTxns:function(t){return Cn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Qn.apply(this,arguments)},calculateFixedInputSwap:Wn};function Yn(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Kn(){return(Kn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(x=r.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Pn,r.default.encodeUint64(H("negative",l,c)),r.default.encodeUint64(H("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[Nn.ASSET_TRANSFER_TXN]=g,I[Nn.APP_CALL_TXN]=x,A=r.default.assignGroupID(I),e.abrupt("return",[{txn:A[Nn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[Nn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function e(n){var a,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,a.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),x=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=H("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(S.V2),appArgs:[Pn,r.default.encodeUint64(I),r.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[Nn.ASSET_TRANSFER_TXN]=T,v[Nn.APP_CALL_TXN]=y,w=r.default.assignGroupID(v),e.abrupt("return",[{txn:w[Nn.ASSET_TRANSFER_TXN],signers:[o]},{txn:w[Nn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,rt(r,[s]);case 3:return o=t.sent,i=v(o,1),c=i[0].txnID,t.next=8,Xe(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var $n,tr,er={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Yn(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=Yn(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Jn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=Jn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return Kn.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Hn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Zn.apply(this,arguments)}},nr=(f($n={},S.V1_1,Bn),f($n,S.V2,er),$n),rr=3n,ar=1000n;function sr(){return(sr=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,a=e.txGroup,s=e.initiatorSigner,t.next=3,s([a]);case 3:return o=t.sent,i=v(o,2),c=i[0],p=i[1],l=a.map((function(t,e){return e===tr.FEE_TXN_INDEX?c:e===tr.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g,x,I,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.quote,o=e.swapType,i=e.slippage,c=e.initiatorAddr,p=s.pool,l=s.quote,d=l.assetInID,f=l.assetOutID,m=p.account.address(),(g=[p.asset1ID,p.asset2ID]).includes(d)&&g.includes(f)&&d!==f){t.next=7;break}throw new F({pool:p,quote:s},"Input asset (#".concat(d,") and output asset (#").concat(f,") provided to generate transactions do not belong to the pool ").concat(m,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return x=t.sent,I=[it("swap"),o===exports.SwapType.FixedInput?it("fi"):it("fo")],A=r.default.makeApplicationNoOpTxnFromObject({from:m,appIndex:p.validatorAppID,appArgs:I,accounts:[c],note:pt.getAppCallTxnNoteWithClientName(S.V1_1),foreignAssets:0==p.asset2ID?[p.asset1ID,p.poolTokenID]:[p.asset1ID,p.asset2ID,p.poolTokenID],suggestedParams:x}),h=o===exports.SwapType.FixedOutput?H("positive",i,l.assetInAmount):l.assetInAmount,T=0===d?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:h,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:m,assetIndex:d,amount:h,suggestedParams:x}),y=o===exports.SwapType.FixedInput?H("negative",i,l.assetOutAmount):l.assetOutAmount,v=0===f?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:m,to:c,amount:y,suggestedParams:x}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:m,to:c,assetIndex:f,amount:y,suggestedParams:x}),w=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:m,amount:A.fee+v.fee,note:a,suggestedParams:x}),_=r.default.assignGroupID([w,A,T,v]),t.abrupt("return",[{txn:_[0],signers:[c]},{txn:_[1],signers:[m]},{txn:_[2],signers:[c]},{txn:_[3],signers:[m]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ur(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var s,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*rr/ar,l=i-u*i/(u+(c-p));if(l>i)throw new Mn;var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:Xt(d),priceImpact:jt(o({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function ir(t){return cr.apply(this,arguments)}function cr(){return(cr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,oe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,oe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new F({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new F({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new Mn;var c=o*u/(u-i)-o,p=c*ar/(ar-rr),l=p-c,d=Z(a.assetOut,Number(i))/Z(a.assetIn,Number(p)),f=Z(a.assetOut,Number(u))/Z(a.assetIn,Number(o)),m=tt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function lr(t){return dr.apply(this,arguments)}function dr(){return(dr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,oe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,rt(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,oe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fr(){return(fr=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new F({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[tr.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[tr.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[tr.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[tr.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,ir({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,lr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ot(s),fees:st(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new F(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(tr||(tr={}));var mr,gr={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?ur({pool:e,reserves:n,assetIn:r,decimals:a}):pr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:ur,getFixedOutputSwapQuote:pr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){return sr.apply(this,arguments)},execute:function(t){return fr.apply(this,arguments)},executeFixedOutputSwap:lr},xr=4*t.ALGORAND_MIN_TX_FEE;function Ir(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof Mn})))throw new Mn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function Ar(){return Ar=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var s=c(u().mark((function s(i,c){var p,l;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetIn:{amount:n,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p=gr.getFixedInputSwapQuote(l),s.next=9;break;case 6:return s.next=8,Jn.getFixedInputSwapQuote(l);case 8:p=s.sent;case 9:i(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,Ir(i);case 4:return p=t.sent,t.abrupt("return",yr(p));case 6:case"end":return t.stop()}}),t)}))),Ar.apply(this,arguments)}function hr(){return hr=c(u().mark((function t(e){var n,r,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,s=e.pools,o=e.isSwapRouterEnabled,i=s.map((function(t){return new Promise(function(){var s=c(u().mark((function s(i,c){var p,l;return u().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:if(l={pool:t.info,assetOut:{amount:n,id:Number(a.id)},decimals:{assetIn:r.decimals,assetOut:a.decimals},reserves:t.reserves,isSwapRouterEnabled:o,network:e.network},s.prev=1,t.info.contractVersion!==S.V1_1){s.next=6;break}p=gr.getFixedOutputSwapQuote(l),s.next=9;break;case 6:return s.next=8,Jn.getFixedOutputSwapQuote(l);case 8:p=s.sent;case 9:i(p),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(1),c(s.t0);case 15:case"end":return s.stop()}}),s,null,[[1,12]])})));return function(t,e){return s.apply(this,arguments)}}())})),t.next=4,Ir(i);case 4:return p=t.sent,t.abrupt("return",yr(p));case 6:case"end":return t.stop()}}),t)}))),hr.apply(this,arguments)}function Tr(t){return t.type===exports.SwapQuoteType.Direct?t.data.quote.rate:Et(t.data.route)}function yr(t){for(var e=t[0],n=Tr(e),r=1;rn&&(e=a,n=s)}return e}var vr=(f(mr={},S.V1_1,gr),f(mr,S.V2,Jn),f(mr,"getQuote",(function(t){var e=t.type,n=t.isSwapRouterEnabled,r=t.pools;if(!n&&r.every((function(t){return ve(t.reserves)})))throw new Error("No pools available for swap");return e===exports.SwapType.FixedInput?function(t){return Ar.apply(this,arguments)}(t):e===exports.SwapType.FixedOutput?function(t){return hr.apply(this,arguments)}(t):Promise.reject(new Error("Invalid swap type"))})),f(mr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&Gt(t.quote)===S.V1_1?gr.generateTxns(o(o({},t),{},{quote:t.quote.data})):Jn.generateTxns(t)})),f(mr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&Gt(t.quote)===S.V1_1){var e=t.quote.data.pool;return gr.signTxns(o({pool:e},t))}return Jn.signTxns(t)})),f(mr,"execute",(function(t){return t.contractVersion===S.V1_1?gr.execute(t):Jn.execute(t)})),mr);function wr(){return(wr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,_r({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,rt(n,[o]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:st(a),confirmedRound:d,txnID:l,groupID:ot(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function _r(t){return br.apply(this,arguments)}function br(){return(br=c(u().mark((function t(e){var n,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,a=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=v(o,1),c=i[0],p=a.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Dr(){return Dr=c(u().mark((function t(e){var n,a,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.data,s=e.initiatorSigner,t.prev=1,o=a.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:st(e),groupID:ot(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var a=c(u().mark((function a(s,o){var c,p,l,d,f,m;return u().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,c=t.txns.map((function(n,a){return 0===a?i[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),a.next=4,rt(n,[c]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),o(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(t,e){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new F(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Dr.apply(this,arguments)}function Sr(){return(Sr=c(u().mark((function t(e){var n,s,o,i,c,p,l,d,f,m,g;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.pool,o=e.assetID,i=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=s.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:s.validatorAppID,appArgs:[it("redeem")],note:pt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[c],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(i),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(i),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:a,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Er(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[it("commit"),i],note:X([it("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function Or(){return(Or=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Er({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[it("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=E,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=On,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=He,exports.CONTRACT_VERSION=S,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=O,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=nr,exports.Swap=vr,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=kn,exports.V2AddLiquidityTxnIndices=an,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=H,exports.calculateAccountMinimumRequiredBalance=ne,exports.calculatePriceImpact=jt,exports.calculateSwapRate=Xt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=De,exports.prepareCommitTransactions=function(t){return Or.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Dr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return wr.apply(this,arguments)},exports.sendAndWaitRawTransaction=rt,exports.sumUpTxnFees=st,exports.tinymanContract_v1_1=Yt,exports.tinymanContract_v2=Zt,exports.tinymanJSSDKConfig=pt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",s=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,s=Object.create(a.prototype),o=new _(r||[]);return s._invoke=function(t,e,n){var r="suspendedStart";return function(a,s){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw s;return S()}for(n.method=a,n.arg=s;;){var o=n.delegate;if(o){var u=y(o,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,o),s}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var I=Object.getPrototypeOf,x=I&&I(I(E([])));x&&x!==e&&n.call(x,a)&&(g=x);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,s,o,u){var i=p(t[a],t,s);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,u)}),(function(t){r("throw",t,o,u)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function s(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(s,s):s()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function w(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function _(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function E(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,s=function e(){for(;++r=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),w(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;w(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:E(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}exports.SwapQuoteErrorType=void 0,(r=exports.SwapQuoteErrorType||(exports.SwapQuoteErrorType={})).SwapRouterStaleDataError="SwapRouterStaleDataError",r.SwapRouterNoRouteError="SwapRouterNoRouteError",r.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",r.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",r.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",r.NoAvailablePoolError="NoAvailablePoolError",r.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",r.UnknownError="UnknownError",r.LowSwapAmountError="LowSwapAmountError",r.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",r.InvalidSwapTypeError="InvalidSwapTypeError";var D=function(t){m(n,h(Error));var e=v(n);function n(t,r){var a;return p(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(T(a),n),a}return d(n)}(),O=Uint8Array.from([1]),N={V1_1:"v1_1",V2:"v2"},P={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},k={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},L="- would result negative",R="logic eval error:",F="exceeds schema integer count",B=/transaction \w+:/,M=function(t){m(n,h(Error));var e=v(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function C(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function j(t){return 0===Number(t)}function X(t){return Number(t.id)}function q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},u=b(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[s?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function U(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=b(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var G=100000n,W=100000n,z=100000n,J=25000n+25000n,Y=25000n+3500n;function K(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return G+W*BigInt(o.length)+z*BigInt(s.length+a.length)+Y*r+J*n}function H(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,H(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function tt(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function et(t,e){var n=Number(t);return rt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function nt(t,e){return rt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function rt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=w(st(e),2),s=a[0],o=a[1],u=w(st(Math.round(Number(at(s,o+r)))),2),i=u[0],c=u[1];return Number(at(i,c-r))}return Math.round(e)}function at(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function st(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function ot(t,e){return ut.apply(this,arguments)}function ut(){return(ut=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=b(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Z(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new M(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function it(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ct(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function pt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function lt(t){return(new TextEncoder).encode(t)}function dt(t){return Boolean(t)&&void 0!==t.fallback_message}var ft,mt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return lt("tinyman/".concat(t===N.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),gt=(f(ft={},N.V1_1,{testnet:62368684,mainnet:552635992}),f(ft,N.V2,{testnet:148607e3,mainnet:1002541853}),ft);function It(t,e){var n=gt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function xt(){return(xt=c(u().mark((function t(e){var n,r,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationOptInTxnFromObject({from:o,appIndex:It(r,s),note:mt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var At,ht,Tt,yt;function vt(){return(vt=c(u().mark((function t(e){var n,r,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:It(r,s),note:mt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(At=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",At.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(yt||(yt={}));var wt,_t,Et=(f(ht={},exports.SwapType.FixedInput,1),f(ht,exports.SwapType.FixedOutput,2),ht),St=lt("swap"),bt=(f(Tt={},exports.SwapType.FixedInput,lt("fixed-input")),f(Tt,exports.SwapType.FixedOutput,lt("fixed-output")),Tt),Dt={ASSET_OPT_IN:lt("asset_opt_in")},Ot={testnet:159521633,mainnet:0},Nt=(f(wt={},exports.SwapType.FixedInput,7),f(wt,exports.SwapType.FixedOutput,8),wt),Pt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function kt(t){var e=Bt(t),n=e.assetIn,r=e.assetOut;return et(r.asset.decimals,Number(r.amount))/et(n.asset.decimals,Number(n.amount))}function Lt(t){if(!Ot[t])throw new Error("Unknown network or network not supported: ".concat(t));return Ot[t]}function Rt(t){return t[t.length-1].quote.amount_out}function Ft(t){return t[0].quote.amount_in}function Bt(t){return{assetIn:Ft(t),assetOut:Rt(t)}}function Mt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function e(n){var r,s,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Dt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,s,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w,_,E,S,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[X(c[0].quote.amount_in.asset),X(c[0].quote.amount_out.asset),X(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Ft(c).amount),Number(Rt(c).amount)],I=g[0],x=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=j(d),v=Lt(o),w=y?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(v),amount:I,suggestedParams:p}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(v),amount:I,assetIndex:d,suggestedParams:p}),(_=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:v,appArgs:[St,bt[i],a.default.encodeUint64(x)],foreignApps:[It(o,N.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Nt[i]+1),E=[w,_],e.next=15,jt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((S=e.sent).length>0)){e.next=21;break}return e.next=19,Mt({client:s,accountAddress:r,assetIDs:S,routerAppID:v});case 19:b=e.sent,E.unshift(b);case 21:return D=a.default.assignGroupID(E),e.abrupt("return",D.map((function(t){return{txn:t,signers:[r]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qt(t){return Ut.apply(this,arguments)}function Ut(){return(Ut=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.next=4,fetch("".concat(Pt[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(!dt(p)){t.next=13;break}throw new D(p.type,p.fallback_message);case 13:throw new D(exports.SwapQuoteErrorType.UnknownError,"Unknown error");case 14:return t.abrupt("return",p);case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Gt(t){var e=t.assetIn,n=t.assetOut;return et(n.decimals,Number(n.amount))/et(e.decimals,Number(e.amount))}function Wt(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=Gt({assetIn:r,assetOut:a}),o=et(a.decimals,Number(n))/et(r.decimals,Number(e));return rt({decimalPlaces:5},Math.abs(s/o-1))}function zt(t){if(t.type===exports.SwapQuoteType.Router){var e=Ft(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function Jt(t){if(t.type===exports.SwapQuoteType.Router){var e=Rt(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function Yt(t){return t.type===exports.SwapQuoteType.Direct?t.data.pool.contractVersion:N.V2}exports.SwapQuoteType=void 0,(_t=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",_t.Router="router";var Kt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Ht={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Zt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),$t=new(function(n){m(a,Zt);var r=v(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=It(n.network,N.V1_1),a=w(C(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:oe(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function se(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function oe(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ue,ie,ce=lt("e");function pe(t){return le.apply(this,arguments)}function le(){return(le=c(u().mark((function n(r){var s,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w,_,E,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=b(p),n.prev=10,g.s();case 12:if((I=g.n()).done){n.next=31;break}if((x=I.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=x["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=q({stateArray:A}),T=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(o.poolTokenID)])),w=h[T],_=h[y],E=h[v],"bigint"==typeof w&&(l=w),"bigint"==typeof _&&(d=_),"bigint"==typeof E&&(f=E);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function de(){return(de=c(u().mark((function n(r){var s,o,i,c,p,l,d,f,m,g,I,x,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mge)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ge,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function he(){return he=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,ae(n,r);case 6:return o=t.sent,i=se(o,It(a,N.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[fe[N.V1_1].asset1],asset2ID:i[fe[N.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),he.apply(this,arguments)}var Te=Object.freeze({__proto__:null,getPoolInfo:Ie,getPoolReserves:function(t,e){return Ae.apply(this,arguments)},getPoolAssets:function(t){return he.apply(this,arguments)}});function ye(t){return ve.apply(this,arguments)}function ve(){return(ve=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=re(N.V2),i=o.generateLogicSigAccountForPool(e),c=It(r,N.V2),p=i.address(),l=C(a,s),t.next=8,ae(n,p);case 8:return d=t.sent,f=se(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:N.V2},f&&(m.asset1ProtocolFees=BigInt(f[fe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[fe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[fe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[fe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[fe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[fe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[fe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[fe.v2.totalFeeShare]),m.poolTokenID=Number(f[fe.v2.poolTokenID]),m.asset1ID=Number(f[fe.v2.asset1]),m.asset2ID=Number(f[fe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function we(){return(we=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ae(e,n.account.address());case 2:return r=t.sent,a=se(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[fe.v2.asset1Reserves]),s.asset2=BigInt(a[fe.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[fe.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(){return(_e=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,ae(n,r);case 3:return s=t.sent,o=se(s,It(a,N.V2)),i=null,o&&(i={asset1ID:o[fe[N.V2].asset1],asset2ID:o[fe[N.V2].asset2],poolTokenID:o[fe[N.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ee=Object.freeze({__proto__:null,getPoolInfo:ye,getPoolReserves:function(t,e){return we.apply(this,arguments)},getPoolAssets:function(t){return _e.apply(this,arguments)}});function Se(t){return Boolean(t&&!(t.asset1+t.asset2))}var be,De,Oe=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Se(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=et(t.asset1,e.asset1)/et(t.asset2,e.asset2)),r},isPoolEmpty:Se,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Ie(t),ye(t)])}}),Ne=o((f(be={},N.V1_1,o(o({},Te),Oe)),f(be,N.V2,o(o({},Ee),Oe)),be),Oe);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(De||(De={}));var Pe=96e4,ke=859e3;function Le(){return(Le=c(u().mark((function t(e){var n,r,s,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,_,E,S,b,D,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=V(s,o),l=w(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],I=g.id,x=g.unit_name,A=j(I),h=It(r,N.V1_1),T=$t.generateLogicSigAccountForPool({network:r,asset1ID:f,asset2ID:I}),y=T.address(),v=a.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:mt.getAppCallTxnNoteWithClientName(N.V1_1),appArgs:[lt("bootstrap"),a.default.encodeUint64(f),a.default.encodeUint64(I)],foreignAssets:A?[f]:[I],suggestedParams:c}),_=a.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:k.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),E=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),S=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Re(A),suggestedParams:c}),(b=[])[De.FUNDING_TXN]=S,b[De.VALIDATOR_APP_CALL]=v,b[De.POOL_TOKEN_CREATE]=_,b[De.ASSET1_OPT_IN]=E,A||(b[De.ASSET2_OPT_IN]=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:I,amount:0,suggestedParams:c})),D=a.default.assignGroupID(b),O=[{txn:D[De.FUNDING_TXN],signers:[i]},{txn:D[De.VALIDATOR_APP_CALL],signers:[y]},{txn:D[De.POOL_TOKEN_CREATE],signers:[y]},{txn:D[De.ASSET1_OPT_IN],signers:[y]}],D[De.ASSET2_OPT_IN]&&O.push({txn:D[De.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Re(t){return t?Pe:ke}function Fe(){return(Fe=c(u().mark((function t(e){var n,r,s,o,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=C(o,i),f=w(d,2),m=f[0],g=f[1],I=$t.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===De.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,s=n.blob;return x.push(r),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(t){return Me.apply(this,arguments)}function Me(){return(Me=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Z(n,a[De.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Qe(){return(Qe=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Be({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",Ne.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ve,Ce={generateTxns:function(t){return Le.apply(this,arguments)},signTxns:function(t){return Fe.apply(this,arguments)},execute:function(t){return Qe.apply(this,arguments)},getBootstrapFundingTxnAmount:Re};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ve||(Ve={}));var je=5,Xe=6;function qe(t,e){return Ue.apply(this,arguments)}function Ue(){return(Ue=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ge(t,e){return We.apply(this,arguments)}function We(){return(We=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qe(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ze(){return(ze=c(u().mark((function e(n){var r,s,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,_,E;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return p=e.sent,l=It(s,N.V2),d=t.getApplicationAddress(l),f=V(o,i),m=w(f,2),g=m[0].id,I=m[1].id,e.next=9,Ne.v2.getPoolInfo({client:r,network:s,asset1ID:g,asset2ID:I});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return x=ne.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:I}),A=x.address(),h=j(I),(T=a.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[lt("bootstrap")],note:mt.getAppCallTxnNoteWithClientName(N.V2),foreignAssets:[g,I],rekeyTo:d,suggestedParams:p})).fee=Ye(h),y=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Je(h),suggestedParams:p}),(v=[])[Ve.FUNDING_TXN]=y,v[Ve.VALIDATOR_APP_CALL]=T,_=a.default.assignGroupID(v),(E=[])[Ve.FUNDING_TXN]={txn:_[Ve.FUNDING_TXN],signers:[c]},E[Ve.VALIDATOR_APP_CALL]={txn:_[Ve.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",E);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Je(t){return function(t,e){var n=re(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(N.V2,t)+Ye(t)+1e5}function Ye(e){return((e?je:Xe)+1)*t.ALGORAND_MIN_TX_FEE}function Ke(){return(Ke=c(u().mark((function t(e){var n,r,s,o,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=C(o,i),f=w(d,2),m=f[0],g=f[1],I=ne.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===Ve.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,s=n.blob;return x.push(r),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function He(){return(He=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,qe(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(fe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Ne.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new M(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Ze,$e={generateTxns:function(t){return ze.apply(this,arguments)},signTxns:function(t){return Ke.apply(this,arguments)},execute:function(t){return He.apply(this,arguments)},getBootstrapFundingTxnAmount:Je,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Je(e)}};var tn,en=(f(Ze={},N.V1_1,Ce),f(Ze,N.V2,$e),f(Ze,"generateTxns",(function(t){return t.contractVersion===N.V1_1?Ce.generateTxns(t):$e.generateTxns(t)})),f(Ze,"signTxns",(function(t){return t.contractVersion===N.V1_1?Ce.signTxns(t):$e.signTxns(t)})),f(Ze,"execute",(function(t){return t.contractVersion===N.V1_1?Ce.execute(t):$e.execute(t)})),f(Ze,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===N.V1_1?Ce.getBootstrapFundingTxnAmount(n):$e.getBootstrapFundingTxnAmount(n)})),Ze);exports.V1_1AddLiquidityTxnIndices=void 0,(tn=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[tn.FEE_TXN=0]="FEE_TXN",tn[tn.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",tn[tn.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",tn[tn.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",tn[tn.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var nn,rn,an,sn,on=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(sn=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",sn.FLEXIBLE="flexible",sn.INITIAL="initial";var un,cn=(f(nn={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(nn,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(nn,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),nn),pn=(f(rn={},exports.V2AddLiquidityType.INITIAL,1),f(rn,exports.V2AddLiquidityType.SINGLE,2),f(rn,exports.V2AddLiquidityType.FLEXIBLE,2),rn),ln=(f(an={},exports.V2AddLiquidityType.INITIAL,3),f(an,exports.V2AddLiquidityType.FLEXIBLE,3),f(an,exports.V2AddLiquidityType.SINGLE,2),an);function dn(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,I=BigInt(o.amount)-m,x=BigInt(u.amount)-g;if(I>x){var A=I;r=fn(A,s),e={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=x;r=fn(h,s),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Wt({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function fn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function mn(e){return(pn[e]+1)*t.ALGORAND_MIN_TX_FEE}var gn=lt("add_liquidity"),In=(f(un={},N.V1_1,[lt("mint")]),f(un,N.V2,{INITIAL_LIQUIDITY:[lt("add_initial_liquidity")],SINGLE_ASSET_MODE:[gn,lt("single")],FLEXIBLE_MODE:[gn,lt("flexible")]}),un);function xn(){return(xn=c(u().mark((function t(e){var n,r,s,o,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.poolAddress,o=e.asset1In,i=e.asset2In,c=e.poolTokenOut,p=e.slippage,l=e.initiatorAddr,d=tt("negative",p,c.amount),t.next=4,n.getTransactionParams().do();case 4:return f=t.sent,m=a.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:It(r,N.V1_1),appArgs:In.v1_1,accounts:[l],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==i.id?[o.id,c.id]:[o.id,i.id,c.id],suggestedParams:f}),g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:s,assetIndex:o.id,amount:o.amount,suggestedParams:f}),I=0===i.id?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:s,amount:i.amount,suggestedParams:f}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:s,assetIndex:i.id,amount:i.amount,suggestedParams:f}),x=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:s,amount:m.fee+x.fee,note:O,suggestedParams:f}),h=a.default.assignGroupID([A,m,g,I,x]),t.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[s]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[s]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function An(){return(An=c(u().mark((function t(e){var n,r,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([r]);case 4:return i=t.sent,c=w(i,3),p=c[0],l=c[1],d=c[2],f=r.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:a.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hn(){return(hn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,pe({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,ot(n,[s]);case 8:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=it(a),I=ct(a),t.next=17,pe({client:n,pool:r,accountAddr:o});case 17:return x=t.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:I});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new M(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Tn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=oi)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:Gt(d),priceImpact:Wt(o({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function zn(t){return Jn.apply(this,arguments)}function Jn(){return(Jn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,ot(n,[a]);case 6:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yn(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*u/(u-i)-o,p=c*qn/(qn-Xn),l=p-c,d=et(a.assetOut,Number(i))/et(a.assetIn,Number(p)),f=et(a.assetOut,Number(u))/et(a.assetIn,Number(o)),m=rt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function Kn(t){return Hn.apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,ot(n,[a]);case 6:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new M({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Cn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Cn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Cn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Cn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,zn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Kn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ct(s),fees:it(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new M(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Cn||(Cn={}));var $n={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Wn({pool:e,reserves:n,assetIn:r,decimals:a}):Yn({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Wn,getFixedOutputSwapQuote:Yn,generateTxns:function(t){return Gn.apply(this,arguments)},signTxns:function(t){return Un.apply(this,arguments)},execute:function(t){return Zn.apply(this,arguments)},executeFixedOutputSwap:Kn},tr=4*t.ALGORAND_MIN_TX_FEE;function er(t){return t instanceof D&&[exports.SwapQuoteErrorType.SwapRouterInsufficientReservesError,exports.SwapQuoteErrorType.SwapRouterLowSwapAmountError,exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,exports.SwapQuoteErrorType.LowSwapAmountError].includes(t.type)}function nr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=w(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(er(n)&&!er(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function rr(){return(rr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,a=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.pools,c=[],(p=i.find((function(t){return t.info.contractVersion===N.V1_1})))?(l={pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:s.decimals},reserves:p.reserves},c.push(new Promise((function(t,e){try{t($n.getFixedInputSwapQuote(l))}catch(t){e(t)}})))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap from non-existent pool"))),d=i.find((function(t){return t.info.contractVersion===N.V2})),c.push(hr.getFixedInputSwapQuote({amount:r,assetInID:X(a),assetOutID:X(s),pool:null!==(n=null==d?void 0:d.info)&&void 0!==n?n:null,decimals:{assetIn:a.decimals,assetOut:s.decimals},isSwapRouterEnabled:o,network:e.network})),t.next=8,nr(c);case 8:return f=t.sent,t.abrupt("return",or(f));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,a=e.assetIn,s=e.assetOut,o=e.pools,i=e.isSwapRouterEnabled,c=[],(p=o.find((function(t){return t.info.contractVersion===N.V1_1})))?(l={pool:p.info,assetOut:{amount:r,id:Number(s.id)},decimals:{assetIn:a.decimals,assetOut:s.decimals},reserves:p.reserves},c.push(new Promise((function(t,e){try{t($n.getFixedOutputSwapQuote(l))}catch(t){e(t)}})))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap from non-existent pool"))),d=o.find((function(t){return t.info.contractVersion===N.V2})),c.push(hr.getFixedOutputSwapQuote({amount:r,assetInID:X(a),assetOutID:X(s),pool:null!==(n=null==d?void 0:d.info)&&void 0!==n?n:null,decimals:{assetIn:a.decimals,assetOut:s.decimals},isSwapRouterEnabled:i,network:e.network})),t.next=8,nr(c);case 8:return f=t.sent,t.abrupt("return",or(f));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sr(t){return t.type===exports.SwapQuoteType.Direct?t.data.quote.rate:kt(t.data.route)}function or(t){for(var e=t[0],n=sr(e),r=1;rn&&(e=a,n=s)}return e}function ur(){return(ur=c(u().mark((function t(e){var n,r,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Vt(o(o({},e),{},{route:e.quote.data.route})));case 2:if(n=e.client,r=e.initiatorAddr,s=e.slippage,i=e.swapType,c=e.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(I=[l.asset1ID,l.asset2ID]).includes(f)&&I.includes(m)&&f!==m){t.next=9;break}throw new M({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return x=t.sent,A=j(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:tt("positive",s,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:tt("negative",s,d.assetOutAmount),y=A?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,suggestedParams:x}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,assetIndex:f,suggestedParams:x}),(v=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:l.validatorAppID,appArgs:[St,bt[i],a.default.encodeUint64(T)],note:mt.getAppCallTxnNoteWithClientName(N.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=ir(i),(w=[])[yt.INPUT_TXN]=y,w[yt.APP_CALL_TXN]=v,_=a.default.assignGroupID(w),t.abrupt("return",[{txn:_[yt.INPUT_TXN],signers:[r]},{txn:_[yt.APP_CALL_TXN],signers:[r]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ir(e){return(Et[e]+1)*t.ALGORAND_MIN_TX_FEE}function cr(){return(cr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,ot(a,[i]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Ge(a,o);case 10:return m=t.sent,g=zt(s),I=Jt(s).id,x=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===I})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:x&&{amount:BigInt(g.amount)-BigInt(x.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:I},quote:s,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pr(){return(pr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.amount,a=e.assetInID,s=e.assetOutID,o=e.decimals,i=e.network,c=e.isSwapRouterEnabled,p=e.pool,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,mr({assetInID:a,assetOutID:s,amount:r,decimals:o,isSwapRouterEnabled:c,network:i,pool:p});case 4:l=t.sent,t.next=10;break;case 7:return t.next=9,Ir({amount:r,assetInID:a,assetOutID:s,decimals:o,isSwapRouterEnabled:c,network:i,pool:p});case 9:l=t.sent;case 10:return t.abrupt("return",l);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function lr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=t[0].reason;if(2===t.length){var n=t[1].reason;if(er(e)&&!er(n))throw e;throw n}throw e}return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function dr(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,s=t.pool;if(!Ne.isPoolReady(s))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");var o,u,i=BigInt(e),c=s.totalFeeShare;if(n===s.asset1ID)o=s.asset1Reserves,u=s.asset2Reserves;else{if(n!==s.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Input asset (#".concat(n,") doesn't belong to the pool ").concat(s.account.address(),"."));o=s.asset2Reserves,u=s.asset1Reserves}var p=Ar({inputSupply:o,outputSupply:u,swapInputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapOutputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(l>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");return{assetInID:n,assetInAmount:i,assetOutID:r,assetOutAmount:l,swapFee:Number(d),rate:et(a.assetOut,Number(l))/et(a.assetIn,Number(i)),priceImpact:f}}function fr(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,s=t.pool;if(!s||!Ne.isPoolReady(s))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");var o,u,i=BigInt(e),c=s.totalFeeShare;if(r===s.asset1ID)o=s.asset2Reserves,u=s.asset1Reserves;else{if(r!==s.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r,") doesn't belong to the pool ").concat(s.account.address(),"."));o=s.asset1Reserves,u=s.asset2Reserves}var p=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:o,totalFeeShare:a}),i=o+u,c=Wt({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:i,totalFeeAmount:u,priceImpact:c}}({inputSupply:o,outputSupply:u,swapOutputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapInputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");return{type:exports.SwapQuoteType.Direct,data:{pool:s,quote:{assetInID:n,assetInAmount:l,assetOutID:r,assetOutAmount:i,swapFee:Number(d),rate:et(a.assetOut,Number(i))/et(a.assetIn,Number(l)),priceImpact:f}}}}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=c(u().mark((function t(e){var n,r,a,s,o,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.decimals,o=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[],p?l.push(new Promise((function(t,e){try{var o=dr({amount:n,assetInID:r,assetOutID:a,decimals:s,pool:p});t({type:exports.SwapQuoteType.Direct,data:{pool:p,quote:o}})}catch(t){e(t)}}))):l.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools."))),o&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedInput,network:i});case 2:return e=t.sent,t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 4:case"end":return t.stop()}}),t)})))()),t.next=6,lr(l);case 6:return d=t.sent,t.abrupt("return",or(d));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ir(t){return xr.apply(this,arguments)}function xr(){return(xr=c(u().mark((function t(e){var n,r,a,s,o,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.decimals,o=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[new Promise((function(t,e){try{t(fr({amount:n,assetInID:r,assetOutID:a,decimals:s,pool:p}))}catch(t){e(t)}}))],o&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedOutput,network:i});case 2:return e=t.sent,t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 4:case"end":return t.stop()}}),t)})))()),t.next=5,lr(l);case 5:return d=t.sent,t.abrupt("return",or(d));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:u,totalFeeAmount:o,priceImpact:Wt({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:u,decimals:s.assetOut}})}}var hr={getQuote:function(t){return pr.apply(this,arguments)},getFixedInputSwapQuote:mr,getFixedInputDirectSwapQuote:dr,getFixedOutputDirectSwapQuote:fr,getFixedOutputSwapQuote:Ir,generateTxns:function(t){return ur.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return cr.apply(this,arguments)},calculateFixedInputSwap:Ar};function Tr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function yr(){return(yr=c(u().mark((function e(n){var r,s,o,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(I=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(tt("negative",l,c)),a.default.encodeUint64(tt("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(x=[])[Rn.ASSET_TRANSFER_TXN]=g,x[Rn.APP_CALL_TXN]=I,A=a.default.assignGroupID(x),e.abrupt("return",[{txn:A[Rn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[Rn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=c(u().mark((function e(n){var r,s,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),I=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(x=0,A=0,h=tt("negative",l,p),c!==f){e.next=17;break}x=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}x=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:I,amount:i,suggestedParams:d}),(y=a.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(x),a.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[Rn.ASSET_TRANSFER_TXN]=T,v[Rn.APP_CALL_TXN]=y,w=a.default.assignGroupID(v),e.abrupt("return",[{txn:w[Rn.ASSET_TRANSFER_TXN],signers:[o]},{txn:w[Rn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function wr(){return(wr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,ot(r,[s]);case 3:return o=t.sent,i=w(o,1),c=i[0].txnID,t.next=8,Ge(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var _r,Er,Sr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Tr(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=Tr(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=hr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var I=hr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=I.swapOutputAmount,A=I.totalFeeAmount,h=I.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return yr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return wr.apply(this,arguments)}},br=(f(_r={},N.V1_1,jn),f(_r,N.V2,Sr),_r),Dr=(f(Er={},N.V1_1,$n),f(Er,N.V2,hr),f(Er,"getQuote",(function(t){var e=t.type,n=t.pools;if(!t.isSwapRouterEnabled&&n.every((function(t){return Se(t.reserves)})))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");if(e===exports.SwapType.FixedInput)return function(t){return rr.apply(this,arguments)}(t);if(e===exports.SwapType.FixedOutput)return function(t){return ar.apply(this,arguments)}(t);throw new D(exports.SwapQuoteErrorType.InvalidSwapTypeError,"Invalid swap type")})),f(Er,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1?$n.generateTxns(o(o({},t),{},{quote:t.quote.data})):hr.generateTxns(t)})),f(Er,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1){var e=t.quote.data.pool;return $n.signTxns(o({pool:e},t))}return hr.signTxns(t)})),f(Er,"execute",(function(t){return t.contractVersion===N.V1_1?$n.execute(t):hr.execute(t)})),Er);function Or(){return(Or=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,Nr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,ot(n,[o]);case 7:return i=t.sent,c=w(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:it(a),confirmedRound:d,txnID:l,groupID:ct(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function Nr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=c(u().mark((function t(e){var n,r,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=w(o,1),c=i[0],p=r.account.lsig,l=n.map((function(t,e){return 0===e?c:a.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function kr(){return kr=c(u().mark((function t(e){var n,r,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.data,s=e.initiatorSigner,t.prev=1,o=r.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:it(e),groupID:ct(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var r=c(u().mark((function r(s,o){var c,p,l,d,f,m;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,c=t.txns.map((function(n,r){return 0===r?i[e]:a.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),r.next=4,ot(n,[c]);case 4:p=r.sent,l=w(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),r.next=15;break;case 12:r.prev=12,r.t0=r.catch(0),o(r.t0);case 15:case"end":return r.stop()}}),r,null,[[0,12]])})));return function(t,e){return r.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),kr.apply(this,arguments)}function Lr(){return(Lr=c(u().mark((function t(e){var n,r,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.assetID,o=e.assetOut,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=r.account.address(),l=a.default.makeApplicationNoOpTxnFromObject({from:p,appIndex:r.validatorAppID,appArgs:[lt("redeem")],note:mt.getAppCallTxnNoteWithClientName(r.contractVersion),accounts:[i],foreignAssets:0==r.asset2ID?[r.asset1ID,r.poolTokenID]:[r.asset1ID,r.asset2ID,r.poolTokenID],suggestedParams:c}),d=0===s?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:i,amount:BigInt(o),suggestedParams:c}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:i,assetIndex:s,amount:BigInt(o),suggestedParams:c}),f=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:p,amount:l.fee+d.fee,note:O,suggestedParams:c}),m=a.default.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[i]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[lt("commit"),i],note:U([lt("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function Fr(){return(Fr=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Rr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[lt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=P,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Ln,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=en,exports.CONTRACT_VERSION=N,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=k,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=br,exports.Swap=Dr,exports.SwapQuoteError=D,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Fn,exports.V2AddLiquidityTxnIndices=cn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=tt,exports.calculateAccountMinimumRequiredBalance=oe,exports.calculatePriceImpact=Wt,exports.calculateSwapRate=Gt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=Ne,exports.prepareCommitTransactions=function(t){return Fr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return kr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Or.apply(this,arguments)},exports.sendAndWaitRawTransaction=ot,exports.sumUpTxnFees=it,exports.tinymanContract_v1_1=$t,exports.tinymanContract_v2=ne,exports.tinymanJSSDKConfig=mt; diff --git a/dist/swap/index.d.ts b/dist/swap/index.d.ts index 11b54255..3cad225c 100644 --- a/dist/swap/index.d.ts +++ b/dist/swap/index.d.ts @@ -47,10 +47,12 @@ export declare const Swap: { }) => Promise>; }; v2: { - getQuote: ({ type, pool, asset, decimals, network, isSwapRouterEnabled }: { + getQuote: ({ type, amount, assetInID, assetOutID, decimals, network, isSwapRouterEnabled, pool }: { type: import("./constants").SwapType; - pool: import("..").V2PoolInfo; - asset: import("../util/asset/assetModels").AssetWithIdAndAmount; + amount: number | bigint; + assetInID: number; + assetOutID: number; + pool: import("..").V2PoolInfo | null; decimals: { assetIn: number; assetOut: number; @@ -58,19 +60,43 @@ export declare const Swap: { network: import("..").SupportedNetwork; isSwapRouterEnabled?: boolean | undefined; }) => Promise; - getFixedInputSwapQuote: ({ assetIn, decimals, pool, isSwapRouterEnabled, network }: { - pool: import("..").V2PoolInfo; - assetIn: import("../util/asset/assetModels").AssetWithIdAndAmount; + getFixedInputSwapQuote: ({ amount, assetInID, assetOutID, decimals, isSwapRouterEnabled, network, pool }: { + amount: number | bigint; + assetInID: number; + assetOutID: number; decimals: { assetIn: number; assetOut: number; }; network: import("..").SupportedNetwork; + pool: import("..").V2PoolInfo | null; isSwapRouterEnabled?: boolean | undefined; }) => Promise; - getFixedOutputSwapQuote: ({ assetOut, decimals, pool, isSwapRouterEnabled, network }: { + getFixedInputDirectSwapQuote: ({ amount, assetInID, assetOutID, decimals, pool }: { pool: import("..").V2PoolInfo; - assetOut: import("../util/asset/assetModels").AssetWithIdAndAmount; + amount: number | bigint; + assetInID: number; + assetOutID: number; + decimals: { + assetIn: number; + assetOut: number; + }; + }) => import("./types").DirectSwapQuote; + getFixedOutputDirectSwapQuote: ({ amount, assetInID, assetOutID, decimals, pool }: { + pool: import("..").V2PoolInfo | null; + amount: number | bigint; + assetInID: number; + assetOutID: number; + decimals: { + assetIn: number; + assetOut: number; + }; + }) => import("./types").SwapQuote; + getFixedOutputSwapQuote: ({ amount, assetInID, assetOutID, decimals, isSwapRouterEnabled, network, pool }: { + amount: number | bigint; + assetInID: number; + assetOutID: number; + pool: import("..").V2PoolInfo | null; decimals: { assetIn: number; assetOut: number; diff --git a/dist/swap/utils.d.ts b/dist/swap/utils.d.ts index d1a64177..1bcaea29 100644 --- a/dist/swap/utils.d.ts +++ b/dist/swap/utils.d.ts @@ -9,6 +9,7 @@ import { SwapType } from "./constants"; * Gets the best quote for swap from the pools and swap router and returns the best option. */ export declare function getQuote(params: GetSwapQuoteParams): Promise; +export declare function isSwapQuoteErrorCausedByAmount(error: Error): boolean; /** * Gets quotes for fixed input swap the pools and swap router, * and returns the best quote (with the highest rate). @@ -19,6 +20,10 @@ export declare function getFixedInputSwapQuote(params: GetSwapQuoteBySwapTypePar * and returns the best quote (with the highest rate). */ export declare function getFixedOutputSwapQuote(params: GetSwapQuoteBySwapTypeParams): Promise; +/** + * Compares the given quotes and returns the best one (with the highest rate). + */ +export declare function getBestQuote(quotes: SwapQuote[]): SwapQuote; export declare function generateTxns(params: GenerateSwapTxnsParams): Promise; export declare function signTxns(params: { quote: SwapQuote; @@ -40,7 +45,7 @@ export declare function execute(params: ({ } | { contractVersion: typeof CONTRACT_VERSION.V2; quote: SwapQuote; -}) & ExecuteCommonParams): Promise | Promise; +}) & ExecuteCommonParams): Promise | Promise; /** * @returns the total fee that will be paid by the user * for the swap transaction with given parameters diff --git a/dist/swap/v2/index.d.ts b/dist/swap/v2/index.d.ts index 2fbfdffd..fb4e19f8 100644 --- a/dist/swap/v2/index.d.ts +++ b/dist/swap/v2/index.d.ts @@ -1,9 +1,8 @@ import { Algodv2 } from "algosdk"; import { InitiatorSigner, SignerTransaction, SupportedNetwork } from "../../util/commonTypes"; import { V2PoolInfo } from "../../util/pool/poolTypes"; -import { GenerateSwapTxnsParams, SwapQuote, V2SwapExecution } from "../types"; +import { DirectSwapQuote, GenerateSwapTxnsParams, SwapQuote, V2SwapExecution } from "../types"; import { SwapType } from "../constants"; -import { AssetWithIdAndAmount } from "../../util/asset/assetModels"; declare function generateTxns(params: GenerateSwapTxnsParams): Promise; declare function signTxns({ txGroup, initiatorSigner }: { txGroup: SignerTransaction[]; @@ -27,10 +26,12 @@ declare function execute({ client, quote, txGroup, signedTxns }: { * @param decimals.assetOut - Decimals quantity for the output asset * @returns A promise for the Swap quote */ -declare function getQuote({ type, pool, asset, decimals, network, isSwapRouterEnabled }: { +declare function getQuote({ type, amount, assetInID, assetOutID, decimals, network, isSwapRouterEnabled, pool }: { type: SwapType; - pool: V2PoolInfo; - asset: AssetWithIdAndAmount; + amount: number | bigint; + assetInID: number; + assetOutID: number; + pool: V2PoolInfo | null; decimals: { assetIn: number; assetOut: number; @@ -38,25 +39,49 @@ declare function getQuote({ type, pool, asset, decimals, network, isSwapRouterEn network: SupportedNetwork; isSwapRouterEnabled?: boolean; }): Promise; +declare function getFixedInputDirectSwapQuote({ amount, assetInID, assetOutID, decimals, pool }: { + pool: V2PoolInfo; + amount: number | bigint; + assetInID: number; + assetOutID: number; + decimals: { + assetIn: number; + assetOut: number; + }; +}): DirectSwapQuote; +declare function getFixedOutputDirectSwapQuote({ amount, assetInID, assetOutID, decimals, pool }: { + pool: V2PoolInfo | null; + amount: number | bigint; + assetInID: number; + assetOutID: number; + decimals: { + assetIn: number; + assetOut: number; + }; +}): SwapQuote; /** * @returns A quote for a fixed input swap. Does NOT execute any transactions. */ -declare function getFixedInputSwapQuote({ assetIn, decimals, pool, isSwapRouterEnabled, network }: { - pool: V2PoolInfo; - assetIn: AssetWithIdAndAmount; +declare function getFixedInputSwapQuote({ amount, assetInID, assetOutID, decimals, isSwapRouterEnabled, network, pool }: { + amount: number | bigint; + assetInID: number; + assetOutID: number; decimals: { assetIn: number; assetOut: number; }; network: SupportedNetwork; + pool: V2PoolInfo | null; isSwapRouterEnabled?: boolean; }): Promise; /** * @returns A quote for a fixed output swap. Does NOT execute any transactions. */ -declare function getFixedOutputSwapQuote({ assetOut, decimals, pool, isSwapRouterEnabled, network }: { - pool: V2PoolInfo; - assetOut: AssetWithIdAndAmount; +declare function getFixedOutputSwapQuote({ amount, assetInID, assetOutID, decimals, isSwapRouterEnabled, network, pool }: { + amount: number | bigint; + assetInID: number; + assetOutID: number; + pool: V2PoolInfo | null; decimals: { assetIn: number; assetOut: number; @@ -81,6 +106,8 @@ declare function calculateFixedInputSwap({ inputSupply, outputSupply, swapInputA export declare const SwapV2: { getQuote: typeof getQuote; getFixedInputSwapQuote: typeof getFixedInputSwapQuote; + getFixedInputDirectSwapQuote: typeof getFixedInputDirectSwapQuote; + getFixedOutputDirectSwapQuote: typeof getFixedOutputDirectSwapQuote; getFixedOutputSwapQuote: typeof getFixedOutputSwapQuote; generateTxns: typeof generateTxns; signTxns: typeof signTxns; diff --git a/dist/util/commonTypes.d.ts b/dist/util/commonTypes.d.ts index 2d934200..862d0d1a 100644 --- a/dist/util/commonTypes.d.ts +++ b/dist/util/commonTypes.d.ts @@ -10,3 +10,11 @@ export interface SignerTransaction { } export declare type InitiatorSigner = (txGroupList: SignerTransaction[][]) => Promise; export declare type SupportedNetwork = "testnet" | "mainnet"; +export interface TinymanApiErrorDetailShape { + [x: string]: undefined | any; +} +export interface TinymanApiErrorShape { + type: Type; + detail: TinymanApiErrorDetailShape; + fallback_message: string; +} diff --git a/dist/util/error/NoAvailablePoolOrRouteError.d.ts b/dist/util/error/NoAvailablePoolOrRouteError.d.ts new file mode 100644 index 00000000..4531ac8a --- /dev/null +++ b/dist/util/error/NoAvailablePoolOrRouteError.d.ts @@ -0,0 +1,4 @@ +declare class NoAvailablePoolOrRouteError extends Error { + constructor(message?: string); +} +export default NoAvailablePoolOrRouteError; diff --git a/dist/util/error/NonExistentPoolError.d.ts b/dist/util/error/NonExistentPoolError.d.ts new file mode 100644 index 00000000..92f4f8a1 --- /dev/null +++ b/dist/util/error/NonExistentPoolError.d.ts @@ -0,0 +1,4 @@ +declare class NonExistentPoolError extends Error { + constructor(message?: string); +} +export default NonExistentPoolError; diff --git a/dist/util/error/SwapError.d.ts b/dist/util/error/SwapError.d.ts new file mode 100644 index 00000000..9c2340b3 --- /dev/null +++ b/dist/util/error/SwapError.d.ts @@ -0,0 +1,15 @@ +export declare enum SwapErrorType { + SwapRouterStaleDataError = "SwapRouterStaleDataError", + SwapRouterNoRouteError = "SwapRouterNoRouteError", + SwapRouterLowSwapAmountError = "SwapRouterLowSwapAmountError", + SwapRouterInsufficientReservesError = "SwapRouterInsufficientReservesError", + SwapRouterPoolHasNoLiquidityError = "SwapRouterPoolHasNoLiquidityError", + NoAvailablePoolError = "NoAvailablePoolError", + OutputAmountExceedsAvailableLiquidityError = "OutputAmountExceedsAvailableLiquidityError" +} +declare class SwapError extends Error { + type: SwapErrorType; + message: string; + constructor(type: SwapErrorType, message: string); +} +export default SwapError; diff --git a/dist/util/error/SwapQuoteError.d.ts b/dist/util/error/SwapQuoteError.d.ts new file mode 100644 index 00000000..aa66c9f4 --- /dev/null +++ b/dist/util/error/SwapQuoteError.d.ts @@ -0,0 +1,19 @@ +export declare enum SwapQuoteErrorType { + SwapRouterStaleDataError = "SwapRouterStaleDataError", + SwapRouterNoRouteError = "SwapRouterNoRouteError", + SwapRouterLowSwapAmountError = "SwapRouterLowSwapAmountError", + SwapRouterInsufficientReservesError = "SwapRouterInsufficientReservesError", + SwapRouterPoolHasNoLiquidityError = "SwapRouterPoolHasNoLiquidityError", + NoAvailablePoolError = "NoAvailablePoolError", + OutputAmountExceedsAvailableLiquidityError = "OutputAmountExceedsAvailableLiquidityError", + UnknownError = "UnknownError", + LowSwapAmountError = "LowSwapAmountError", + AssetDoesNotBelongToPoolError = "AssetDoesNotBelongToPoolError", + InvalidSwapTypeError = "InvalidSwapTypeError" +} +declare class SwapQuoteError extends Error { + type: SwapQuoteErrorType; + message: string; + constructor(type: SwapQuoteErrorType, message: string); +} +export default SwapQuoteError; diff --git a/dist/util/util.d.ts b/dist/util/util.d.ts index b1692fab..ebb53388 100644 --- a/dist/util/util.d.ts +++ b/dist/util/util.d.ts @@ -1,6 +1,6 @@ /// import { Algodv2 } from "algosdk"; -import { SignerTransaction } from "./commonTypes"; +import { SignerTransaction, TinymanApiErrorShape } from "./commonTypes"; import { AccountInformation } from "./account/accountTypes"; export declare function decodeState({ stateArray, shouldDecodeKeys }: { stateArray: AccountInformation["apps-local-state"][0]["key-value"]; @@ -56,3 +56,4 @@ export declare function encodeInteger(number: any): number[]; * Converts a text into bytes */ export declare function encodeString(text: string): Uint8Array; +export declare function hasTinymanApiErrorShape(error: any): error is TinymanApiErrorShape; diff --git a/src/index.ts b/src/index.ts index 88c1ab44..4c491f8d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ +import SwapQuoteError from "./util/error/SwapQuoteError"; + export type { InitiatorSigner, SignerTransaction, @@ -120,6 +122,9 @@ export {SwapType} from "./swap/constants"; export {Swap} from "./swap"; // eslint-disable-next-line no-duplicate-imports export {SwapQuoteType} from "./swap/types"; +export {SwapQuoteError}; +// eslint-disable-next-line no-duplicate-imports +export {SwapQuoteErrorType} from "./util/error/SwapQuoteError"; export { redeemExcessAsset, diff --git a/src/swap/utils.ts b/src/swap/utils.ts index 24b0683d..0fc4ba02 100644 --- a/src/swap/utils.ts +++ b/src/swap/utils.ts @@ -16,19 +16,23 @@ import {SwapV1_1} from "./v1_1"; import {SwapV2} from "./v2"; import {V1_1_SWAP_TOTAL_FEE} from "./v1_1/constants"; import {getV2SwapTotalFee} from "./v2/util"; -import {isPoolEmpty} from "../util/pool/common"; import {getSwapRouteRate} from "./v2/router/util"; -import OutputAmountExceedsAvailableLiquidityError from "../util/error/OutputAmountExceedsAvailableLiquidityError"; import {getSwapQuoteContractVersion} from "./common/utils"; +import {getAssetId} from "../util/asset/assetUtils"; +import {isPoolEmpty} from "../util/pool/common"; +import SwapQuoteError, {SwapQuoteErrorType} from "../util/error/SwapQuoteError"; /** * Gets the best quote for swap from the pools and swap router and returns the best option. */ export function getQuote(params: GetSwapQuoteParams): Promise { - const {type, isSwapRouterEnabled, pools} = params; + const {type, pools, isSwapRouterEnabled} = params; if (!isSwapRouterEnabled && pools.every((pool) => isPoolEmpty(pool.reserves))) { - throw new Error("No pools available for swap"); + throw new SwapQuoteError( + SwapQuoteErrorType.NoAvailablePoolError, + "There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools." + ); } if (type === SwapType.FixedInput) { @@ -37,7 +41,19 @@ export function getQuote(params: GetSwapQuoteParams): Promise { return getFixedOutputSwapQuote(params); } - return Promise.reject(new Error("Invalid swap type")); + throw new SwapQuoteError(SwapQuoteErrorType.InvalidSwapTypeError, "Invalid swap type"); +} + +export function isSwapQuoteErrorCausedByAmount(error: Error): boolean { + return ( + error instanceof SwapQuoteError && + [ + SwapQuoteErrorType.SwapRouterInsufficientReservesError, + SwapQuoteErrorType.SwapRouterLowSwapAmountError, + SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError, + SwapQuoteErrorType.LowSwapAmountError + ].includes(error.type) + ); } /** @@ -47,21 +63,28 @@ export function getQuote(params: GetSwapQuoteParams): Promise { */ function validateQuotes(promises: Promise[]): Promise { return Promise.allSettled(promises).then((results) => { - if ( - results.every( - (result) => - result.status === "rejected" && - result.reason instanceof OutputAmountExceedsAvailableLiquidityError - ) - ) { - throw new OutputAmountExceedsAvailableLiquidityError(); + if (results.every((result) => result.status === "rejected")) { + const [v1_1PoolError, v2PoolError] = (results as PromiseRejectedResult[]).map( + (result) => result.reason + ) as SwapQuoteError[]; + + if ( + isSwapQuoteErrorCausedByAmount(v1_1PoolError) && + !isSwapQuoteErrorCausedByAmount(v2PoolError) + ) { + throw v1_1PoolError; + } + + throw v2PoolError; } - return ( + const filteredResults = ( results.filter( (result) => result.status === "fulfilled" && result.value !== undefined ) as PromiseFulfilledResult[] ).map((result) => result.value); + + return filteredResults; }); } @@ -72,34 +95,55 @@ function validateQuotes(promises: Promise[]): Promise { export async function getFixedInputSwapQuote( params: GetSwapQuoteBySwapTypeParams ): Promise { - const {amount, assetIn, assetOut, pools, isSwapRouterEnabled} = params; - - const quotePromises = pools.map>((pool) => { - return new Promise(async (resolve, reject) => { - let quote: SwapQuote | undefined; - - const quoteGetterArgs = { - pool: pool.info, - assetIn: {amount, id: Number(assetIn.id)}, - decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, - reserves: pool.reserves, - isSwapRouterEnabled, - network: params.network - }; - - try { - if (pool.info.contractVersion === CONTRACT_VERSION.V1_1) { - quote = SwapV1_1.getFixedInputSwapQuote(quoteGetterArgs); - } else { - quote = await SwapV2.getFixedInputSwapQuote(quoteGetterArgs); + const {amount, assetIn, assetOut, isSwapRouterEnabled, pools} = params; + + const quotePromises: Promise[] = []; + + const v1_1Pool = pools.find( + (pool) => pool.info.contractVersion === CONTRACT_VERSION.V1_1 + ); + + if (v1_1Pool) { + const quoteGetterArgs = { + pool: v1_1Pool.info, + assetIn: {amount, id: Number(assetIn.id)}, + decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, + reserves: v1_1Pool.reserves + }; + + quotePromises.push( + new Promise((resolve, reject) => { + try { + resolve(SwapV1_1.getFixedInputSwapQuote(quoteGetterArgs)); + } catch (error) { + reject(error); } + }) + ); + } else { + quotePromises.push( + Promise.reject( + new SwapQuoteError( + SwapQuoteErrorType.NoAvailablePoolError, + "Trying to swap from non-existent pool" + ) + ) + ); + } - resolve(quote); - } catch (error) { - reject(error); - } - }); - }); + const v2Pool = pools.find((pool) => pool.info.contractVersion === CONTRACT_VERSION.V2); + + quotePromises.push( + SwapV2.getFixedInputSwapQuote({ + amount, + assetInID: getAssetId(assetIn), + assetOutID: getAssetId(assetOut), + pool: v2Pool?.info ?? null, + decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, + isSwapRouterEnabled, + network: params.network + }) + ); const validQuotes = await validateQuotes(quotePromises); @@ -115,31 +159,53 @@ export async function getFixedOutputSwapQuote( ): Promise { const {amount, assetIn, assetOut, pools, isSwapRouterEnabled} = params; - const quotePromises = pools.map>((pool) => { - return new Promise(async (resolve, reject) => { - let quote: SwapQuote | undefined; - const quoteGetterArgs = { - pool: pool.info, - assetOut: {amount, id: Number(assetOut.id)}, - decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, - reserves: pool.reserves, - isSwapRouterEnabled, - network: params.network - }; - - try { - if (pool.info.contractVersion === CONTRACT_VERSION.V1_1) { - quote = SwapV1_1.getFixedOutputSwapQuote(quoteGetterArgs); - } else { - quote = await SwapV2.getFixedOutputSwapQuote(quoteGetterArgs); + const quotePromises: Promise[] = []; + + const v1_1Pool = pools.find( + (pool) => pool.info.contractVersion === CONTRACT_VERSION.V1_1 + ); + + if (v1_1Pool) { + const quoteGetterArgs = { + pool: v1_1Pool.info, + assetOut: {amount, id: Number(assetOut.id)}, + decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, + reserves: v1_1Pool.reserves + }; + + quotePromises.push( + new Promise((resolve, reject) => { + try { + resolve(SwapV1_1.getFixedOutputSwapQuote(quoteGetterArgs)); + } catch (error) { + reject(error); } + }) + ); + } else { + quotePromises.push( + Promise.reject( + new SwapQuoteError( + SwapQuoteErrorType.NoAvailablePoolError, + "Trying to swap from non-existent pool" + ) + ) + ); + } - resolve(quote); - } catch (error) { - reject(error); - } - }); - }); + const v2Pool = pools.find((pool) => pool.info.contractVersion === CONTRACT_VERSION.V2); + + quotePromises.push( + SwapV2.getFixedOutputSwapQuote({ + amount, + assetInID: getAssetId(assetIn), + assetOutID: getAssetId(assetOut), + pool: v2Pool?.info ?? null, + decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, + isSwapRouterEnabled, + network: params.network + }) + ); const validQuotes = await validateQuotes(quotePromises); @@ -160,7 +226,7 @@ function getSwapQuoteRate(quote: SwapQuote): number { /** * Compares the given quotes and returns the best one (with the highest rate). */ -function getBestQuote(quotes: SwapQuote[]): SwapQuote { +export function getBestQuote(quotes: SwapQuote[]): SwapQuote { let bestQuote: SwapQuote = quotes[0]; let bestQuoteRate = getSwapQuoteRate(bestQuote); diff --git a/src/swap/v1_1/index.ts b/src/swap/v1_1/index.ts index 8af53a72..563a78ac 100644 --- a/src/swap/v1_1/index.ts +++ b/src/swap/v1_1/index.ts @@ -24,10 +24,10 @@ import { } from "../types"; import {SwapType} from "../constants"; import {calculatePriceImpact, calculateSwapRate} from "../common/utils"; -import OutputAmountExceedsAvailableLiquidityError from "../../util/error/OutputAmountExceedsAvailableLiquidityError"; import {AssetWithIdAndAmount} from "../../util/asset/assetModels"; import {tinymanJSSDKConfig} from "../../config"; import {CONTRACT_VERSION} from "../../contract/constants"; +import SwapQuoteError, {SwapQuoteErrorType} from "../../util/error/SwapQuoteError"; // FEE = %0.3 or 3/1000 const FEE_NUMERATOR = 3n; @@ -228,7 +228,10 @@ function getFixedInputSwapQuote({ decimals: {assetIn: number; assetOut: number}; }): SwapQuote { if (pool.status !== PoolStatus.READY) { - throw new TinymanError({pool, assetIn}, "Trying to swap on a non-existent pool"); + throw new SwapQuoteError( + SwapQuoteErrorType.NoAvailablePoolError, + "Trying to swap on a non-existent pool" + ); } const assetInAmount = BigInt(assetIn.amount); @@ -246,8 +249,8 @@ function getFixedInputSwapQuote({ inputSupply = reserves.asset2; outputSupply = reserves.asset1; } else { - throw new TinymanError( - {pool, assetIn}, + throw new SwapQuoteError( + SwapQuoteErrorType.AssetDoesNotBelongToPoolError, `Input asset (#${assetIn.id}) doesn't belong to the pool ${pool.account.address()}.` ); } @@ -259,7 +262,10 @@ function getFixedInputSwapQuote({ const assetOutAmount = outputSupply - k / (inputSupply + assetInAmountMinusFee); if (assetOutAmount > outputSupply) { - throw new OutputAmountExceedsAvailableLiquidityError(); + throw new SwapQuoteError( + SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError, + "Output amount exceeds available liquidity." + ); } const assetDataForSwapUtils = { @@ -387,7 +393,10 @@ function getFixedOutputSwapQuote({ decimals: {assetIn: number; assetOut: number}; }): SwapQuote { if (pool.status !== PoolStatus.READY) { - throw new TinymanError({pool, assetOut}, "Trying to swap on a non-existent pool"); + throw new SwapQuoteError( + SwapQuoteErrorType.NoAvailablePoolError, + "Trying to swap on a non-existent pool" + ); } const assetOutAmount = BigInt(assetOut.amount); @@ -405,8 +414,8 @@ function getFixedOutputSwapQuote({ inputSupply = reserves.asset1; outputSupply = reserves.asset2; } else { - throw new TinymanError( - {pool, assetOut}, + throw new SwapQuoteError( + SwapQuoteErrorType.AssetDoesNotBelongToPoolError, `Output asset (#${ assetOut.id }) doesn't belong to the pool ${pool.account.address()}.` @@ -414,7 +423,10 @@ function getFixedOutputSwapQuote({ } if (assetOutAmount > outputSupply) { - throw new OutputAmountExceedsAvailableLiquidityError(); + throw new SwapQuoteError( + SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError, + "Output amount exceeds available liquidity." + ); } const k = inputSupply * outputSupply; diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index a64affb4..cb954e31 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -11,7 +11,7 @@ import { SupportedNetwork } from "../../util/commonTypes"; import TinymanError from "../../util/error/TinymanError"; -import {PoolStatus, V2PoolInfo} from "../../util/pool/poolTypes"; +import {V2PoolInfo} from "../../util/pool/poolTypes"; import { DirectSwapQuote, GenerateSwapTxnsParams, @@ -33,12 +33,12 @@ import { getAssetOutFromSwapQuote } from "../common/utils"; import {getAppCallInnerTxns} from "../../util/transaction/transactionUtils"; -import OutputAmountExceedsAvailableLiquidityError from "../../util/error/OutputAmountExceedsAvailableLiquidityError"; -import {AssetWithIdAndAmount} from "../../util/asset/assetModels"; import {tinymanJSSDKConfig} from "../../config"; import {CONTRACT_VERSION} from "../../contract/constants"; import {generateSwapRouterTxns, getSwapRoute} from "./router/swap-router"; -import {getAssetOutFromSwapRoute} from "./router/util"; +import {poolUtils} from "../../util/pool"; +import {getBestQuote, isSwapQuoteErrorCausedByAmount} from "../utils"; +import SwapQuoteError, {SwapQuoteErrorType} from "../../util/error/SwapQuoteError"; async function generateTxns( params: GenerateSwapTxnsParams @@ -207,15 +207,19 @@ async function execute({ */ async function getQuote({ type, - pool, - asset, + amount, + assetInID, + assetOutID, decimals, network, - isSwapRouterEnabled + isSwapRouterEnabled, + pool }: { type: SwapType; - pool: V2PoolInfo; - asset: AssetWithIdAndAmount; + amount: number | bigint; + assetInID: number; + assetOutID: number; + pool: V2PoolInfo | null; decimals: {assetIn: number; assetOut: number}; network: SupportedNetwork; isSwapRouterEnabled?: boolean; @@ -224,64 +228,100 @@ async function getQuote({ if (type === SwapType.FixedInput) { quote = await getFixedInputSwapQuote({ - pool, - assetIn: asset, + assetInID, + assetOutID, + amount, decimals, isSwapRouterEnabled, - network + network, + pool }); } else { quote = await getFixedOutputSwapQuote({ - pool, - assetOut: asset, + amount, + assetInID, + assetOutID, decimals, isSwapRouterEnabled, - network + network, + pool }); } return quote; } -/** - * @returns A quote for a fixed input swap. Does NOT execute any transactions. - */ -async function getFixedInputSwapQuote({ - assetIn, +function validateQuotes(quotePromises: Promise[]): Promise { + return Promise.allSettled(quotePromises).then((results) => { + if (results.every((result) => result.status === "rejected")) { + const directQuoteError = (results[0] as PromiseRejectedResult) + .reason as SwapQuoteError; + + // If all promises are rejected and there are 2 of them, it means that both direct and router quotes failed. + // In this case, if the direct quote failed because of an OutputAmountExceedsAvailableLiquidityError and the router quote failed because of a SwapRouterRouteError, + // we want to throw OutputAmountExceedsAvailableLiquidityError error instead of the SwapRouterRouteError. Otherwise, we want to throw the error from swap router. + if (results.length === 2) { + const routerQuoteError = (results[1] as PromiseRejectedResult) + .reason as SwapQuoteError; + + if ( + isSwapQuoteErrorCausedByAmount(directQuoteError) && + !isSwapQuoteErrorCausedByAmount(routerQuoteError) + ) { + throw directQuoteError; + } + + throw routerQuoteError; + } + + // Otherwise, we want to throw the error from the direct quote. + throw directQuoteError; + } + + return ( + results.filter( + (result) => result.status === "fulfilled" && result.value !== undefined + ) as PromiseFulfilledResult[] + ).map((result) => result.value); + }); +} + +function getFixedInputDirectSwapQuote({ + amount, + assetInID, + assetOutID, decimals, - pool, - isSwapRouterEnabled, - network + pool }: { pool: V2PoolInfo; - assetIn: AssetWithIdAndAmount; + amount: number | bigint; + assetInID: number; + assetOutID: number; decimals: {assetIn: number; assetOut: number}; - network: SupportedNetwork; - isSwapRouterEnabled?: boolean; -}): Promise { - if (pool.status !== PoolStatus.READY && !isSwapRouterEnabled) { - throw new TinymanError({pool, assetIn}, "Trying to swap on a non-existent pool"); +}): DirectSwapQuote { + if (!poolUtils.isPoolReady(pool)) { + throw new SwapQuoteError( + SwapQuoteErrorType.NoAvailablePoolError, + "There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools." + ); } - const assetInAmount = BigInt(assetIn.amount); + const assetInAmount = BigInt(amount); const totalFeeShare = pool.totalFeeShare!; - let assetOutID: number; let inputSupply: bigint; let outputSupply: bigint; - if (assetIn.id === pool.asset1ID) { - assetOutID = pool.asset2ID; + if (assetInID === pool.asset1ID) { inputSupply = pool.asset1Reserves!; outputSupply = pool.asset2Reserves!; - } else if (assetIn.id === pool.asset2ID) { - assetOutID = pool.asset1ID; + } else if (assetInID === pool.asset2ID) { inputSupply = pool.asset2Reserves!; outputSupply = pool.asset1Reserves!; } else { - throw new TinymanError( - {pool, assetIn}, - `Input asset (#${assetIn.id}) doesn't belong to the pool ${pool.account.address()}.` + throw new SwapQuoteError( + SwapQuoteErrorType.AssetDoesNotBelongToPoolError, + `Input asset (#${assetInID}) doesn't belong to the pool ${pool.account.address()}.` ); } @@ -293,8 +333,15 @@ async function getFixedInputSwapQuote({ decimals }); - const directSwapQuote: DirectSwapQuote = { - assetInID: assetIn.id, + if (swapOutputAmount > outputSupply) { + throw new SwapQuoteError( + SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError, + "Output amount exceeds available liquidity." + ); + } + + return { + assetInID, assetInAmount, assetOutID, assetOutAmount: swapOutputAmount, @@ -304,81 +351,43 @@ async function getFixedInputSwapQuote({ convertFromBaseUnits(decimals.assetIn, Number(assetInAmount)), priceImpact }; - - if (isSwapRouterEnabled) { - const swapRoute = await getSwapRoute({ - amount: assetIn.amount, - assetInID: Number(assetIn.id), - assetOutID, - swapType: SwapType.FixedInput, - network - }); - - if ( - swapRoute.route.length > 1 && - BigInt(getAssetOutFromSwapRoute(swapRoute.route).amount) > - directSwapQuote.assetOutAmount - ) { - return { - data: swapRoute, - type: SwapQuoteType.Router - }; - } - } - - if (swapOutputAmount > outputSupply) { - throw new OutputAmountExceedsAvailableLiquidityError(); - } - - return { - data: { - pool, - quote: directSwapQuote - }, - type: SwapQuoteType.Direct - }; } -/** - * @returns A quote for a fixed output swap. Does NOT execute any transactions. - */ -async function getFixedOutputSwapQuote({ - assetOut, +function getFixedOutputDirectSwapQuote({ + amount, + assetInID, + assetOutID, decimals, - pool, - isSwapRouterEnabled, - network + pool }: { - pool: V2PoolInfo; - assetOut: AssetWithIdAndAmount; + pool: V2PoolInfo | null; + amount: number | bigint; + assetInID: number; + assetOutID: number; decimals: {assetIn: number; assetOut: number}; - network: SupportedNetwork; - isSwapRouterEnabled?: boolean; -}): Promise { - if (pool.status !== PoolStatus.READY && !isSwapRouterEnabled) { - throw new TinymanError({pool, assetOut}, "Trying to swap on a non-existent pool"); +}): SwapQuote { + if (!pool || !poolUtils.isPoolReady(pool)) { + throw new SwapQuoteError( + SwapQuoteErrorType.NoAvailablePoolError, + "There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools." + ); } - const assetOutAmount = BigInt(assetOut.amount); + const assetOutAmount = BigInt(amount); const totalFeeShare = pool.totalFeeShare!; - let assetInID: number; let inputSupply: bigint; let outputSupply: bigint; - if (assetOut.id === pool.asset1ID) { - assetInID = pool.asset2ID; + if (assetOutID === pool.asset1ID) { inputSupply = pool.asset2Reserves!; outputSupply = pool.asset1Reserves!; - } else if (assetOut.id === pool.asset2ID) { - assetInID = pool.asset1ID; + } else if (assetOutID === pool.asset2ID) { inputSupply = pool.asset1Reserves!; outputSupply = pool.asset2Reserves!; } else { - throw new TinymanError( - {pool, assetOut}, - `Output asset (#${ - assetOut.id - }) doesn't belong to the pool ${pool.account.address()}.` + throw new SwapQuoteError( + SwapQuoteErrorType.AssetDoesNotBelongToPoolError, + `Output asset (#${assetOutID}) doesn't belong to the pool ${pool.account.address()}.` ); } @@ -390,52 +399,175 @@ async function getFixedOutputSwapQuote({ decimals }); - const directSwapQuote = { - assetInID, - assetInAmount: swapInputAmount, - assetOutID: assetOut.id, - assetOutAmount, - swapFee: Number(totalFeeAmount), - rate: - convertFromBaseUnits(decimals.assetOut, Number(assetOutAmount)) / - convertFromBaseUnits(decimals.assetIn, Number(swapInputAmount)), - priceImpact - }; - - if (isSwapRouterEnabled) { - const swapRoute = await getSwapRoute({ - amount: assetOut.amount, - assetInID, - assetOutID: assetOut.id, - swapType: SwapType.FixedOutput, - network - }); - - if ( - swapRoute.route.length > 1 && - BigInt(getAssetOutFromSwapRoute(swapRoute.route).amount) < - directSwapQuote.assetInAmount - ) { - return { - data: swapRoute, - type: SwapQuoteType.Router - }; - } - } - if (assetOutAmount > outputSupply) { - throw new OutputAmountExceedsAvailableLiquidityError(); + throw new SwapQuoteError( + SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError, + "Output amount exceeds available liquidity." + ); } return { + type: SwapQuoteType.Direct, data: { pool, - quote: directSwapQuote - }, - type: SwapQuoteType.Direct + quote: { + assetInID, + assetInAmount: swapInputAmount, + assetOutID, + assetOutAmount, + swapFee: Number(totalFeeAmount), + rate: + convertFromBaseUnits(decimals.assetOut, Number(assetOutAmount)) / + convertFromBaseUnits(decimals.assetIn, Number(swapInputAmount)), + priceImpact + } + } }; } +/** + * @returns A quote for a fixed input swap. Does NOT execute any transactions. + */ +async function getFixedInputSwapQuote({ + amount, + assetInID, + assetOutID, + decimals, + isSwapRouterEnabled, + network, + pool +}: { + amount: number | bigint; + assetInID: number; + assetOutID: number; + decimals: {assetIn: number; assetOut: number}; + network: SupportedNetwork; + pool: V2PoolInfo | null; + isSwapRouterEnabled?: boolean; +}): Promise { + const quotePromises: Promise[] = []; + + if (pool) { + quotePromises.push( + new Promise((resolve, reject) => { + try { + const quote = getFixedInputDirectSwapQuote({ + amount, + assetInID, + assetOutID, + decimals, + pool + }); + + resolve({ + type: SwapQuoteType.Direct, + data: { + pool, + quote + } + }); + } catch (error) { + reject(error); + } + }) + ); + } else { + quotePromises.push( + Promise.reject( + new SwapQuoteError( + SwapQuoteErrorType.NoAvailablePoolError, + "There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools." + ) + ) + ); + } + + if (isSwapRouterEnabled) { + quotePromises.push( + (async () => { + const data = await getSwapRoute({ + amount, + assetInID, + assetOutID, + swapType: SwapType.FixedInput, + network + }); + + return { + type: SwapQuoteType.Router, + data + }; + })() + ); + } + + const validQuotes = await validateQuotes(quotePromises); + + return getBestQuote(validQuotes); +} + +/** + * @returns A quote for a fixed output swap. Does NOT execute any transactions. + */ +async function getFixedOutputSwapQuote({ + amount, + assetInID, + assetOutID, + decimals, + isSwapRouterEnabled, + network, + pool +}: { + amount: number | bigint; + assetInID: number; + assetOutID: number; + pool: V2PoolInfo | null; + decimals: {assetIn: number; assetOut: number}; + network: SupportedNetwork; + isSwapRouterEnabled?: boolean; +}): Promise { + const quotePromises: Promise[] = [ + new Promise((resolve, reject) => { + try { + resolve( + getFixedOutputDirectSwapQuote({ + amount, + assetInID, + assetOutID, + decimals, + pool + }) + ); + } catch (error) { + reject(error); + } + }) + ]; + + if (isSwapRouterEnabled) { + quotePromises.push( + (async () => { + const data = await getSwapRoute({ + amount, + assetInID, + assetOutID, + swapType: SwapType.FixedOutput, + network + }); + + return { + type: SwapQuoteType.Router, + data + }; + })() + ); + } + + const validQuotes = await validateQuotes(quotePromises); + + return getBestQuote(validQuotes); +} + function calculateFixedInputSwap({ inputSupply, outputSupply, @@ -586,6 +718,8 @@ function calculateSwapAmountOfFixedOutputSwap({ export const SwapV2 = { getQuote, getFixedInputSwapQuote, + getFixedInputDirectSwapQuote, + getFixedOutputDirectSwapQuote, getFixedOutputSwapQuote, generateTxns, signTxns, diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index cfe05b5e..826b9c05 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -5,6 +5,8 @@ import {CONTRACT_VERSION} from "../../../contract/constants"; import {ALGO_ASSET_ID} from "../../../util/asset/assetConstants"; import {getAssetId, isAlgo} from "../../../util/asset/assetUtils"; import {SupportedNetwork} from "../../../util/commonTypes"; +import SwapQuoteError, {SwapQuoteErrorType} from "../../../util/error/SwapQuoteError"; +import {hasTinymanApiErrorShape} from "../../../util/util"; import {getValidatorAppID} from "../../../validator"; import {SwapType} from "../../constants"; import { @@ -181,23 +183,31 @@ export async function getSwapRoute({ amount: String(amount) }; - try { - const response = await fetch( - `${TINYMAN_ANALYTICS_API_BASE_URLS[network].v1}/swap-router/quotes/`, - { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(payload) - } - ); - - return response.json(); - // TODO: Handle all errors properly - } catch (error) { - console.error(error); - - throw error; + const response = await fetch( + `${TINYMAN_ANALYTICS_API_BASE_URLS[network].v1}/swap-router/quotes/`, + { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify(payload) + } + ).catch(() => { + throw new Error("Network error"); + }); + + const serializedResponse = await response.json(); + + if (!response.ok) { + if (hasTinymanApiErrorShape(serializedResponse)) { + throw new SwapQuoteError( + serializedResponse.type as SwapQuoteErrorType, + serializedResponse.fallback_message + ); + } else { + throw new SwapQuoteError(SwapQuoteErrorType.UnknownError, "Unknown error"); + } } + + return serializedResponse; } diff --git a/src/util/commonTypes.ts b/src/util/commonTypes.ts index d76cc5fe..d5e12eee 100644 --- a/src/util/commonTypes.ts +++ b/src/util/commonTypes.ts @@ -15,3 +15,13 @@ export type InitiatorSigner = ( ) => Promise; export type SupportedNetwork = "testnet" | "mainnet"; + +export interface TinymanApiErrorDetailShape { + [x: string]: undefined | any; +} + +export interface TinymanApiErrorShape { + type: Type; + detail: TinymanApiErrorDetailShape; + fallback_message: string; +} diff --git a/src/util/error/OutputAmountExceedsAvailableLiquidityError.ts b/src/util/error/OutputAmountExceedsAvailableLiquidityError.ts deleted file mode 100644 index a04aefe3..00000000 --- a/src/util/error/OutputAmountExceedsAvailableLiquidityError.ts +++ /dev/null @@ -1,7 +0,0 @@ -class OutputAmountExceedsAvailableLiquidityError extends Error { - constructor(message = "Output amount exceeds available liquidity") { - super(message); - } -} - -export default OutputAmountExceedsAvailableLiquidityError; diff --git a/src/util/error/SwapQuoteError.ts b/src/util/error/SwapQuoteError.ts new file mode 100644 index 00000000..c6883a1f --- /dev/null +++ b/src/util/error/SwapQuoteError.ts @@ -0,0 +1,31 @@ +export enum SwapQuoteErrorType { + SwapRouterStaleDataError = "SwapRouterStaleDataError", + SwapRouterNoRouteError = "SwapRouterNoRouteError", + SwapRouterLowSwapAmountError = "SwapRouterLowSwapAmountError", + SwapRouterInsufficientReservesError = "SwapRouterInsufficientReservesError", + SwapRouterPoolHasNoLiquidityError = "SwapRouterPoolHasNoLiquidityError", + NoAvailablePoolError = "NoAvailablePoolError", + OutputAmountExceedsAvailableLiquidityError = "OutputAmountExceedsAvailableLiquidityError", + UnknownError = "UnknownError", + LowSwapAmountError = "LowSwapAmountError", + AssetDoesNotBelongToPoolError = "AssetDoesNotBelongToPoolError", + InvalidSwapTypeError = "InvalidSwapTypeError" +} + +class SwapQuoteError extends Error { + type: SwapQuoteErrorType; + message: string; + + constructor(type: SwapQuoteErrorType, message: string) { + super(message); + + this.type = type; + this.message = message; + + if (Error.captureStackTrace) { + Error.captureStackTrace(this, SwapQuoteError); + } + } +} + +export default SwapQuoteError; diff --git a/src/util/util.ts b/src/util/util.ts index 65c4c0a2..9f4837b0 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -1,6 +1,6 @@ import {Algodv2} from "algosdk"; -import {SignerTransaction} from "./commonTypes"; +import {SignerTransaction, TinymanApiErrorShape} from "./commonTypes"; import {AccountInformation} from "./account/accountTypes"; import TinymanError from "./error/TinymanError"; @@ -331,3 +331,7 @@ export function encodeInteger(number) { export function encodeString(text: string) { return new TextEncoder().encode(text); } + +export function hasTinymanApiErrorShape(error: any): error is TinymanApiErrorShape { + return Boolean(error) && typeof error.fallback_message !== "undefined"; +} From f3124cb01ab537c4eefabc8e3f71329e4c222b3d Mon Sep 17 00:00:00 2001 From: anlerkan Date: Mon, 20 Mar 2023 15:09:39 +0300 Subject: [PATCH 23/39] feat(swap): add swap amount low error, throw error if the route lenght is lower than 2 --- dist/index.js | 4 ++-- dist/swap/constants.d.ts | 1 + dist/swap/v2/util.d.ts | 8 ++++++++ src/swap/constants.ts | 2 ++ src/swap/v2/index.ts | 22 ++++++++++++++++++++++ src/swap/v2/util.ts | 19 ++++++++++++++++++- 6 files changed, 53 insertions(+), 3 deletions(-) diff --git a/dist/index.js b/dist/index.js index ee0b1a37..19e43257 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r,a=n(t);function s(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),i=n.call(s,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),w(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;w(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:E(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,s,o){try{var u=t[s](o),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var s=t.apply(e,n);function o(t){i(s,r,a,o,u,"next",t)}function u(t){i(s,r,a,o,u,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw s}}}}exports.SwapQuoteErrorType=void 0,(r=exports.SwapQuoteErrorType||(exports.SwapQuoteErrorType={})).SwapRouterStaleDataError="SwapRouterStaleDataError",r.SwapRouterNoRouteError="SwapRouterNoRouteError",r.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",r.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",r.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",r.NoAvailablePoolError="NoAvailablePoolError",r.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",r.UnknownError="UnknownError",r.LowSwapAmountError="LowSwapAmountError",r.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",r.InvalidSwapTypeError="InvalidSwapTypeError";var D=function(t){m(n,h(Error));var e=v(n);function n(t,r){var a;return p(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(T(a),n),a}return d(n)}(),O=Uint8Array.from([1]),N={V1_1:"v1_1",V2:"v2"},P={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},k={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},L="- would result negative",R="logic eval error:",F="exceeds schema integer count",B=/transaction \w+:/,M=function(t){m(n,h(Error));var e=v(n);function n(t,r){var a;p(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ur?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function C(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function j(t){return 0===Number(t)}function X(t){return Number(t.id)}function q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},u=b(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}o[s?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return o}function U(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=b(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var G=100000n,W=100000n,z=100000n,J=25000n+25000n,Y=25000n+3500n;function K(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return G+W*BigInt(o.length)+z*BigInt(s.length+a.length)+Y*r+J*n}function H(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,H(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function tt(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function et(t,e){var n=Number(t);return rt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function nt(t,e){return rt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function rt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=w(st(e),2),s=a[0],o=a[1],u=w(st(Math.round(Number(at(s,o+r)))),2),i=u[0],c=u[1];return Number(at(i,c-r))}return Math.round(e)}function at(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function st(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function ot(t,e){return ut.apply(this,arguments)}function ut(){return(ut=c(u().mark((function t(e,n){var r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=b(n),t.prev=3,a.s();case 5:if((s=a.n()).done){t.next=18;break}return o=s.value,t.next=9,e.sendRawTransaction(o).do();case 9:return i=t.sent,c=i.txId,t.next=13,Z(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new M(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function it(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ct(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function pt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function lt(t){return(new TextEncoder).encode(t)}function dt(t){return Boolean(t)&&void 0!==t.fallback_message}var ft,mt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return lt("tinyman/".concat(t===N.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),gt=(f(ft={},N.V1_1,{testnet:62368684,mainnet:552635992}),f(ft,N.V2,{testnet:148607e3,mainnet:1002541853}),ft);function It(t,e){var n=gt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function xt(){return(xt=c(u().mark((function t(e){var n,r,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationOptInTxnFromObject({from:o,appIndex:It(r,s),note:mt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var At,ht,Tt,yt;function vt(){return(vt=c(u().mark((function t(e){var n,r,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:It(r,s),note:mt.getAppCallTxnNoteWithClientName(s),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(At=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",At.FixedOutput="fixed-output",function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(yt||(yt={}));var wt,_t,Et=(f(ht={},exports.SwapType.FixedInput,1),f(ht,exports.SwapType.FixedOutput,2),ht),St=lt("swap"),bt=(f(Tt={},exports.SwapType.FixedInput,lt("fixed-input")),f(Tt,exports.SwapType.FixedOutput,lt("fixed-output")),Tt),Dt={ASSET_OPT_IN:lt("asset_opt_in")},Ot={testnet:159521633,mainnet:0},Nt=(f(wt={},exports.SwapType.FixedInput,7),f(wt,exports.SwapType.FixedOutput,8),wt),Pt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function kt(t){var e=Bt(t),n=e.assetIn,r=e.assetOut;return et(r.asset.decimals,Number(r.amount))/et(n.asset.decimals,Number(n.amount))}function Lt(t){if(!Ot[t])throw new Error("Unknown network or network not supported: ".concat(t));return Ot[t]}function Rt(t){return t[t.length-1].quote.amount_out}function Ft(t){return t[0].quote.amount_in}function Bt(t){return{assetIn:Ft(t),assetOut:Rt(t)}}function Mt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function e(n){var r,s,o,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.accountAddress,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Dt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:c}),l=o.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,s,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w,_,E,S,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,c=n.route,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[X(c[0].quote.amount_in.asset),X(c[0].quote.amount_out.asset),X(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Ft(c).amount),Number(Rt(c).amount)],I=g[0],x=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=j(d),v=Lt(o),w=y?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(v),amount:I,suggestedParams:p}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(v),amount:I,assetIndex:d,suggestedParams:p}),(_=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:v,appArgs:[St,bt[i],a.default.encodeUint64(x)],foreignApps:[It(o,N.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Nt[i]+1),E=[w,_],e.next=15,jt({client:s,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((S=e.sent).length>0)){e.next=21;break}return e.next=19,Mt({client:s,accountAddress:r,assetIDs:S,routerAppID:v});case 19:b=e.sent,E.unshift(b);case 21:return D=a.default.assignGroupID(E),e.abrupt("return",D.map((function(t){return{txn:t,signers:[r]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(u().mark((function e(n){var r,a,s,o,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,s=n.assetIDs,o=t.getApplicationAddress(a),e.next=4,r.accountInformation(o).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=s.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qt(t){return Ut.apply(this,arguments)}function Ut(){return(Ut=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.swapType,o=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:s,amount:String(n)},t.next=4,fetch("".concat(Pt[o].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(!dt(p)){t.next=13;break}throw new D(p.type,p.fallback_message);case 13:throw new D(exports.SwapQuoteErrorType.UnknownError,"Unknown error");case 14:return t.abrupt("return",p);case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Gt(t){var e=t.assetIn,n=t.assetOut;return et(n.decimals,Number(n.amount))/et(e.decimals,Number(e.amount))}function Wt(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=Gt({assetIn:r,assetOut:a}),o=et(a.decimals,Number(n))/et(r.decimals,Number(e));return rt({decimalPlaces:5},Math.abs(s/o-1))}function zt(t){if(t.type===exports.SwapQuoteType.Router){var e=Ft(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function Jt(t){if(t.type===exports.SwapQuoteType.Router){var e=Rt(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function Yt(t){return t.type===exports.SwapQuoteType.Direct?t.data.pool.contractVersion:N.V2}exports.SwapQuoteType=void 0,(_t=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",_t.Router="router";var Kt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Ht={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Zt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),$t=new(function(n){m(a,Zt);var r=v(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=It(n.network,N.V1_1),a=w(C(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,s){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(o(o({},i),{},{minimum_required_balance:oe(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),s(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function se(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function oe(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ue,ie,ce=lt("e");function pe(t){return le.apply(this,arguments)}function le(){return(le=c(u().mark((function n(r){var s,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w,_,E,S;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=b(p),n.prev=10,g.s();case 12:if((I=g.n()).done){n.next=31;break}if((x=I.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=x["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=q({stateArray:A}),T=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(o.asset1ID)])),y=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(o.asset2ID)])),v=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(o.poolTokenID)])),w=h[T],_=h[y],E=h[v],"bigint"==typeof w&&(l=w),"bigint"==typeof _&&(d=_),"bigint"==typeof E&&(f=E);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((S={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||S.excessAsset2<0n||S.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(S));case 42:return n.abrupt("return",S);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function de(){return(de=c(u().mark((function n(r){var s,o,i,c,p,l,d,f,m,g,I,x,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mge)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ge,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function he(){return he=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(s=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",s[r]);case 4:return t.next=6,ae(n,r);case 6:return o=t.sent,i=se(o,It(a,N.V1_1)),c=null,i&&(l=o["created-assets"][0],p=l.index,c={asset1ID:i[fe[N.V1_1].asset1],asset2ID:i[fe[N.V1_1].asset2],poolTokenID:p},s[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),he.apply(this,arguments)}var Te=Object.freeze({__proto__:null,getPoolInfo:Ie,getPoolReserves:function(t,e){return Ae.apply(this,arguments)},getPoolAssets:function(t){return he.apply(this,arguments)}});function ye(t){return ve.apply(this,arguments)}function ve(){return(ve=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,s=e.asset2ID,o=re(N.V2),i=o.generateLogicSigAccountForPool(e),c=It(r,N.V2),p=i.address(),l=C(a,s),t.next=8,ae(n,p);case 8:return d=t.sent,f=se(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:N.V2},f&&(m.asset1ProtocolFees=BigInt(f[fe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[fe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[fe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[fe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[fe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[fe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[fe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[fe.v2.totalFeeShare]),m.poolTokenID=Number(f[fe.v2.poolTokenID]),m.asset1ID=Number(f[fe.v2.asset1]),m.asset2ID=Number(f[fe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function we(){return(we=c(u().mark((function t(e,n){var r,a,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ae(e,n.account.address());case 2:return r=t.sent,a=se(r,n.validatorAppID),s={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(s.asset1=BigInt(a[fe.v2.asset1Reserves]),s.asset2=BigInt(a[fe.v2.asset2Reserves]),s.issuedLiquidity=BigInt(a[fe.v2.issuedPoolTokens])),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(){return(_e=c(u().mark((function t(e){var n,r,a,s,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,ae(n,r);case 3:return s=t.sent,o=se(s,It(a,N.V2)),i=null,o&&(i={asset1ID:o[fe[N.V2].asset1],asset2ID:o[fe[N.V2].asset2],poolTokenID:o[fe[N.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ee=Object.freeze({__proto__:null,getPoolInfo:ye,getPoolReserves:function(t,e){return we.apply(this,arguments)},getPoolAssets:function(t){return _e.apply(this,arguments)}});function Se(t){return Boolean(t&&!(t.asset1+t.asset2))}var be,De,Oe=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Se(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=et(t.asset1,e.asset1)/et(t.asset2,e.asset2)),r},isPoolEmpty:Se,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Ie(t),ye(t)])}}),Ne=o((f(be={},N.V1_1,o(o({},Te),Oe)),f(be,N.V2,o(o({},Ee),Oe)),be),Oe);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(De||(De={}));var Pe=96e4,ke=859e3;function Le(){return(Le=c(u().mark((function t(e){var n,r,s,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,_,E,S,b,D,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset_1,o=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=V(s,o),l=w(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],I=g.id,x=g.unit_name,A=j(I),h=It(r,N.V1_1),T=$t.generateLogicSigAccountForPool({network:r,asset1ID:f,asset2ID:I}),y=T.address(),v=a.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:mt.getAppCallTxnNoteWithClientName(N.V1_1),appArgs:[lt("bootstrap"),a.default.encodeUint64(f),a.default.encodeUint64(I)],foreignAssets:A?[f]:[I],suggestedParams:c}),_=a.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:k.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),E=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),S=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Re(A),suggestedParams:c}),(b=[])[De.FUNDING_TXN]=S,b[De.VALIDATOR_APP_CALL]=v,b[De.POOL_TOKEN_CREATE]=_,b[De.ASSET1_OPT_IN]=E,A||(b[De.ASSET2_OPT_IN]=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:I,amount:0,suggestedParams:c})),D=a.default.assignGroupID(b),O=[{txn:D[De.FUNDING_TXN],signers:[i]},{txn:D[De.VALIDATOR_APP_CALL],signers:[y]},{txn:D[De.POOL_TOKEN_CREATE],signers:[y]},{txn:D[De.ASSET1_OPT_IN],signers:[y]}],D[De.ASSET2_OPT_IN]&&O.push({txn:D[De.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Re(t){return t?Pe:ke}function Fe(){return(Fe=c(u().mark((function t(e){var n,r,s,o,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=C(o,i),f=w(d,2),m=f[0],g=f[1],I=$t.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===De.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,s=n.blob;return x.push(r),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(t){return Me.apply(this,arguments)}function Me(){return(Me=c(u().mark((function t(e){var n,r,a,s,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Z(n,a[De.POOL_TOKEN_CREATE]);case 6:if(s=t.sent,"number"==typeof(o=s["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Qe(){return(Qe=c(u().mark((function t(e){var n,r,a,s,o,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Be({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",Ne.v1_1.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ve,Ce={generateTxns:function(t){return Le.apply(this,arguments)},signTxns:function(t){return Fe.apply(this,arguments)},execute:function(t){return Qe.apply(this,arguments)},getBootstrapFundingTxnAmount:Re};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ve||(Ve={}));var je=5,Xe=6;function qe(t,e){return Ue.apply(this,arguments)}function Ue(){return(Ue=c(u().mark((function e(n,r){var a,s,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(s=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,s,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ge(t,e){return We.apply(this,arguments)}function We(){return(We=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qe(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ze(){return(ze=c(u().mark((function e(n){var r,s,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,_,E;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return p=e.sent,l=It(s,N.V2),d=t.getApplicationAddress(l),f=V(o,i),m=w(f,2),g=m[0].id,I=m[1].id,e.next=9,Ne.v2.getPoolInfo({client:r,network:s,asset1ID:g,asset2ID:I});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return x=ne.generateLogicSigAccountForPool({network:s,asset1ID:g,asset2ID:I}),A=x.address(),h=j(I),(T=a.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[lt("bootstrap")],note:mt.getAppCallTxnNoteWithClientName(N.V2),foreignAssets:[g,I],rekeyTo:d,suggestedParams:p})).fee=Ye(h),y=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Je(h),suggestedParams:p}),(v=[])[Ve.FUNDING_TXN]=y,v[Ve.VALIDATOR_APP_CALL]=T,_=a.default.assignGroupID(v),(E=[])[Ve.FUNDING_TXN]={txn:_[Ve.FUNDING_TXN],signers:[c]},E[Ve.VALIDATOR_APP_CALL]={txn:_[Ve.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",E);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Je(t){return function(t,e){var n=re(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(N.V2,t)+Ye(t)+1e5}function Ye(e){return((e?je:Xe)+1)*t.ALGORAND_MIN_TX_FEE}function Ke(){return(Ke=c(u().mark((function t(e){var n,r,s,o,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,s=e.initiatorSigner,o=e.asset1ID,i=e.asset2ID,t.next=3,s([n]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=C(o,i),f=w(d,2),m=f[0],g=f[1],I=ne.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===Ve.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,s=n.blob;return x.push(r),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function He(){return(He=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,s=a.asset1ID,o=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,qe(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(fe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Ne.v2.getPoolInfo({client:n,network:r,asset1ID:s,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new M(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Ze,$e={generateTxns:function(t){return ze.apply(this,arguments)},signTxns:function(t){return Ke.apply(this,arguments)},execute:function(t){return He.apply(this,arguments)},getBootstrapFundingTxnAmount:Je,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Je(e)}};var tn,en=(f(Ze={},N.V1_1,Ce),f(Ze,N.V2,$e),f(Ze,"generateTxns",(function(t){return t.contractVersion===N.V1_1?Ce.generateTxns(t):$e.generateTxns(t)})),f(Ze,"signTxns",(function(t){return t.contractVersion===N.V1_1?Ce.signTxns(t):$e.signTxns(t)})),f(Ze,"execute",(function(t){return t.contractVersion===N.V1_1?Ce.execute(t):$e.execute(t)})),f(Ze,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===N.V1_1?Ce.getBootstrapFundingTxnAmount(n):$e.getBootstrapFundingTxnAmount(n)})),Ze);exports.V1_1AddLiquidityTxnIndices=void 0,(tn=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[tn.FEE_TXN=0]="FEE_TXN",tn[tn.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",tn[tn.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",tn[tn.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",tn[tn.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var nn,rn,an,sn,on=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(sn=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",sn.FLEXIBLE="flexible",sn.INITIAL="initial";var un,cn=(f(nn={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(nn,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(nn,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),nn),pn=(f(rn={},exports.V2AddLiquidityType.INITIAL,1),f(rn,exports.V2AddLiquidityType.SINGLE,2),f(rn,exports.V2AddLiquidityType.FLEXIBLE,2),rn),ln=(f(an={},exports.V2AddLiquidityType.INITIAL,3),f(an,exports.V2AddLiquidityType.FLEXIBLE,3),f(an,exports.V2AddLiquidityType.SINGLE,2),an);function dn(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,I=BigInt(o.amount)-m,x=BigInt(u.amount)-g;if(I>x){var A=I;r=fn(A,s),e={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=x;r=fn(h,s),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Wt({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function fn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function mn(e){return(pn[e]+1)*t.ALGORAND_MIN_TX_FEE}var gn=lt("add_liquidity"),In=(f(un={},N.V1_1,[lt("mint")]),f(un,N.V2,{INITIAL_LIQUIDITY:[lt("add_initial_liquidity")],SINGLE_ASSET_MODE:[gn,lt("single")],FLEXIBLE_MODE:[gn,lt("flexible")]}),un);function xn(){return(xn=c(u().mark((function t(e){var n,r,s,o,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.poolAddress,o=e.asset1In,i=e.asset2In,c=e.poolTokenOut,p=e.slippage,l=e.initiatorAddr,d=tt("negative",p,c.amount),t.next=4,n.getTransactionParams().do();case 4:return f=t.sent,m=a.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:It(r,N.V1_1),appArgs:In.v1_1,accounts:[l],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==i.id?[o.id,c.id]:[o.id,i.id,c.id],suggestedParams:f}),g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:s,assetIndex:o.id,amount:o.amount,suggestedParams:f}),I=0===i.id?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:s,amount:i.amount,suggestedParams:f}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:s,assetIndex:i.id,amount:i.amount,suggestedParams:f}),x=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:s,amount:m.fee+x.fee,note:O,suggestedParams:f}),h=a.default.assignGroupID([A,m,g,I,x]),t.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[s]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[s]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function An(){return(An=c(u().mark((function t(e){var n,r,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,s=e.initiatorSigner,o=n.account,t.next=4,s([r]);case 4:return i=t.sent,c=w(i,3),p=c[0],l=c[1],d=c[2],f=r.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:a.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hn(){return(hn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.signedTxns,o=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,pe({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,ot(n,[s]);case 8:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=it(a),I=ct(a),t.next=17,pe({client:n,pool:r,accountAddr:o});case 17:return x=t.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:I});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new M(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Tn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=oi)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:s,assetOutAmount:l,swapFee:Number(p),rate:Gt(d),priceImpact:Wt(o({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function zn(t){return Jn.apply(this,arguments)}function Jn(){return(Jn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,ot(n,[a]);case 6:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+A,excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yn(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap on a non-existent pool");var s,o,u,i=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,u=n.asset2}if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*u/(u-i)-o,p=c*qn/(qn-Xn),l=p-c,d=et(a.assetOut,Number(i))/et(a.assetIn,Number(p)),f=et(a.assetOut,Number(u))/et(a.assetIn,Number(o)),m=rt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function Kn(t){return Hn.apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,s=e.assetIn,o=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,ot(n,[a]);case 6:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:s.id,assetInAmount:BigInt(s.amount)-A,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,r,a,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,s=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new M({pool:r,swapType:a,txGroup:s},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:s[Cn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:s[Cn.ASSET_IN_TXN_INDEX].txn.amount},l={id:s[Cn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:s[Cn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,zn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Kn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:ct(s),fees:it(s)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new M(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Cn||(Cn={}));var $n={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Wn({pool:e,reserves:n,assetIn:r,decimals:a}):Yn({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Wn,getFixedOutputSwapQuote:Yn,generateTxns:function(t){return Gn.apply(this,arguments)},signTxns:function(t){return Un.apply(this,arguments)},execute:function(t){return Zn.apply(this,arguments)},executeFixedOutputSwap:Kn},tr=4*t.ALGORAND_MIN_TX_FEE;function er(t){return t instanceof D&&[exports.SwapQuoteErrorType.SwapRouterInsufficientReservesError,exports.SwapQuoteErrorType.SwapRouterLowSwapAmountError,exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,exports.SwapQuoteErrorType.LowSwapAmountError].includes(t.type)}function nr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=w(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(er(n)&&!er(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function rr(){return(rr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,a=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.pools,c=[],(p=i.find((function(t){return t.info.contractVersion===N.V1_1})))?(l={pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:s.decimals},reserves:p.reserves},c.push(new Promise((function(t,e){try{t($n.getFixedInputSwapQuote(l))}catch(t){e(t)}})))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap from non-existent pool"))),d=i.find((function(t){return t.info.contractVersion===N.V2})),c.push(hr.getFixedInputSwapQuote({amount:r,assetInID:X(a),assetOutID:X(s),pool:null!==(n=null==d?void 0:d.info)&&void 0!==n?n:null,decimals:{assetIn:a.decimals,assetOut:s.decimals},isSwapRouterEnabled:o,network:e.network})),t.next=8,nr(c);case 8:return f=t.sent,t.abrupt("return",or(f));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(){return(ar=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,a=e.assetIn,s=e.assetOut,o=e.pools,i=e.isSwapRouterEnabled,c=[],(p=o.find((function(t){return t.info.contractVersion===N.V1_1})))?(l={pool:p.info,assetOut:{amount:r,id:Number(s.id)},decimals:{assetIn:a.decimals,assetOut:s.decimals},reserves:p.reserves},c.push(new Promise((function(t,e){try{t($n.getFixedOutputSwapQuote(l))}catch(t){e(t)}})))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap from non-existent pool"))),d=o.find((function(t){return t.info.contractVersion===N.V2})),c.push(hr.getFixedOutputSwapQuote({amount:r,assetInID:X(a),assetOutID:X(s),pool:null!==(n=null==d?void 0:d.info)&&void 0!==n?n:null,decimals:{assetIn:a.decimals,assetOut:s.decimals},isSwapRouterEnabled:i,network:e.network})),t.next=8,nr(c);case 8:return f=t.sent,t.abrupt("return",or(f));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sr(t){return t.type===exports.SwapQuoteType.Direct?t.data.quote.rate:kt(t.data.route)}function or(t){for(var e=t[0],n=sr(e),r=1;rn&&(e=a,n=s)}return e}function ur(){return(ur=c(u().mark((function t(e){var n,r,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Vt(o(o({},e),{},{route:e.quote.data.route})));case 2:if(n=e.client,r=e.initiatorAddr,s=e.slippage,i=e.swapType,c=e.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(I=[l.asset1ID,l.asset2ID]).includes(f)&&I.includes(m)&&f!==m){t.next=9;break}throw new M({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return x=t.sent,A=j(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:tt("positive",s,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:tt("negative",s,d.assetOutAmount),y=A?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,suggestedParams:x}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,assetIndex:f,suggestedParams:x}),(v=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:l.validatorAppID,appArgs:[St,bt[i],a.default.encodeUint64(T)],note:mt.getAppCallTxnNoteWithClientName(N.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=ir(i),(w=[])[yt.INPUT_TXN]=y,w[yt.APP_CALL_TXN]=v,_=a.default.assignGroupID(w),t.abrupt("return",[{txn:_[yt.INPUT_TXN],signers:[r]},{txn:_[yt.APP_CALL_TXN],signers:[r]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ir(e){return(Et[e]+1)*t.ALGORAND_MIN_TX_FEE}function cr(){return(cr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,s=e.quote,o=e.txGroup,i=e.signedTxns,t.next=3,ot(a,[i]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Ge(a,o);case 10:return m=t.sent,g=zt(s),I=Jt(s).id,x=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===I})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:x&&{amount:BigInt(g.amount)-BigInt(x.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:I},quote:s,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pr(){return(pr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.amount,a=e.assetInID,s=e.assetOutID,o=e.decimals,i=e.network,c=e.isSwapRouterEnabled,p=e.pool,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,mr({assetInID:a,assetOutID:s,amount:r,decimals:o,isSwapRouterEnabled:c,network:i,pool:p});case 4:l=t.sent,t.next=10;break;case 7:return t.next=9,Ir({amount:r,assetInID:a,assetOutID:s,decimals:o,isSwapRouterEnabled:c,network:i,pool:p});case 9:l=t.sent;case 10:return t.abrupt("return",l);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function lr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=t[0].reason;if(2===t.length){var n=t[1].reason;if(er(e)&&!er(n))throw e;throw n}throw e}return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function dr(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,s=t.pool;if(!Ne.isPoolReady(s))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");var o,u,i=BigInt(e),c=s.totalFeeShare;if(n===s.asset1ID)o=s.asset1Reserves,u=s.asset2Reserves;else{if(n!==s.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Input asset (#".concat(n,") doesn't belong to the pool ").concat(s.account.address(),"."));o=s.asset2Reserves,u=s.asset1Reserves}var p=Ar({inputSupply:o,outputSupply:u,swapInputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapOutputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(l>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");return{assetInID:n,assetInAmount:i,assetOutID:r,assetOutAmount:l,swapFee:Number(d),rate:et(a.assetOut,Number(l))/et(a.assetIn,Number(i)),priceImpact:f}}function fr(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,s=t.pool;if(!s||!Ne.isPoolReady(s))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");var o,u,i=BigInt(e),c=s.totalFeeShare;if(r===s.asset1ID)o=s.asset2Reserves,u=s.asset1Reserves;else{if(r!==s.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r,") doesn't belong to the pool ").concat(s.account.address(),"."));o=s.asset1Reserves,u=s.asset2Reserves}var p=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:o,totalFeeShare:a}),i=o+u,c=Wt({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:i,totalFeeAmount:u,priceImpact:c}}({inputSupply:o,outputSupply:u,swapOutputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapInputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");return{type:exports.SwapQuoteType.Direct,data:{pool:s,quote:{assetInID:n,assetInAmount:l,assetOutID:r,assetOutAmount:i,swapFee:Number(d),rate:et(a.assetOut,Number(i))/et(a.assetIn,Number(l)),priceImpact:f}}}}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=c(u().mark((function t(e){var n,r,a,s,o,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.decimals,o=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[],p?l.push(new Promise((function(t,e){try{var o=dr({amount:n,assetInID:r,assetOutID:a,decimals:s,pool:p});t({type:exports.SwapQuoteType.Direct,data:{pool:p,quote:o}})}catch(t){e(t)}}))):l.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools."))),o&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedInput,network:i});case 2:return e=t.sent,t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 4:case"end":return t.stop()}}),t)})))()),t.next=6,lr(l);case 6:return d=t.sent,t.abrupt("return",or(d));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ir(t){return xr.apply(this,arguments)}function xr(){return(xr=c(u().mark((function t(e){var n,r,a,s,o,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,s=e.decimals,o=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[new Promise((function(t,e){try{t(fr({amount:n,assetInID:r,assetOutID:a,decimals:s,pool:p}))}catch(t){e(t)}}))],o&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedOutput,network:i});case 2:return e=t.sent,t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 4:case"end":return t.stop()}}),t)})))()),t.next=5,lr(l);case 5:return d=t.sent,t.abrupt("return",or(d));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:u,totalFeeAmount:o,priceImpact:Wt({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:u,decimals:s.assetOut}})}}var hr={getQuote:function(t){return pr.apply(this,arguments)},getFixedInputSwapQuote:mr,getFixedInputDirectSwapQuote:dr,getFixedOutputDirectSwapQuote:fr,getFixedOutputSwapQuote:Ir,generateTxns:function(t){return ur.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return cr.apply(this,arguments)},calculateFixedInputSwap:Ar};function Tr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(1e3)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function yr(){return(yr=c(u().mark((function e(n){var r,s,o,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=s.account.address(),m=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:o,suggestedParams:d}),(I=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(tt("negative",l,c)),a.default.encodeUint64(tt("negative",l,p))],accounts:[f],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(x=[])[Rn.ASSET_TRANSFER_TXN]=g,x[Rn.APP_CALL_TXN]=I,A=a.default.assignGroupID(x),e.abrupt("return",[{txn:A[Rn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[Rn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=c(u().mark((function e(n){var r,s,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=s.asset1ID,m=s.asset2ID,g=s.account.address(),I=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(x=0,A=0,h=tt("negative",l,p),c!==f){e.next=17;break}x=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}x=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:I,amount:i,suggestedParams:d}),(y=a.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(x),a.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[Rn.ASSET_TRANSFER_TXN]=T,v[Rn.APP_CALL_TXN]=y,w=a.default.assignGroupID(v),e.abrupt("return",[{txn:w[Rn.ASSET_TRANSFER_TXN],signers:[o]},{txn:w[Rn.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function wr(){return(wr=c(u().mark((function t(e){var n,r,a,s,o,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,s=e.signedTxns,t.next=3,ot(r,[s]);case 3:return o=t.sent,i=w(o,1),c=i[0].txnID,t.next=8,Ge(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var _r,Er,Sr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=Tr(a,n),o=s.asset1OutputAmount,u=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,u=BigInt(a),i=Tr(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=hr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var I=hr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=I.swapOutputAmount,A=I.totalFeeAmount,h=I.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return yr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return wr.apply(this,arguments)}},br=(f(_r={},N.V1_1,jn),f(_r,N.V2,Sr),_r),Dr=(f(Er={},N.V1_1,$n),f(Er,N.V2,hr),f(Er,"getQuote",(function(t){var e=t.type,n=t.pools;if(!t.isSwapRouterEnabled&&n.every((function(t){return Se(t.reserves)})))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");if(e===exports.SwapType.FixedInput)return function(t){return rr.apply(this,arguments)}(t);if(e===exports.SwapType.FixedOutput)return function(t){return ar.apply(this,arguments)}(t);throw new D(exports.SwapQuoteErrorType.InvalidSwapTypeError,"Invalid swap type")})),f(Er,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1?$n.generateTxns(o(o({},t),{},{quote:t.quote.data})):hr.generateTxns(t)})),f(Er,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1){var e=t.quote.data.pool;return $n.signTxns(o({pool:e},t))}return hr.signTxns(t)})),f(Er,"execute",(function(t){return t.contractVersion===N.V1_1?$n.execute(t):hr.execute(t)})),Er);function Or(){return(Or=c(u().mark((function t(e){var n,r,a,s,o,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,s=e.initiatorSigner,t.prev=1,t.next=4,Nr({txGroup:a,pool:r,initiatorSigner:s});case 4:return o=t.sent,t.next=7,ot(n,[o]);case 7:return i=t.sent,c=w(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:it(a),confirmedRound:d,txnID:l,groupID:ct(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function Nr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=c(u().mark((function t(e){var n,r,s,o,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.pool,s=e.initiatorSigner,t.next=3,s([n]);case 3:return o=t.sent,i=w(o,1),c=i[0],p=r.account.lsig,l=n.map((function(t,e){return 0===e?c:a.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function kr(){return kr=c(u().mark((function t(e){var n,r,s,o,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.data,s=e.initiatorSigner,t.prev=1,o=r.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:it(e),groupID:ct(e),lsig:n.account.lsig}})),t.next=5,s(o.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var r=c(u().mark((function r(s,o){var c,p,l,d,f,m;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,c=t.txns.map((function(n,r){return 0===r?i[e]:a.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),r.next=4,ot(n,[c]);case 4:p=r.sent,l=w(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,s({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),r.next=15;break;case 12:r.prev=12,r.t0=r.catch(0),o(r.t0);case 15:case"end":return r.stop()}}),r,null,[[0,12]])})));return function(t,e){return r.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),kr.apply(this,arguments)}function Lr(){return(Lr=c(u().mark((function t(e){var n,r,s,o,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.assetID,o=e.assetOut,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=r.account.address(),l=a.default.makeApplicationNoOpTxnFromObject({from:p,appIndex:r.validatorAppID,appArgs:[lt("redeem")],note:mt.getAppCallTxnNoteWithClientName(r.contractVersion),accounts:[i],foreignAssets:0==r.asset2ID?[r.asset1ID,r.poolTokenID]:[r.asset1ID,r.asset2ID,r.poolTokenID],suggestedParams:c}),d=0===s?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:i,amount:BigInt(o),suggestedParams:c}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:i,assetIndex:s,amount:BigInt(o),suggestedParams:c}),f=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:p,amount:l.fee+d.fee,note:O,suggestedParams:c}),m=a.default.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[i]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[lt("commit"),i],note:U([lt("tinymanStaking/v1:b"),c,t.encodeUint64(s),i])})}function Fr(){return(Fr=c(u().mark((function e(n){var r,a,s,o,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,s=n.program,o=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Rr({suggestedParams:l,stakingAppID:a,program:s,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[lt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=P,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Ln,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=en,exports.CONTRACT_VERSION=N,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=k,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=br,exports.Swap=Dr,exports.SwapQuoteError=D,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Fn,exports.V2AddLiquidityTxnIndices=cn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=tt,exports.calculateAccountMinimumRequiredBalance=oe,exports.calculatePriceImpact=Wt,exports.calculateSwapRate=Gt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=Ne,exports.prepareCommitTransactions=function(t){return Fr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return kr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Or.apply(this,arguments)},exports.sendAndWaitRawTransaction=ot,exports.sumUpTxnFees=it,exports.tinymanContract_v1_1=$t,exports.tinymanContract_v2=ne,exports.tinymanJSSDKConfig=mt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",o=r.asyncIterator||"@@asyncIterator",s=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,o=Object.create(a.prototype),s=new _(r||[]);return o._invoke=function(t,e,n){var r="suspendedStart";return function(a,o){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw o;return E()}for(n.method=a,n.arg=o;;){var s=n.delegate;if(s){var u=y(s,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,s),o}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var I=Object.getPrototypeOf,x=I&&I(I(S([])));x&&x!==e&&n.call(x,a)&&(g=x);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,o,s,u){var i=p(t[a],t,o);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,s,u)}),(function(t){r("throw",t,s,u)})):e.resolve(l).then((function(t){c.value=t,s(c)}),(function(t){return r("throw",t,s,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function o(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(o,o):o()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function w(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function _(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function S(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,o=function e(){for(;++r=0;--a){var o=this.tryEntries[a],s=o.completion;if("root"===o.tryLoc)return r("end");if(o.tryLoc<=this.prev){var u=n.call(o,"catchLoc"),i=n.call(o,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),w(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;w(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:S(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,o,s){try{var u=t[o](s),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function s(t){i(o,r,a,s,u,"next",t)}function u(t){i(o,r,a,s,u,"throw",t)}s(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,s=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){u=!0,o=t},f:function(){try{s||null==n.return||n.return()}finally{if(u)throw o}}}}exports.SwapQuoteErrorType=void 0,(r=exports.SwapQuoteErrorType||(exports.SwapQuoteErrorType={})).SwapRouterStaleDataError="SwapRouterStaleDataError",r.SwapRouterNoRouteError="SwapRouterNoRouteError",r.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",r.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",r.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",r.NoAvailablePoolError="NoAvailablePoolError",r.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",r.UnknownError="UnknownError",r.LowSwapAmountError="LowSwapAmountError",r.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",r.InvalidSwapTypeError="InvalidSwapTypeError";var D=function(t){m(n,h(Error));var e=v(n);function n(t,r){var a;return p(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(T(a),n),a}return d(n)}(),O=Uint8Array.from([1]),N={V1_1:"v1_1",V2:"v2"},P={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},k={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},L="- would result negative",R="logic eval error:",F="exceeds schema integer count",B=/transaction \w+:/,M=function(t){m(n,h(Error));var e=v(n);function n(t,r){var a;p(this,n);for(var o=arguments.length,s=new Array(o>2?o-2:0),u=2;ur?[s(s({},t),{},{id:n}),s(s({},e),{},{id:r})]:[s(s({},e),{},{id:r}),s(s({},t),{},{id:n})]}function C(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function j(t){return 0===Number(t)}function X(t){return Number(t.id)}function q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,o=void 0!==a&&a,s={},u=b(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}s[o?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return s}function U(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,o=b(t);try{for(o.s();!(e=o.n()).done;){var s=e.value;r.set(s,a),a+=s.length}}catch(t){o.e(t)}finally{o.f()}return r}var G=100000n,W=100000n,z=100000n,J=25000n+25000n,Y=25000n+3500n;function K(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],o=t["created-apps"]||[],s=t.assets||[];return G+W*BigInt(s.length)+z*BigInt(o.length+a.length)+Y*r+J*n}function H(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,H(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function tt(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function et(t,e){var n=Number(t);return rt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function nt(t,e){return rt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function rt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=w(ot(e),2),o=a[0],s=a[1],u=w(ot(Math.round(Number(at(o,s+r)))),2),i=u[0],c=u[1];return Number(at(i,c-r))}return Math.round(e)}function at(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ot(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function st(t,e){return ut.apply(this,arguments)}function ut(){return(ut=c(u().mark((function t(e,n){var r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=b(n),t.prev=3,a.s();case 5:if((o=a.n()).done){t.next=18;break}return s=o.value,t.next=9,e.sendRawTransaction(s).do();case 9:return i=t.sent,c=i.txId,t.next=13,Z(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new M(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function it(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ct(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function pt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function lt(t){return(new TextEncoder).encode(t)}function dt(t){return Boolean(t)&&void 0!==t.fallback_message}var ft,mt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return lt("tinyman/".concat(t===N.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),gt=(f(ft={},N.V1_1,{testnet:62368684,mainnet:552635992}),f(ft,N.V2,{testnet:148607e3,mainnet:1002541853}),ft);function It(t,e){var n=gt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function xt(){return(xt=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationOptInTxnFromObject({from:s,appIndex:It(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var At;function ht(){return(ht=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationClearStateTxnFromObject({from:s,appIndex:It(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(At=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",At.FixedOutput="fixed-output";var Tt,yt,vt;!function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(vt||(vt={}));var wt,_t,St=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),Et=lt("swap"),bt=(f(yt={},exports.SwapType.FixedInput,lt("fixed-input")),f(yt,exports.SwapType.FixedOutput,lt("fixed-output")),yt),Dt={ASSET_OPT_IN:lt("asset_opt_in")},Ot={testnet:159521633,mainnet:0},Nt=(f(wt={},exports.SwapType.FixedInput,7),f(wt,exports.SwapType.FixedOutput,8),wt),Pt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function kt(t){var e=Bt(t),n=e.assetIn,r=e.assetOut;return et(r.asset.decimals,Number(r.amount))/et(n.asset.decimals,Number(n.amount))}function Lt(t){if(!Ot[t])throw new Error("Unknown network or network not supported: ".concat(t));return Ot[t]}function Rt(t){return t[t.length-1].quote.amount_out}function Ft(t){return t[0].quote.amount_in}function Bt(t){return{assetIn:Ft(t),assetOut:Rt(t)}}function Mt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function e(n){var r,o,s,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.routerAppID,s=n.assetIDs,i=n.accountAddress,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o,appArgs:[Dt.ASSET_OPT_IN],foreignAssets:s,suggestedParams:c}),l=s.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w,_,S,E,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,o=n.client,s=n.network,i=n.swapType,c=n.route,e.next=3,o.getTransactionParams().do();case 3:return p=e.sent,l=[X(c[0].quote.amount_in.asset),X(c[0].quote.amount_out.asset),X(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Ft(c).amount),Number(Rt(c).amount)],I=g[0],x=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=j(d),v=Lt(s),w=y?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(v),amount:I,suggestedParams:p}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(v),amount:I,assetIndex:d,suggestedParams:p}),(_=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:v,appArgs:[Et,bt[i],a.default.encodeUint64(x)],foreignApps:[It(s,N.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Nt[i]+1),S=[w,_],e.next=15,jt({client:o,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((E=e.sent).length>0)){e.next=21;break}return e.next=19,Mt({client:o,accountAddress:r,assetIDs:E,routerAppID:v});case 19:b=e.sent,S.unshift(b);case 21:return D=a.default.assignGroupID(S),e.abrupt("return",D.map((function(t){return{txn:t,signers:[r]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(u().mark((function e(n){var r,a,o,s,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,o=n.assetIDs,s=t.getApplicationAddress(a),e.next=4,r.accountInformation(s).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=o.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qt(t){return Ut.apply(this,arguments)}function Ut(){return(Ut=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.swapType,s=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Pt[s].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(!dt(p)){t.next=13;break}throw new D(p.type,p.fallback_message);case 13:throw new D(exports.SwapQuoteErrorType.UnknownError,"Unknown error");case 14:return t.abrupt("return",p);case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Gt(t){var e=t.assetIn,n=t.assetOut;return et(n.decimals,Number(n.amount))/et(e.decimals,Number(e.amount))}function Wt(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,o=Gt({assetIn:r,assetOut:a}),s=et(a.decimals,Number(n))/et(r.decimals,Number(e));return rt({decimalPlaces:5},Math.abs(o/s-1))}function zt(t){if(t.type===exports.SwapQuoteType.Router){var e=Ft(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function Jt(t){if(t.type===exports.SwapQuoteType.Router){var e=Rt(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function Yt(t){return t.type===exports.SwapQuoteType.Direct?t.data.pool.contractVersion:N.V2}exports.SwapQuoteType=void 0,(_t=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",_t.Router="router";var Kt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Ht={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Zt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),$t=new(function(n){m(a,Zt);var r=v(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=It(n.network,N.V1_1),a=w(C(n.asset1ID,n.asset2ID),2),o=a[0],s=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:o,asset_id_2:s,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(s(s({},i),{},{minimum_required_balance:se(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),o(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function oe(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function se(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ue,ie,ce=lt("e");function pe(t){return le.apply(this,arguments)}function le(){return(le=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w,_,S,E;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.pool,i=r.accountAddr,n.next=3,o.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=s.account.address(),g=b(p),n.prev=10,g.s();case 12:if((I=g.n()).done){n.next=31;break}if((x=I.value).id==s.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=x["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=q({stateArray:A}),T=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.asset1ID)])),y=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.asset2ID)])),v=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.poolTokenID)])),w=h[T],_=h[y],S=h[v],"bigint"==typeof w&&(l=w),"bigint"==typeof _&&(d=_),"bigint"==typeof S&&(f=S);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((E={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function de(){return(de=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,I,x,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.accountAddr,i=r.validatorAppID,n.next=3,o.accountInformation(s).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mge)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ge,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function he(){return he=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ae(n,r);case 6:return s=t.sent,i=oe(s,It(a,N.V1_1)),c=null,i&&(l=s["created-assets"][0],p=l.index,c={asset1ID:i[fe[N.V1_1].asset1],asset2ID:i[fe[N.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),he.apply(this,arguments)}var Te=Object.freeze({__proto__:null,getPoolInfo:Ie,getPoolReserves:function(t,e){return Ae.apply(this,arguments)},getPoolAssets:function(t){return he.apply(this,arguments)}});function ye(t){return ve.apply(this,arguments)}function ve(){return(ve=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,o=e.asset2ID,s=re(N.V2),i=s.generateLogicSigAccountForPool(e),c=It(r,N.V2),p=i.address(),l=C(a,o),t.next=8,ae(n,p);case 8:return d=t.sent,f=oe(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:N.V2},f&&(m.asset1ProtocolFees=BigInt(f[fe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[fe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[fe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[fe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[fe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[fe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[fe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[fe.v2.totalFeeShare]),m.poolTokenID=Number(f[fe.v2.poolTokenID]),m.asset1ID=Number(f[fe.v2.asset1]),m.asset2ID=Number(f[fe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function we(){return(we=c(u().mark((function t(e,n){var r,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ae(e,n.account.address());case 2:return r=t.sent,a=oe(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[fe.v2.asset1Reserves]),o.asset2=BigInt(a[fe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[fe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(){return(_e=c(u().mark((function t(e){var n,r,a,o,s,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,ae(n,r);case 3:return o=t.sent,s=oe(o,It(a,N.V2)),i=null,s&&(i={asset1ID:s[fe[N.V2].asset1],asset2ID:s[fe[N.V2].asset2],poolTokenID:s[fe[N.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Se=Object.freeze({__proto__:null,getPoolInfo:ye,getPoolReserves:function(t,e){return we.apply(this,arguments)},getPoolAssets:function(t){return _e.apply(this,arguments)}});function Ee(t){return Boolean(t&&!(t.asset1+t.asset2))}var be,De,Oe=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Ee(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=et(t.asset1,e.asset1)/et(t.asset2,e.asset2)),r},isPoolEmpty:Ee,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Ie(t),ye(t)])}}),Ne=s((f(be={},N.V1_1,s(s({},Te),Oe)),f(be,N.V2,s(s({},Se),Oe)),be),Oe);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(De||(De={}));var Pe=96e4,ke=859e3;function Le(){return(Le=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,_,S,E,b,D,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.asset_1,s=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=V(o,s),l=w(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],I=g.id,x=g.unit_name,A=j(I),h=It(r,N.V1_1),T=$t.generateLogicSigAccountForPool({network:r,asset1ID:f,asset2ID:I}),y=T.address(),v=a.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:mt.getAppCallTxnNoteWithClientName(N.V1_1),appArgs:[lt("bootstrap"),a.default.encodeUint64(f),a.default.encodeUint64(I)],foreignAssets:A?[f]:[I],suggestedParams:c}),_=a.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:k.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),S=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Re(A),suggestedParams:c}),(b=[])[De.FUNDING_TXN]=E,b[De.VALIDATOR_APP_CALL]=v,b[De.POOL_TOKEN_CREATE]=_,b[De.ASSET1_OPT_IN]=S,A||(b[De.ASSET2_OPT_IN]=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:I,amount:0,suggestedParams:c})),D=a.default.assignGroupID(b),O=[{txn:D[De.FUNDING_TXN],signers:[i]},{txn:D[De.VALIDATOR_APP_CALL],signers:[y]},{txn:D[De.POOL_TOKEN_CREATE],signers:[y]},{txn:D[De.ASSET1_OPT_IN],signers:[y]}],D[De.ASSET2_OPT_IN]&&O.push({txn:D[De.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Re(t){return t?Pe:ke}function Fe(){return(Fe=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=C(s,i),f=w(d,2),m=f[0],g=f[1],I=$t.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===De.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,o=n.blob;return x.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(t){return Me.apply(this,arguments)}function Me(){return(Me=c(u().mark((function t(e){var n,r,a,o,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Z(n,a[De.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(s=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(s));case 10:return t.abrupt("return",{poolTokenID:s});case 13:throw t.prev=13,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Qe(){return(Qe=c(u().mark((function t(e){var n,r,a,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Be({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",Ne.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ve,Ce={generateTxns:function(t){return Le.apply(this,arguments)},signTxns:function(t){return Fe.apply(this,arguments)},execute:function(t){return Qe.apply(this,arguments)},getBootstrapFundingTxnAmount:Re};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ve||(Ve={}));var je=5,Xe=6;function qe(t,e){return Ue.apply(this,arguments)}function Ue(){return(Ue=c(u().mark((function e(n,r){var a,o,s;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return s=e.t0,e.abrupt("return",s);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ge(t,e){return We.apply(this,arguments)}function We(){return(We=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qe(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ze(){return(ze=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,_,S;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.network,s=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return p=e.sent,l=It(o,N.V2),d=t.getApplicationAddress(l),f=V(s,i),m=w(f,2),g=m[0].id,I=m[1].id,e.next=9,Ne.v2.getPoolInfo({client:r,network:o,asset1ID:g,asset2ID:I});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(s.unit_name,"-").concat(i.unit_name," already exists"));case 12:return x=ne.generateLogicSigAccountForPool({network:o,asset1ID:g,asset2ID:I}),A=x.address(),h=j(I),(T=a.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[lt("bootstrap")],note:mt.getAppCallTxnNoteWithClientName(N.V2),foreignAssets:[g,I],rekeyTo:d,suggestedParams:p})).fee=Ye(h),y=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Je(h),suggestedParams:p}),(v=[])[Ve.FUNDING_TXN]=y,v[Ve.VALIDATOR_APP_CALL]=T,_=a.default.assignGroupID(v),(S=[])[Ve.FUNDING_TXN]={txn:_[Ve.FUNDING_TXN],signers:[c]},S[Ve.VALIDATOR_APP_CALL]={txn:_[Ve.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",S);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Je(t){return function(t,e){var n=re(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(N.V2,t)+Ye(t)+1e5}function Ye(e){return((e?je:Xe)+1)*t.ALGORAND_MIN_TX_FEE}function Ke(){return(Ke=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=C(s,i),f=w(d,2),m=f[0],g=f[1],I=ne.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===Ve.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,o=n.blob;return x.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function He(){return(He=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,qe(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(fe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Ne.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 21:throw t.prev=21,t.t3=t.catch(1),new M(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Ze,$e={generateTxns:function(t){return ze.apply(this,arguments)},signTxns:function(t){return Ke.apply(this,arguments)},execute:function(t){return He.apply(this,arguments)},getBootstrapFundingTxnAmount:Je,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Je(e)}};var tn,en=(f(Ze={},N.V1_1,Ce),f(Ze,N.V2,$e),f(Ze,"generateTxns",(function(t){return t.contractVersion===N.V1_1?Ce.generateTxns(t):$e.generateTxns(t)})),f(Ze,"signTxns",(function(t){return t.contractVersion===N.V1_1?Ce.signTxns(t):$e.signTxns(t)})),f(Ze,"execute",(function(t){return t.contractVersion===N.V1_1?Ce.execute(t):$e.execute(t)})),f(Ze,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===N.V1_1?Ce.getBootstrapFundingTxnAmount(n):$e.getBootstrapFundingTxnAmount(n)})),Ze);exports.V1_1AddLiquidityTxnIndices=void 0,(tn=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[tn.FEE_TXN=0]="FEE_TXN",tn[tn.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",tn[tn.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",tn[tn.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",tn[tn.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var nn,rn,an,on,sn=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(on=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",on.FLEXIBLE="flexible",on.INITIAL="initial";var un,cn=(f(nn={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(nn,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(nn,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),nn),pn=(f(rn={},exports.V2AddLiquidityType.INITIAL,1),f(rn,exports.V2AddLiquidityType.SINGLE,2),f(rn,exports.V2AddLiquidityType.FLEXIBLE,2),rn),ln=(f(an={},exports.V2AddLiquidityType.INITIAL,3),f(an,exports.V2AddLiquidityType.FLEXIBLE,3),f(an,exports.V2AddLiquidityType.SINGLE,2),an);function dn(t){var e,n,r,a=t.reserves,o=t.totalFeeShare,s=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(s.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,I=BigInt(s.amount)-m,x=BigInt(u.amount)-g;if(I>x){var A=I;r=fn(A,o),e={id:s.id,amount:A+r,decimals:s.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=x;r=fn(h,o),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:s.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:s.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Wt({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function fn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function mn(e){return(pn[e]+1)*t.ALGORAND_MIN_TX_FEE}var gn=lt("add_liquidity"),In=(f(un={},N.V1_1,[lt("mint")]),f(un,N.V2,{INITIAL_LIQUIDITY:[lt("add_initial_liquidity")],SINGLE_ASSET_MODE:[gn,lt("single")],FLEXIBLE_MODE:[gn,lt("flexible")]}),un);function xn(){return(xn=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.poolAddress,s=e.asset1In,i=e.asset2In,c=e.poolTokenOut,p=e.slippage,l=e.initiatorAddr,d=tt("negative",p,c.amount),t.next=4,n.getTransactionParams().do();case 4:return f=t.sent,m=a.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:It(r,N.V1_1),appArgs:In.v1_1,accounts:[l],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==i.id?[s.id,c.id]:[s.id,i.id,c.id],suggestedParams:f}),g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:s.id,amount:s.amount,suggestedParams:f}),I=0===i.id?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:i.amount,suggestedParams:f}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),x=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:O,suggestedParams:f}),h=a.default.assignGroupID([A,m,g,I,x]),t.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function An(){return(An=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,s=n.account,t.next=4,o([r]);case 4:return i=t.sent,c=w(i,3),p=c[0],l=c[1],d=c[2],f=r.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:a.default.signLogicSigTransactionObject(t.txn,s).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hn(){return(hn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.signedTxns,s=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,pe({client:n,pool:r,accountAddr:s});case 5:return c=t.sent,t.next=8,st(n,[o]);case 8:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=it(a),I=ct(a),t.next=17,pe({client:n,pool:r,accountAddr:s});case 17:return x=t.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:I});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new M(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Tn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var o=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(o<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(o,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:o-BigInt(1e3),share:1}}var s=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=si)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Gt(d),priceImpact:Wt(s({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function zn(t){return Jn.apply(this,arguments)}function Jn(){return(Jn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:s.id,assetOutAmount:BigInt(s.amount)+A,excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yn(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap on a non-existent pool");var o,s,u,i=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,s=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,s=n.asset1,u=n.asset2}if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=s*u/(u-i)-s,p=c*qn/(qn-Xn),l=p-c,d=et(a.assetOut,Number(i))/et(a.assetIn,Number(p)),f=et(a.assetOut,Number(u))/et(a.assetIn,Number(s)),m=rt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:o,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function Kn(t){return Hn.apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:s.id,assetOutAmount:BigInt(s.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,r,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,o=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new M({pool:r,swapType:a,txGroup:o},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:o[Cn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:o[Cn.ASSET_IN_TXN_INDEX].txn.amount},l={id:o[Cn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:o[Cn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,zn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Kn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",s(s({},d),{},{groupID:ct(o),fees:it(o)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new M(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Cn||(Cn={}));var $n={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Wn({pool:e,reserves:n,assetIn:r,decimals:a}):Yn({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Wn,getFixedOutputSwapQuote:Yn,generateTxns:function(t){return Gn.apply(this,arguments)},signTxns:function(t){return Un.apply(this,arguments)},execute:function(t){return Zn.apply(this,arguments)},executeFixedOutputSwap:Kn},tr=4*t.ALGORAND_MIN_TX_FEE;function er(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:.003;return Math.ceil(1/t)}function nr(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:.003;return tn&&(e=a,n=o)}return e}function cr(){return(cr=c(u().mark((function t(e){var n,r,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Vt(s(s({},e),{},{route:e.quote.data.route})));case 2:if(n=e.client,r=e.initiatorAddr,o=e.slippage,i=e.swapType,c=e.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(I=[l.asset1ID,l.asset2ID]).includes(f)&&I.includes(m)&&f!==m){t.next=9;break}throw new M({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return x=t.sent,A=j(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:tt("positive",o,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:tt("negative",o,d.assetOutAmount),y=A?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,suggestedParams:x}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,assetIndex:f,suggestedParams:x}),(v=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:l.validatorAppID,appArgs:[Et,bt[i],a.default.encodeUint64(T)],note:mt.getAppCallTxnNoteWithClientName(N.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=pr(i),(w=[])[vt.INPUT_TXN]=y,w[vt.APP_CALL_TXN]=v,_=a.default.assignGroupID(w),t.abrupt("return",[{txn:_[vt.INPUT_TXN],signers:[r]},{txn:_[vt.APP_CALL_TXN],signers:[r]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pr(e){return(St[e]+1)*t.ALGORAND_MIN_TX_FEE}function lr(){return(lr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,o=e.quote,s=e.txGroup,i=e.signedTxns,t.next=3,st(a,[i]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Ge(a,s);case 10:return m=t.sent,g=zt(o),I=Jt(o).id,x=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===I})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:x&&{amount:BigInt(g.amount)-BigInt(x.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:I},quote:o,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dr(){return(dr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.amount,a=e.assetInID,o=e.assetOutID,s=e.decimals,i=e.network,c=e.isSwapRouterEnabled,p=e.pool,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,Ir({assetInID:a,assetOutID:o,amount:r,decimals:s,isSwapRouterEnabled:c,network:i,pool:p});case 4:l=t.sent,t.next=10;break;case 7:return t.next=9,Ar({amount:r,assetInID:a,assetOutID:o,decimals:s,isSwapRouterEnabled:c,network:i,pool:p});case 9:l=t.sent;case 10:return t.abrupt("return",l);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=t[0].reason;if(2===t.length){var n=t[1].reason;if(rr(e)&&!rr(n))throw e;throw n}throw e}return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function mr(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,o=t.pool;if(!Ne.isPoolReady(o))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");if(nr(Number(e)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");var s,u,i=BigInt(e),c=o.totalFeeShare;if(n===o.asset1ID)s=o.asset1Reserves,u=o.asset2Reserves;else{if(n!==o.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Input asset (#".concat(n,") doesn't belong to the pool ").concat(o.account.address(),"."));s=o.asset2Reserves,u=o.asset1Reserves}var p=Tr({inputSupply:s,outputSupply:u,swapInputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapOutputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(l>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");return{assetInID:n,assetInAmount:i,assetOutID:r,assetOutAmount:l,swapFee:Number(d),rate:et(a.assetOut,Number(l))/et(a.assetIn,Number(i)),priceImpact:f}}function gr(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,o=t.pool;if(!o||!Ne.isPoolReady(o))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");var s,u,i=BigInt(e),c=o.totalFeeShare;if(r===o.asset1ID)s=o.asset2Reserves,u=o.asset1Reserves;else{if(r!==o.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r,") doesn't belong to the pool ").concat(o.account.address(),"."));s=o.asset1Reserves,u=o.asset2Reserves}var p=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,o=t.decimals,s=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:s,totalFeeShare:a}),i=s+u,c=Wt({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:o.assetIn},assetOut:{amount:r,decimals:o.assetOut}});return{swapInputAmount:i,totalFeeAmount:u,priceImpact:c}}({inputSupply:s,outputSupply:u,swapOutputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapInputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(nr(Number(l)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");return{type:exports.SwapQuoteType.Direct,data:{pool:o,quote:{assetInID:n,assetInAmount:l,assetOutID:r,assetOutAmount:i,swapFee:Number(d),rate:et(a.assetOut,Number(i))/et(a.assetIn,Number(l)),priceImpact:f}}}}function Ir(t){return xr.apply(this,arguments)}function xr(){return(xr=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[],p?l.push(new Promise((function(t,e){try{var s=mr({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p});t({type:exports.SwapQuoteType.Direct,data:{pool:p,quote:s}})}catch(t){e(t)}}))):l.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools."))),s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedInput,network:i});case 2:if(!((e=t.sent).route.length<2)){t.next=5;break}throw new D(exports.SwapQuoteErrorType.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 5:return t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 6:case"end":return t.stop()}}),t)})))()),t.next=6,fr(l);case 6:return d=t.sent,t.abrupt("return",ir(d));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){return hr.apply(this,arguments)}function hr(){return(hr=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[new Promise((function(t,e){try{t(gr({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p}))}catch(t){e(t)}}))],s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedOutput,network:i});case 2:return e=t.sent,t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 4:case"end":return t.stop()}}),t)})))()),t.next=5,fr(l);case 5:return d=t.sent,t.abrupt("return",ir(d));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Tr(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,o=t.decimals,s=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-s});return{swapOutputAmount:u,totalFeeAmount:s,priceImpact:Wt({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:o.assetIn},assetOut:{amount:u,decimals:o.assetOut}})}}var yr={getQuote:function(t){return dr.apply(this,arguments)},getFixedInputSwapQuote:Ir,getFixedInputDirectSwapQuote:mr,getFixedOutputDirectSwapQuote:gr,getFixedOutputSwapQuote:Ar,generateTxns:function(t){return cr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return lr.apply(this,arguments)},calculateFixedInputSwap:Tr};function vr(t,e){var n,r,a=BigInt(t),o=e.issuedLiquidity;return o>a+BigInt(1e3)?(n=a*e.asset1/o,r=a*e.asset2/o):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.account.address(),m=o.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:s,suggestedParams:d}),(I=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(tt("negative",l,c)),a.default.encodeUint64(tt("negative",l,p))],accounts:[f],foreignAssets:[o.asset1ID,o.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(x=[])[Rn.ASSET_TRANSFER_TXN]=g,x[Rn.APP_CALL_TXN]=I,A=a.default.assignGroupID(x),e.abrupt("return",[{txn:A[Rn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[Rn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function _r(){return(_r=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.asset1ID,m=o.asset2ID,g=o.account.address(),I=o.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(x=0,A=0,h=tt("negative",l,p),c!==f){e.next=17;break}x=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}x=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,assetIndex:I,amount:i,suggestedParams:d}),(y=a.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(x),a.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[Rn.ASSET_TRANSFER_TXN]=T,v[Rn.APP_CALL_TXN]=y,w=a.default.assignGroupID(v),e.abrupt("return",[{txn:w[Rn.ASSET_TRANSFER_TXN],signers:[s]},{txn:w[Rn.APP_CALL_TXN],signers:[s]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Sr(){return(Sr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,o=e.signedTxns,t.next=3,st(r,[o]);case 3:return s=t.sent,i=w(s,1),c=i[0].txnID,t.next=8,Ge(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Er,br,Dr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=vr(a,n),s=o.asset1OutputAmount,u=o.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:s},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,o=t.assetOutID,s=t.decimals,u=BigInt(a),i=vr(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(o===n.asset1ID){var d=yr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:s}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(o!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var I=yr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:s}),x=I.swapOutputAmount,A=I.totalFeeAmount,h=I.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return _r.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Sr.apply(this,arguments)}},Or=(f(Er={},N.V1_1,jn),f(Er,N.V2,Dr),Er),Nr=(f(br={},N.V1_1,$n),f(br,N.V2,yr),f(br,"getQuote",(function(t){var e=t.type,n=t.pools;if(!t.isSwapRouterEnabled&&n.every((function(t){return Ee(t.reserves)})))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");if(e===exports.SwapType.FixedInput)return function(t){return or.apply(this,arguments)}(t);if(e===exports.SwapType.FixedOutput)return function(t){return sr.apply(this,arguments)}(t);throw new D(exports.SwapQuoteErrorType.InvalidSwapTypeError,"Invalid swap type")})),f(br,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1?$n.generateTxns(s(s({},t),{},{quote:t.quote.data})):yr.generateTxns(t)})),f(br,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1){var e=t.quote.data.pool;return $n.signTxns(s({pool:e},t))}return yr.signTxns(t)})),f(br,"execute",(function(t){return t.contractVersion===N.V1_1?$n.execute(t):yr.execute(t)})),br);function Pr(){return(Pr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,kr({txGroup:a,pool:r,initiatorSigner:o});case 4:return s=t.sent,t.next=7,st(n,[s]);case 7:return i=t.sent,c=w(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:it(a),confirmedRound:d,txnID:l,groupID:ct(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function kr(t){return Lr.apply(this,arguments)}function Lr(){return(Lr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.pool,o=e.initiatorSigner,t.next=3,o([n]);case 3:return s=t.sent,i=w(s,1),c=i[0],p=r.account.lsig,l=n.map((function(t,e){return 0===e?c:a.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rr(){return Rr=c(u().mark((function t(e){var n,r,o,s,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.data,o=e.initiatorSigner,t.prev=1,s=r.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:it(e),groupID:ct(e),lsig:n.account.lsig}})),t.next=5,o(s.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(s.map((function(t,e){return new Promise(function(){var r=c(u().mark((function r(o,s){var c,p,l,d,f,m;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,c=t.txns.map((function(n,r){return 0===r?i[e]:a.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),r.next=4,st(n,[c]);case 4:p=r.sent,l=w(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),r.next=15;break;case 12:r.prev=12,r.t0=r.catch(0),s(r.t0);case 15:case"end":return r.stop()}}),r,null,[[0,12]])})));return function(t,e){return r.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Rr.apply(this,arguments)}function Fr(){return(Fr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,o=e.assetID,s=e.assetOut,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=r.account.address(),l=a.default.makeApplicationNoOpTxnFromObject({from:p,appIndex:r.validatorAppID,appArgs:[lt("redeem")],note:mt.getAppCallTxnNoteWithClientName(r.contractVersion),accounts:[i],foreignAssets:0==r.asset2ID?[r.asset1ID,r.poolTokenID]:[r.asset1ID,r.asset2ID,r.poolTokenID],suggestedParams:c}),d=0===o?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:i,amount:BigInt(s),suggestedParams:c}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:i,assetIndex:o,amount:BigInt(s),suggestedParams:c}),f=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:p,amount:l.fee+d.fee,note:O,suggestedParams:c}),m=a.default.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[i]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,o=e.liquidityAssetID,s=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(s.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[lt("commit"),i],note:U([lt("tinymanStaking/v1:b"),c,t.encodeUint64(o),i])})}function Mr(){return(Mr=c(u().mark((function e(n){var r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,s=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Br({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof s){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[lt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=P,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Ln,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=en,exports.CONTRACT_VERSION=N,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=k,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Or,exports.Swap=Nr,exports.SwapQuoteError=D,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Fn,exports.V2AddLiquidityTxnIndices=cn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=tt,exports.calculateAccountMinimumRequiredBalance=se,exports.calculatePriceImpact=Wt,exports.calculateSwapRate=Gt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=Ne,exports.prepareCommitTransactions=function(t){return Mr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Rr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Pr.apply(this,arguments)},exports.sendAndWaitRawTransaction=st,exports.sumUpTxnFees=it,exports.tinymanContract_v1_1=$t,exports.tinymanContract_v2=ne,exports.tinymanJSSDKConfig=mt; diff --git a/dist/swap/constants.d.ts b/dist/swap/constants.d.ts index c5a3af4d..fe4d5ef0 100644 --- a/dist/swap/constants.d.ts +++ b/dist/swap/constants.d.ts @@ -2,3 +2,4 @@ export declare enum SwapType { FixedInput = "fixed-input", FixedOutput = "fixed-output" } +export declare const DEFAULT_SWAP_FEE_RATE = 0.003; diff --git a/dist/swap/v2/util.d.ts b/dist/swap/v2/util.d.ts index 59c2da73..3e4582f0 100644 --- a/dist/swap/v2/util.d.ts +++ b/dist/swap/v2/util.d.ts @@ -3,3 +3,11 @@ import { SwapType } from "../constants"; * @returns the total fee for the swap operation including all transactions (including inner transactions) fees */ export declare function getV2SwapTotalFee(mode: SwapType): number; +/** + * @returns the minimum possible amount of assetIn that can be used for swap in V2 pools + */ +export declare function getV2MinSwapAssetInAmount(feeRate?: number): number; +/** + * @returns true if the amount of assetIn is less than the minimum possible amount of assetIn that can be used for swap in V2 pools + */ +export declare function isSwapAssetInAmountLow(amount: number, feeRate?: number): boolean; diff --git a/src/swap/constants.ts b/src/swap/constants.ts index 833f5573..cc63bfb2 100644 --- a/src/swap/constants.ts +++ b/src/swap/constants.ts @@ -2,3 +2,5 @@ export enum SwapType { FixedInput = "fixed-input", FixedOutput = "fixed-output" } + +export const DEFAULT_SWAP_FEE_RATE = 0.003; diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index cb954e31..d4db7893 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -39,6 +39,7 @@ import {generateSwapRouterTxns, getSwapRoute} from "./router/swap-router"; import {poolUtils} from "../../util/pool"; import {getBestQuote, isSwapQuoteErrorCausedByAmount} from "../utils"; import SwapQuoteError, {SwapQuoteErrorType} from "../../util/error/SwapQuoteError"; +import {isSwapAssetInAmountLow} from "./util"; async function generateTxns( params: GenerateSwapTxnsParams @@ -306,6 +307,13 @@ function getFixedInputDirectSwapQuote({ ); } + if (isSwapAssetInAmountLow(Number(amount))) { + throw new SwapQuoteError( + SwapQuoteErrorType.LowSwapAmountError, + "Swap amount is too low." + ); + } + const assetInAmount = BigInt(amount); const totalFeeShare = pool.totalFeeShare!; @@ -399,6 +407,13 @@ function getFixedOutputDirectSwapQuote({ decimals }); + if (isSwapAssetInAmountLow(Number(swapInputAmount))) { + throw new SwapQuoteError( + SwapQuoteErrorType.LowSwapAmountError, + "Swap amount is too low." + ); + } + if (assetOutAmount > outputSupply) { throw new SwapQuoteError( SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError, @@ -493,6 +508,13 @@ async function getFixedInputSwapQuote({ network }); + if (data.route.length < 2) { + throw new SwapQuoteError( + SwapQuoteErrorType.SwapRouterNoRouteError, + "Swap router couldn't find a route for this swap." + ); + } + return { type: SwapQuoteType.Router, data diff --git a/src/swap/v2/util.ts b/src/swap/v2/util.ts index 59c87d81..269e04de 100644 --- a/src/swap/v2/util.ts +++ b/src/swap/v2/util.ts @@ -1,6 +1,6 @@ import {ALGORAND_MIN_TX_FEE} from "algosdk"; -import {SwapType} from "../constants"; +import {SwapType, DEFAULT_SWAP_FEE_RATE} from "../constants"; import {V2_SWAP_APP_CALL_INNER_TXN_COUNT, V2_SWAP_TXN_COUNT} from "./constants"; /** @@ -11,3 +11,20 @@ export function getV2SwapTotalFee(mode: SwapType) { return totalTxnCount * ALGORAND_MIN_TX_FEE; } + +/** + * @returns the minimum possible amount of assetIn that can be used for swap in V2 pools + */ +export function getV2MinSwapAssetInAmount(feeRate: number = DEFAULT_SWAP_FEE_RATE) { + return Math.ceil(1 / feeRate); +} + +/** + * @returns true if the amount of assetIn is less than the minimum possible amount of assetIn that can be used for swap in V2 pools + */ +export function isSwapAssetInAmountLow( + amount: number, + feeRate: number = DEFAULT_SWAP_FEE_RATE +) { + return amount < getV2MinSwapAssetInAmount(feeRate); +} From 346432b80c9945cab255d0f6807ef9ffafe7778e Mon Sep 17 00:00:00 2001 From: anlerkan Date: Mon, 20 Mar 2023 15:13:09 +0300 Subject: [PATCH 24/39] chore: export v2 swap utils --- dist/index.d.ts | 1 + dist/index.js | 2 +- src/index.ts | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index 53030492..38a11ceb 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -31,6 +31,7 @@ export { V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT } from "./remove-liquidity export { RemoveLiquidity } from "./remove-liquidity"; export type { SwapQuote, V1SwapExecution, V2SwapExecution, DirectSwapQuote, SwapRoute, GenerateSwapTxnsParams } from "./swap/types"; export { getSwapTotalFee } from "./swap/utils"; +export * from "./swap/v2/util"; export { SwapType } from "./swap/constants"; export { Swap } from "./swap"; export { SwapQuoteType } from "./swap/types"; diff --git a/dist/index.js b/dist/index.js index 19e43257..f752d231 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r,a=n(t);function o(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function s(t){for(var e=1;e=0;--a){var o=this.tryEntries[a],s=o.completion;if("root"===o.tryLoc)return r("end");if(o.tryLoc<=this.prev){var u=n.call(o,"catchLoc"),i=n.call(o,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),w(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;w(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:S(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,o,s){try{var u=t[o](s),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function s(t){i(o,r,a,s,u,"next",t)}function u(t){i(o,r,a,s,u,"throw",t)}s(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,s=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){u=!0,o=t},f:function(){try{s||null==n.return||n.return()}finally{if(u)throw o}}}}exports.SwapQuoteErrorType=void 0,(r=exports.SwapQuoteErrorType||(exports.SwapQuoteErrorType={})).SwapRouterStaleDataError="SwapRouterStaleDataError",r.SwapRouterNoRouteError="SwapRouterNoRouteError",r.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",r.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",r.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",r.NoAvailablePoolError="NoAvailablePoolError",r.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",r.UnknownError="UnknownError",r.LowSwapAmountError="LowSwapAmountError",r.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",r.InvalidSwapTypeError="InvalidSwapTypeError";var D=function(t){m(n,h(Error));var e=v(n);function n(t,r){var a;return p(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(T(a),n),a}return d(n)}(),O=Uint8Array.from([1]),N={V1_1:"v1_1",V2:"v2"},P={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},k={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},L="- would result negative",R="logic eval error:",F="exceeds schema integer count",B=/transaction \w+:/,M=function(t){m(n,h(Error));var e=v(n);function n(t,r){var a;p(this,n);for(var o=arguments.length,s=new Array(o>2?o-2:0),u=2;ur?[s(s({},t),{},{id:n}),s(s({},e),{},{id:r})]:[s(s({},e),{},{id:r}),s(s({},t),{},{id:n})]}function C(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function j(t){return 0===Number(t)}function X(t){return Number(t.id)}function q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,o=void 0!==a&&a,s={},u=b(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}s[o?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return s}function U(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,o=b(t);try{for(o.s();!(e=o.n()).done;){var s=e.value;r.set(s,a),a+=s.length}}catch(t){o.e(t)}finally{o.f()}return r}var G=100000n,W=100000n,z=100000n,J=25000n+25000n,Y=25000n+3500n;function K(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],o=t["created-apps"]||[],s=t.assets||[];return G+W*BigInt(s.length)+z*BigInt(o.length+a.length)+Y*r+J*n}function H(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,H(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function tt(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function et(t,e){var n=Number(t);return rt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function nt(t,e){return rt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function rt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=w(ot(e),2),o=a[0],s=a[1],u=w(ot(Math.round(Number(at(o,s+r)))),2),i=u[0],c=u[1];return Number(at(i,c-r))}return Math.round(e)}function at(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ot(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function st(t,e){return ut.apply(this,arguments)}function ut(){return(ut=c(u().mark((function t(e,n){var r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=b(n),t.prev=3,a.s();case 5:if((o=a.n()).done){t.next=18;break}return s=o.value,t.next=9,e.sendRawTransaction(s).do();case 9:return i=t.sent,c=i.txId,t.next=13,Z(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new M(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function it(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ct(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function pt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function lt(t){return(new TextEncoder).encode(t)}function dt(t){return Boolean(t)&&void 0!==t.fallback_message}var ft,mt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return lt("tinyman/".concat(t===N.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),gt=(f(ft={},N.V1_1,{testnet:62368684,mainnet:552635992}),f(ft,N.V2,{testnet:148607e3,mainnet:1002541853}),ft);function It(t,e){var n=gt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function xt(){return(xt=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationOptInTxnFromObject({from:s,appIndex:It(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var At;function ht(){return(ht=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationClearStateTxnFromObject({from:s,appIndex:It(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(At=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",At.FixedOutput="fixed-output";var Tt,yt,vt;!function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(vt||(vt={}));var wt,_t,St=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),Et=lt("swap"),bt=(f(yt={},exports.SwapType.FixedInput,lt("fixed-input")),f(yt,exports.SwapType.FixedOutput,lt("fixed-output")),yt),Dt={ASSET_OPT_IN:lt("asset_opt_in")},Ot={testnet:159521633,mainnet:0},Nt=(f(wt={},exports.SwapType.FixedInput,7),f(wt,exports.SwapType.FixedOutput,8),wt),Pt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function kt(t){var e=Bt(t),n=e.assetIn,r=e.assetOut;return et(r.asset.decimals,Number(r.amount))/et(n.asset.decimals,Number(n.amount))}function Lt(t){if(!Ot[t])throw new Error("Unknown network or network not supported: ".concat(t));return Ot[t]}function Rt(t){return t[t.length-1].quote.amount_out}function Ft(t){return t[0].quote.amount_in}function Bt(t){return{assetIn:Ft(t),assetOut:Rt(t)}}function Mt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function e(n){var r,o,s,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.routerAppID,s=n.assetIDs,i=n.accountAddress,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o,appArgs:[Dt.ASSET_OPT_IN],foreignAssets:s,suggestedParams:c}),l=s.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w,_,S,E,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,o=n.client,s=n.network,i=n.swapType,c=n.route,e.next=3,o.getTransactionParams().do();case 3:return p=e.sent,l=[X(c[0].quote.amount_in.asset),X(c[0].quote.amount_out.asset),X(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Ft(c).amount),Number(Rt(c).amount)],I=g[0],x=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=j(d),v=Lt(s),w=y?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(v),amount:I,suggestedParams:p}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(v),amount:I,assetIndex:d,suggestedParams:p}),(_=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:v,appArgs:[Et,bt[i],a.default.encodeUint64(x)],foreignApps:[It(s,N.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Nt[i]+1),S=[w,_],e.next=15,jt({client:o,routerAppID:v,assetIDs:[d,f,m]});case 15:if(!((E=e.sent).length>0)){e.next=21;break}return e.next=19,Mt({client:o,accountAddress:r,assetIDs:E,routerAppID:v});case 19:b=e.sent,S.unshift(b);case 21:return D=a.default.assignGroupID(S),e.abrupt("return",D.map((function(t){return{txn:t,signers:[r]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(u().mark((function e(n){var r,a,o,s,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,o=n.assetIDs,s=t.getApplicationAddress(a),e.next=4,r.accountInformation(s).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=o.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qt(t){return Ut.apply(this,arguments)}function Ut(){return(Ut=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.swapType,s=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Pt[s].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(!dt(p)){t.next=13;break}throw new D(p.type,p.fallback_message);case 13:throw new D(exports.SwapQuoteErrorType.UnknownError,"Unknown error");case 14:return t.abrupt("return",p);case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Gt(t){var e=t.assetIn,n=t.assetOut;return et(n.decimals,Number(n.amount))/et(e.decimals,Number(e.amount))}function Wt(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,o=Gt({assetIn:r,assetOut:a}),s=et(a.decimals,Number(n))/et(r.decimals,Number(e));return rt({decimalPlaces:5},Math.abs(o/s-1))}function zt(t){if(t.type===exports.SwapQuoteType.Router){var e=Ft(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function Jt(t){if(t.type===exports.SwapQuoteType.Router){var e=Rt(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function Yt(t){return t.type===exports.SwapQuoteType.Direct?t.data.pool.contractVersion:N.V2}exports.SwapQuoteType=void 0,(_t=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",_t.Router="router";var Kt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Ht={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Zt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),$t=new(function(n){m(a,Zt);var r=v(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=It(n.network,N.V1_1),a=w(C(n.asset1ID,n.asset2ID),2),o=a[0],s=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:o,asset_id_2:s,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(s(s({},i),{},{minimum_required_balance:se(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),o(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function oe(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function se(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ue,ie,ce=lt("e");function pe(t){return le.apply(this,arguments)}function le(){return(le=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w,_,S,E;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.pool,i=r.accountAddr,n.next=3,o.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=s.account.address(),g=b(p),n.prev=10,g.s();case 12:if((I=g.n()).done){n.next=31;break}if((x=I.value).id==s.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=x["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=q({stateArray:A}),T=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.asset1ID)])),y=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.asset2ID)])),v=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.poolTokenID)])),w=h[T],_=h[y],S=h[v],"bigint"==typeof w&&(l=w),"bigint"==typeof _&&(d=_),"bigint"==typeof S&&(f=S);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((E={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function de(){return(de=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,I,x,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.accountAddr,i=r.validatorAppID,n.next=3,o.accountInformation(s).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mge)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ge,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function he(){return he=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ae(n,r);case 6:return s=t.sent,i=oe(s,It(a,N.V1_1)),c=null,i&&(l=s["created-assets"][0],p=l.index,c={asset1ID:i[fe[N.V1_1].asset1],asset2ID:i[fe[N.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),he.apply(this,arguments)}var Te=Object.freeze({__proto__:null,getPoolInfo:Ie,getPoolReserves:function(t,e){return Ae.apply(this,arguments)},getPoolAssets:function(t){return he.apply(this,arguments)}});function ye(t){return ve.apply(this,arguments)}function ve(){return(ve=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,o=e.asset2ID,s=re(N.V2),i=s.generateLogicSigAccountForPool(e),c=It(r,N.V2),p=i.address(),l=C(a,o),t.next=8,ae(n,p);case 8:return d=t.sent,f=oe(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:N.V2},f&&(m.asset1ProtocolFees=BigInt(f[fe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[fe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[fe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[fe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[fe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[fe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[fe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[fe.v2.totalFeeShare]),m.poolTokenID=Number(f[fe.v2.poolTokenID]),m.asset1ID=Number(f[fe.v2.asset1]),m.asset2ID=Number(f[fe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function we(){return(we=c(u().mark((function t(e,n){var r,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ae(e,n.account.address());case 2:return r=t.sent,a=oe(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[fe.v2.asset1Reserves]),o.asset2=BigInt(a[fe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[fe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(){return(_e=c(u().mark((function t(e){var n,r,a,o,s,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,ae(n,r);case 3:return o=t.sent,s=oe(o,It(a,N.V2)),i=null,s&&(i={asset1ID:s[fe[N.V2].asset1],asset2ID:s[fe[N.V2].asset2],poolTokenID:s[fe[N.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Se=Object.freeze({__proto__:null,getPoolInfo:ye,getPoolReserves:function(t,e){return we.apply(this,arguments)},getPoolAssets:function(t){return _e.apply(this,arguments)}});function Ee(t){return Boolean(t&&!(t.asset1+t.asset2))}var be,De,Oe=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Ee(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=et(t.asset1,e.asset1)/et(t.asset2,e.asset2)),r},isPoolEmpty:Ee,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Ie(t),ye(t)])}}),Ne=s((f(be={},N.V1_1,s(s({},Te),Oe)),f(be,N.V2,s(s({},Se),Oe)),be),Oe);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(De||(De={}));var Pe=96e4,ke=859e3;function Le(){return(Le=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,_,S,E,b,D,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.asset_1,s=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=V(o,s),l=w(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],I=g.id,x=g.unit_name,A=j(I),h=It(r,N.V1_1),T=$t.generateLogicSigAccountForPool({network:r,asset1ID:f,asset2ID:I}),y=T.address(),v=a.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:mt.getAppCallTxnNoteWithClientName(N.V1_1),appArgs:[lt("bootstrap"),a.default.encodeUint64(f),a.default.encodeUint64(I)],foreignAssets:A?[f]:[I],suggestedParams:c}),_=a.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:k.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),S=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Re(A),suggestedParams:c}),(b=[])[De.FUNDING_TXN]=E,b[De.VALIDATOR_APP_CALL]=v,b[De.POOL_TOKEN_CREATE]=_,b[De.ASSET1_OPT_IN]=S,A||(b[De.ASSET2_OPT_IN]=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:I,amount:0,suggestedParams:c})),D=a.default.assignGroupID(b),O=[{txn:D[De.FUNDING_TXN],signers:[i]},{txn:D[De.VALIDATOR_APP_CALL],signers:[y]},{txn:D[De.POOL_TOKEN_CREATE],signers:[y]},{txn:D[De.ASSET1_OPT_IN],signers:[y]}],D[De.ASSET2_OPT_IN]&&O.push({txn:D[De.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Re(t){return t?Pe:ke}function Fe(){return(Fe=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=C(s,i),f=w(d,2),m=f[0],g=f[1],I=$t.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===De.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,o=n.blob;return x.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(t){return Me.apply(this,arguments)}function Me(){return(Me=c(u().mark((function t(e){var n,r,a,o,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Z(n,a[De.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(s=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(s));case 10:return t.abrupt("return",{poolTokenID:s});case 13:throw t.prev=13,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Qe(){return(Qe=c(u().mark((function t(e){var n,r,a,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Be({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",Ne.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ve,Ce={generateTxns:function(t){return Le.apply(this,arguments)},signTxns:function(t){return Fe.apply(this,arguments)},execute:function(t){return Qe.apply(this,arguments)},getBootstrapFundingTxnAmount:Re};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ve||(Ve={}));var je=5,Xe=6;function qe(t,e){return Ue.apply(this,arguments)}function Ue(){return(Ue=c(u().mark((function e(n,r){var a,o,s;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return s=e.t0,e.abrupt("return",s);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ge(t,e){return We.apply(this,arguments)}function We(){return(We=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qe(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ze(){return(ze=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,_,S;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.network,s=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return p=e.sent,l=It(o,N.V2),d=t.getApplicationAddress(l),f=V(s,i),m=w(f,2),g=m[0].id,I=m[1].id,e.next=9,Ne.v2.getPoolInfo({client:r,network:o,asset1ID:g,asset2ID:I});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(s.unit_name,"-").concat(i.unit_name," already exists"));case 12:return x=ne.generateLogicSigAccountForPool({network:o,asset1ID:g,asset2ID:I}),A=x.address(),h=j(I),(T=a.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[lt("bootstrap")],note:mt.getAppCallTxnNoteWithClientName(N.V2),foreignAssets:[g,I],rekeyTo:d,suggestedParams:p})).fee=Ye(h),y=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Je(h),suggestedParams:p}),(v=[])[Ve.FUNDING_TXN]=y,v[Ve.VALIDATOR_APP_CALL]=T,_=a.default.assignGroupID(v),(S=[])[Ve.FUNDING_TXN]={txn:_[Ve.FUNDING_TXN],signers:[c]},S[Ve.VALIDATOR_APP_CALL]={txn:_[Ve.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",S);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Je(t){return function(t,e){var n=re(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(N.V2,t)+Ye(t)+1e5}function Ye(e){return((e?je:Xe)+1)*t.ALGORAND_MIN_TX_FEE}function Ke(){return(Ke=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=C(s,i),f=w(d,2),m=f[0],g=f[1],I=ne.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===Ve.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,o=n.blob;return x.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function He(){return(He=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,qe(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(fe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Ne.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 21:throw t.prev=21,t.t3=t.catch(1),new M(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Ze,$e={generateTxns:function(t){return ze.apply(this,arguments)},signTxns:function(t){return Ke.apply(this,arguments)},execute:function(t){return He.apply(this,arguments)},getBootstrapFundingTxnAmount:Je,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Je(e)}};var tn,en=(f(Ze={},N.V1_1,Ce),f(Ze,N.V2,$e),f(Ze,"generateTxns",(function(t){return t.contractVersion===N.V1_1?Ce.generateTxns(t):$e.generateTxns(t)})),f(Ze,"signTxns",(function(t){return t.contractVersion===N.V1_1?Ce.signTxns(t):$e.signTxns(t)})),f(Ze,"execute",(function(t){return t.contractVersion===N.V1_1?Ce.execute(t):$e.execute(t)})),f(Ze,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===N.V1_1?Ce.getBootstrapFundingTxnAmount(n):$e.getBootstrapFundingTxnAmount(n)})),Ze);exports.V1_1AddLiquidityTxnIndices=void 0,(tn=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[tn.FEE_TXN=0]="FEE_TXN",tn[tn.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",tn[tn.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",tn[tn.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",tn[tn.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var nn,rn,an,on,sn=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(on=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",on.FLEXIBLE="flexible",on.INITIAL="initial";var un,cn=(f(nn={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(nn,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(nn,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),nn),pn=(f(rn={},exports.V2AddLiquidityType.INITIAL,1),f(rn,exports.V2AddLiquidityType.SINGLE,2),f(rn,exports.V2AddLiquidityType.FLEXIBLE,2),rn),ln=(f(an={},exports.V2AddLiquidityType.INITIAL,3),f(an,exports.V2AddLiquidityType.FLEXIBLE,3),f(an,exports.V2AddLiquidityType.SINGLE,2),an);function dn(t){var e,n,r,a=t.reserves,o=t.totalFeeShare,s=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(s.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,I=BigInt(s.amount)-m,x=BigInt(u.amount)-g;if(I>x){var A=I;r=fn(A,o),e={id:s.id,amount:A+r,decimals:s.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=x;r=fn(h,o),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:s.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:s.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Wt({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function fn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function mn(e){return(pn[e]+1)*t.ALGORAND_MIN_TX_FEE}var gn=lt("add_liquidity"),In=(f(un={},N.V1_1,[lt("mint")]),f(un,N.V2,{INITIAL_LIQUIDITY:[lt("add_initial_liquidity")],SINGLE_ASSET_MODE:[gn,lt("single")],FLEXIBLE_MODE:[gn,lt("flexible")]}),un);function xn(){return(xn=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.poolAddress,s=e.asset1In,i=e.asset2In,c=e.poolTokenOut,p=e.slippage,l=e.initiatorAddr,d=tt("negative",p,c.amount),t.next=4,n.getTransactionParams().do();case 4:return f=t.sent,m=a.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:It(r,N.V1_1),appArgs:In.v1_1,accounts:[l],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==i.id?[s.id,c.id]:[s.id,i.id,c.id],suggestedParams:f}),g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:s.id,amount:s.amount,suggestedParams:f}),I=0===i.id?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:i.amount,suggestedParams:f}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),x=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:O,suggestedParams:f}),h=a.default.assignGroupID([A,m,g,I,x]),t.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function An(){return(An=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,s=n.account,t.next=4,o([r]);case 4:return i=t.sent,c=w(i,3),p=c[0],l=c[1],d=c[2],f=r.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:a.default.signLogicSigTransactionObject(t.txn,s).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hn(){return(hn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.signedTxns,s=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,pe({client:n,pool:r,accountAddr:s});case 5:return c=t.sent,t.next=8,st(n,[o]);case 8:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=it(a),I=ct(a),t.next=17,pe({client:n,pool:r,accountAddr:s});case 17:return x=t.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:I});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new M(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Tn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var o=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(o<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(o,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:o-BigInt(1e3),share:1}}var s=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=si)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Gt(d),priceImpact:Wt(s({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function zn(t){return Jn.apply(this,arguments)}function Jn(){return(Jn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:s.id,assetOutAmount:BigInt(s.amount)+A,excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yn(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap on a non-existent pool");var o,s,u,i=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,s=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,s=n.asset1,u=n.asset2}if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=s*u/(u-i)-s,p=c*qn/(qn-Xn),l=p-c,d=et(a.assetOut,Number(i))/et(a.assetIn,Number(p)),f=et(a.assetOut,Number(u))/et(a.assetIn,Number(s)),m=rt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:o,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function Kn(t){return Hn.apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:s.id,assetOutAmount:BigInt(s.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,r,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,o=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new M({pool:r,swapType:a,txGroup:o},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:o[Cn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:o[Cn.ASSET_IN_TXN_INDEX].txn.amount},l={id:o[Cn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:o[Cn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,zn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Kn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",s(s({},d),{},{groupID:ct(o),fees:it(o)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new M(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Cn||(Cn={}));var $n={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Wn({pool:e,reserves:n,assetIn:r,decimals:a}):Yn({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Wn,getFixedOutputSwapQuote:Yn,generateTxns:function(t){return Gn.apply(this,arguments)},signTxns:function(t){return Un.apply(this,arguments)},execute:function(t){return Zn.apply(this,arguments)},executeFixedOutputSwap:Kn},tr=4*t.ALGORAND_MIN_TX_FEE;function er(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:.003;return Math.ceil(1/t)}function nr(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:.003;return tn&&(e=a,n=o)}return e}function cr(){return(cr=c(u().mark((function t(e){var n,r,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Vt(s(s({},e),{},{route:e.quote.data.route})));case 2:if(n=e.client,r=e.initiatorAddr,o=e.slippage,i=e.swapType,c=e.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(I=[l.asset1ID,l.asset2ID]).includes(f)&&I.includes(m)&&f!==m){t.next=9;break}throw new M({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return x=t.sent,A=j(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:tt("positive",o,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:tt("negative",o,d.assetOutAmount),y=A?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,suggestedParams:x}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,assetIndex:f,suggestedParams:x}),(v=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:l.validatorAppID,appArgs:[Et,bt[i],a.default.encodeUint64(T)],note:mt.getAppCallTxnNoteWithClientName(N.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=pr(i),(w=[])[vt.INPUT_TXN]=y,w[vt.APP_CALL_TXN]=v,_=a.default.assignGroupID(w),t.abrupt("return",[{txn:_[vt.INPUT_TXN],signers:[r]},{txn:_[vt.APP_CALL_TXN],signers:[r]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pr(e){return(St[e]+1)*t.ALGORAND_MIN_TX_FEE}function lr(){return(lr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,o=e.quote,s=e.txGroup,i=e.signedTxns,t.next=3,st(a,[i]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Ge(a,s);case 10:return m=t.sent,g=zt(o),I=Jt(o).id,x=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===I})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:x&&{amount:BigInt(g.amount)-BigInt(x.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:I},quote:o,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dr(){return(dr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.amount,a=e.assetInID,o=e.assetOutID,s=e.decimals,i=e.network,c=e.isSwapRouterEnabled,p=e.pool,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,Ir({assetInID:a,assetOutID:o,amount:r,decimals:s,isSwapRouterEnabled:c,network:i,pool:p});case 4:l=t.sent,t.next=10;break;case 7:return t.next=9,Ar({amount:r,assetInID:a,assetOutID:o,decimals:s,isSwapRouterEnabled:c,network:i,pool:p});case 9:l=t.sent;case 10:return t.abrupt("return",l);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=t[0].reason;if(2===t.length){var n=t[1].reason;if(rr(e)&&!rr(n))throw e;throw n}throw e}return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function mr(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,o=t.pool;if(!Ne.isPoolReady(o))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");if(nr(Number(e)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");var s,u,i=BigInt(e),c=o.totalFeeShare;if(n===o.asset1ID)s=o.asset1Reserves,u=o.asset2Reserves;else{if(n!==o.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Input asset (#".concat(n,") doesn't belong to the pool ").concat(o.account.address(),"."));s=o.asset2Reserves,u=o.asset1Reserves}var p=Tr({inputSupply:s,outputSupply:u,swapInputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapOutputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(l>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");return{assetInID:n,assetInAmount:i,assetOutID:r,assetOutAmount:l,swapFee:Number(d),rate:et(a.assetOut,Number(l))/et(a.assetIn,Number(i)),priceImpact:f}}function gr(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,o=t.pool;if(!o||!Ne.isPoolReady(o))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");var s,u,i=BigInt(e),c=o.totalFeeShare;if(r===o.asset1ID)s=o.asset2Reserves,u=o.asset1Reserves;else{if(r!==o.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r,") doesn't belong to the pool ").concat(o.account.address(),"."));s=o.asset1Reserves,u=o.asset2Reserves}var p=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,o=t.decimals,s=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:s,totalFeeShare:a}),i=s+u,c=Wt({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:o.assetIn},assetOut:{amount:r,decimals:o.assetOut}});return{swapInputAmount:i,totalFeeAmount:u,priceImpact:c}}({inputSupply:s,outputSupply:u,swapOutputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapInputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(nr(Number(l)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");return{type:exports.SwapQuoteType.Direct,data:{pool:o,quote:{assetInID:n,assetInAmount:l,assetOutID:r,assetOutAmount:i,swapFee:Number(d),rate:et(a.assetOut,Number(i))/et(a.assetIn,Number(l)),priceImpact:f}}}}function Ir(t){return xr.apply(this,arguments)}function xr(){return(xr=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[],p?l.push(new Promise((function(t,e){try{var s=mr({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p});t({type:exports.SwapQuoteType.Direct,data:{pool:p,quote:s}})}catch(t){e(t)}}))):l.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools."))),s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedInput,network:i});case 2:if(!((e=t.sent).route.length<2)){t.next=5;break}throw new D(exports.SwapQuoteErrorType.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 5:return t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 6:case"end":return t.stop()}}),t)})))()),t.next=6,fr(l);case 6:return d=t.sent,t.abrupt("return",ir(d));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){return hr.apply(this,arguments)}function hr(){return(hr=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[new Promise((function(t,e){try{t(gr({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p}))}catch(t){e(t)}}))],s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedOutput,network:i});case 2:return e=t.sent,t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 4:case"end":return t.stop()}}),t)})))()),t.next=5,fr(l);case 5:return d=t.sent,t.abrupt("return",ir(d));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Tr(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,o=t.decimals,s=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-s});return{swapOutputAmount:u,totalFeeAmount:s,priceImpact:Wt({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:o.assetIn},assetOut:{amount:u,decimals:o.assetOut}})}}var yr={getQuote:function(t){return dr.apply(this,arguments)},getFixedInputSwapQuote:Ir,getFixedInputDirectSwapQuote:mr,getFixedOutputDirectSwapQuote:gr,getFixedOutputSwapQuote:Ar,generateTxns:function(t){return cr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return lr.apply(this,arguments)},calculateFixedInputSwap:Tr};function vr(t,e){var n,r,a=BigInt(t),o=e.issuedLiquidity;return o>a+BigInt(1e3)?(n=a*e.asset1/o,r=a*e.asset2/o):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.account.address(),m=o.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:s,suggestedParams:d}),(I=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(tt("negative",l,c)),a.default.encodeUint64(tt("negative",l,p))],accounts:[f],foreignAssets:[o.asset1ID,o.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(x=[])[Rn.ASSET_TRANSFER_TXN]=g,x[Rn.APP_CALL_TXN]=I,A=a.default.assignGroupID(x),e.abrupt("return",[{txn:A[Rn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[Rn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function _r(){return(_r=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,v,w;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.asset1ID,m=o.asset2ID,g=o.account.address(),I=o.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(x=0,A=0,h=tt("negative",l,p),c!==f){e.next=17;break}x=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}x=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,assetIndex:I,amount:i,suggestedParams:d}),(y=a.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(x),a.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[Rn.ASSET_TRANSFER_TXN]=T,v[Rn.APP_CALL_TXN]=y,w=a.default.assignGroupID(v),e.abrupt("return",[{txn:w[Rn.ASSET_TRANSFER_TXN],signers:[s]},{txn:w[Rn.APP_CALL_TXN],signers:[s]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Sr(){return(Sr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,o=e.signedTxns,t.next=3,st(r,[o]);case 3:return s=t.sent,i=w(s,1),c=i[0].txnID,t.next=8,Ge(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Er,br,Dr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=vr(a,n),s=o.asset1OutputAmount,u=o.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:s},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,o=t.assetOutID,s=t.decimals,u=BigInt(a),i=vr(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(o===n.asset1ID){var d=yr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:s}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(o!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var I=yr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:s}),x=I.swapOutputAmount,A=I.totalFeeAmount,h=I.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return _r.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Sr.apply(this,arguments)}},Or=(f(Er={},N.V1_1,jn),f(Er,N.V2,Dr),Er),Nr=(f(br={},N.V1_1,$n),f(br,N.V2,yr),f(br,"getQuote",(function(t){var e=t.type,n=t.pools;if(!t.isSwapRouterEnabled&&n.every((function(t){return Ee(t.reserves)})))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");if(e===exports.SwapType.FixedInput)return function(t){return or.apply(this,arguments)}(t);if(e===exports.SwapType.FixedOutput)return function(t){return sr.apply(this,arguments)}(t);throw new D(exports.SwapQuoteErrorType.InvalidSwapTypeError,"Invalid swap type")})),f(br,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1?$n.generateTxns(s(s({},t),{},{quote:t.quote.data})):yr.generateTxns(t)})),f(br,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1){var e=t.quote.data.pool;return $n.signTxns(s({pool:e},t))}return yr.signTxns(t)})),f(br,"execute",(function(t){return t.contractVersion===N.V1_1?$n.execute(t):yr.execute(t)})),br);function Pr(){return(Pr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,kr({txGroup:a,pool:r,initiatorSigner:o});case 4:return s=t.sent,t.next=7,st(n,[s]);case 7:return i=t.sent,c=w(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:it(a),confirmedRound:d,txnID:l,groupID:ct(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function kr(t){return Lr.apply(this,arguments)}function Lr(){return(Lr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.pool,o=e.initiatorSigner,t.next=3,o([n]);case 3:return s=t.sent,i=w(s,1),c=i[0],p=r.account.lsig,l=n.map((function(t,e){return 0===e?c:a.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rr(){return Rr=c(u().mark((function t(e){var n,r,o,s,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.data,o=e.initiatorSigner,t.prev=1,s=r.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:it(e),groupID:ct(e),lsig:n.account.lsig}})),t.next=5,o(s.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(s.map((function(t,e){return new Promise(function(){var r=c(u().mark((function r(o,s){var c,p,l,d,f,m;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,c=t.txns.map((function(n,r){return 0===r?i[e]:a.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),r.next=4,st(n,[c]);case 4:p=r.sent,l=w(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),r.next=15;break;case 12:r.prev=12,r.t0=r.catch(0),s(r.t0);case 15:case"end":return r.stop()}}),r,null,[[0,12]])})));return function(t,e){return r.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Rr.apply(this,arguments)}function Fr(){return(Fr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,o=e.assetID,s=e.assetOut,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=r.account.address(),l=a.default.makeApplicationNoOpTxnFromObject({from:p,appIndex:r.validatorAppID,appArgs:[lt("redeem")],note:mt.getAppCallTxnNoteWithClientName(r.contractVersion),accounts:[i],foreignAssets:0==r.asset2ID?[r.asset1ID,r.poolTokenID]:[r.asset1ID,r.asset2ID,r.poolTokenID],suggestedParams:c}),d=0===o?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:i,amount:BigInt(s),suggestedParams:c}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:i,assetIndex:o,amount:BigInt(s),suggestedParams:c}),f=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:p,amount:l.fee+d.fee,note:O,suggestedParams:c}),m=a.default.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[i]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,o=e.liquidityAssetID,s=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(s.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[lt("commit"),i],note:U([lt("tinymanStaking/v1:b"),c,t.encodeUint64(o),i])})}function Mr(){return(Mr=c(u().mark((function e(n){var r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,s=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Br({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof s){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[lt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=P,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Ln,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=en,exports.CONTRACT_VERSION=N,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=k,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Or,exports.Swap=Nr,exports.SwapQuoteError=D,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Fn,exports.V2AddLiquidityTxnIndices=cn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=tt,exports.calculateAccountMinimumRequiredBalance=se,exports.calculatePriceImpact=Wt,exports.calculateSwapRate=Gt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=Ne,exports.prepareCommitTransactions=function(t){return Mr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Rr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Pr.apply(this,arguments)},exports.sendAndWaitRawTransaction=st,exports.sumUpTxnFees=it,exports.tinymanContract_v1_1=$t,exports.tinymanContract_v2=ne,exports.tinymanJSSDKConfig=mt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",o=r.asyncIterator||"@@asyncIterator",s=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,o=Object.create(a.prototype),s=new _(r||[]);return o._invoke=function(t,e,n){var r="suspendedStart";return function(a,o){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw o;return E()}for(n.method=a,n.arg=o;;){var s=n.delegate;if(s){var u=y(s,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,s),o}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var I=Object.getPrototypeOf,x=I&&I(I(S([])));x&&x!==e&&n.call(x,a)&&(g=x);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,o,s,u){var i=p(t[a],t,o);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,s,u)}),(function(t){r("throw",t,s,u)})):e.resolve(l).then((function(t){c.value=t,s(c)}),(function(t){return r("throw",t,s,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function o(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(o,o):o()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function w(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function v(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function _(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(w,this),this.reset(!0)}function S(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,o=function e(){for(;++r=0;--a){var o=this.tryEntries[a],s=o.completion;if("root"===o.tryLoc)return r("end");if(o.tryLoc<=this.prev){var u=n.call(o,"catchLoc"),i=n.call(o,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),v(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;v(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:S(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,o,s){try{var u=t[o](s),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function s(t){i(o,r,a,s,u,"next",t)}function u(t){i(o,r,a,s,u,"throw",t)}s(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,s=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){u=!0,o=t},f:function(){try{s||null==n.return||n.return()}finally{if(u)throw o}}}}exports.SwapQuoteErrorType=void 0,(r=exports.SwapQuoteErrorType||(exports.SwapQuoteErrorType={})).SwapRouterStaleDataError="SwapRouterStaleDataError",r.SwapRouterNoRouteError="SwapRouterNoRouteError",r.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",r.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",r.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",r.NoAvailablePoolError="NoAvailablePoolError",r.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",r.UnknownError="UnknownError",r.LowSwapAmountError="LowSwapAmountError",r.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",r.InvalidSwapTypeError="InvalidSwapTypeError";var D=function(t){m(n,h(Error));var e=w(n);function n(t,r){var a;return p(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(T(a),n),a}return d(n)}(),O=Uint8Array.from([1]),N={V1_1:"v1_1",V2:"v2"},P={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},k={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},L="- would result negative",R="logic eval error:",F="exceeds schema integer count",B=/transaction \w+:/,M=function(t){m(n,h(Error));var e=w(n);function n(t,r){var a;p(this,n);for(var o=arguments.length,s=new Array(o>2?o-2:0),u=2;ur?[s(s({},t),{},{id:n}),s(s({},e),{},{id:r})]:[s(s({},e),{},{id:r}),s(s({},t),{},{id:n})]}function C(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function j(t){return 0===Number(t)}function X(t){return Number(t.id)}function q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,o=void 0!==a&&a,s={},u=b(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}s[o?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return s}function U(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,o=b(t);try{for(o.s();!(e=o.n()).done;){var s=e.value;r.set(s,a),a+=s.length}}catch(t){o.e(t)}finally{o.f()}return r}var G=100000n,W=100000n,z=100000n,J=25000n+25000n,Y=25000n+3500n;function K(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],o=t["created-apps"]||[],s=t.assets||[];return G+W*BigInt(s.length)+z*BigInt(o.length+a.length)+Y*r+J*n}function H(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,H(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function tt(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function et(t,e){var n=Number(t);return rt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function nt(t,e){return rt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function rt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(ot(e),2),o=a[0],s=a[1],u=v(ot(Math.round(Number(at(o,s+r)))),2),i=u[0],c=u[1];return Number(at(i,c-r))}return Math.round(e)}function at(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ot(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function st(t,e){return ut.apply(this,arguments)}function ut(){return(ut=c(u().mark((function t(e,n){var r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=b(n),t.prev=3,a.s();case 5:if((o=a.n()).done){t.next=18;break}return s=o.value,t.next=9,e.sendRawTransaction(s).do();case 9:return i=t.sent,c=i.txId,t.next=13,Z(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new M(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function it(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ct(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function pt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function lt(t){return(new TextEncoder).encode(t)}function dt(t){return Boolean(t)&&void 0!==t.fallback_message}var ft,mt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return lt("tinyman/".concat(t===N.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),gt=(f(ft={},N.V1_1,{testnet:62368684,mainnet:552635992}),f(ft,N.V2,{testnet:148607e3,mainnet:1002541853}),ft);function It(t,e){var n=gt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function xt(){return(xt=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationOptInTxnFromObject({from:s,appIndex:It(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var At;function ht(){return(ht=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationClearStateTxnFromObject({from:s,appIndex:It(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(At=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",At.FixedOutput="fixed-output";var Tt,yt,wt;!function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(wt||(wt={}));var vt,_t,St=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),Et=lt("swap"),bt=(f(yt={},exports.SwapType.FixedInput,lt("fixed-input")),f(yt,exports.SwapType.FixedOutput,lt("fixed-output")),yt),Dt={ASSET_OPT_IN:lt("asset_opt_in")},Ot={testnet:159521633,mainnet:0},Nt=(f(vt={},exports.SwapType.FixedInput,7),f(vt,exports.SwapType.FixedOutput,8),vt),Pt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function kt(t){var e=Bt(t),n=e.assetIn,r=e.assetOut;return et(r.asset.decimals,Number(r.amount))/et(n.asset.decimals,Number(n.amount))}function Lt(t){if(!Ot[t])throw new Error("Unknown network or network not supported: ".concat(t));return Ot[t]}function Rt(t){return t[t.length-1].quote.amount_out}function Ft(t){return t[0].quote.amount_in}function Bt(t){return{assetIn:Ft(t),assetOut:Rt(t)}}function Mt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function e(n){var r,o,s,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.routerAppID,s=n.assetIDs,i=n.accountAddress,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o,appArgs:[Dt.ASSET_OPT_IN],foreignAssets:s,suggestedParams:c}),l=s.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v,_,S,E,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,o=n.client,s=n.network,i=n.swapType,c=n.route,e.next=3,o.getTransactionParams().do();case 3:return p=e.sent,l=[X(c[0].quote.amount_in.asset),X(c[0].quote.amount_out.asset),X(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Ft(c).amount),Number(Rt(c).amount)],I=g[0],x=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=j(d),w=Lt(s),v=y?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(w),amount:I,suggestedParams:p}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(w),amount:I,assetIndex:d,suggestedParams:p}),(_=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:w,appArgs:[Et,bt[i],a.default.encodeUint64(x)],foreignApps:[It(s,N.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Nt[i]+1),S=[v,_],e.next=15,jt({client:o,routerAppID:w,assetIDs:[d,f,m]});case 15:if(!((E=e.sent).length>0)){e.next=21;break}return e.next=19,Mt({client:o,accountAddress:r,assetIDs:E,routerAppID:w});case 19:b=e.sent,S.unshift(b);case 21:return D=a.default.assignGroupID(S),e.abrupt("return",D.map((function(t){return{txn:t,signers:[r]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(u().mark((function e(n){var r,a,o,s,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,o=n.assetIDs,s=t.getApplicationAddress(a),e.next=4,r.accountInformation(s).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=o.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qt(t){return Ut.apply(this,arguments)}function Ut(){return(Ut=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.swapType,s=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Pt[s].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(!dt(p)){t.next=13;break}throw new D(p.type,p.fallback_message);case 13:throw new D(exports.SwapQuoteErrorType.UnknownError,"Unknown error");case 14:return t.abrupt("return",p);case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Gt(t){var e=t.assetIn,n=t.assetOut;return et(n.decimals,Number(n.amount))/et(e.decimals,Number(e.amount))}function Wt(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,o=Gt({assetIn:r,assetOut:a}),s=et(a.decimals,Number(n))/et(r.decimals,Number(e));return rt({decimalPlaces:5},Math.abs(o/s-1))}function zt(t){if(t.type===exports.SwapQuoteType.Router){var e=Ft(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function Jt(t){if(t.type===exports.SwapQuoteType.Router){var e=Rt(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function Yt(t){return t.type===exports.SwapQuoteType.Direct?t.data.pool.contractVersion:N.V2}exports.SwapQuoteType=void 0,(_t=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",_t.Router="router";var Kt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Ht={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Zt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),$t=new(function(n){m(a,Zt);var r=w(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=It(n.network,N.V1_1),a=v(C(n.asset1ID,n.asset2ID),2),o=a[0],s=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:o,asset_id_2:s,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(s(s({},i),{},{minimum_required_balance:se(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),o(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function oe(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function se(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ue,ie,ce=lt("e");function pe(t){return le.apply(this,arguments)}function le(){return(le=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v,_,S,E;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.pool,i=r.accountAddr,n.next=3,o.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=s.account.address(),g=b(p),n.prev=10,g.s();case 12:if((I=g.n()).done){n.next=31;break}if((x=I.value).id==s.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=x["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=q({stateArray:A}),T=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.asset1ID)])),y=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.asset2ID)])),w=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.poolTokenID)])),v=h[T],_=h[y],S=h[w],"bigint"==typeof v&&(l=v),"bigint"==typeof _&&(d=_),"bigint"==typeof S&&(f=S);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((E={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function de(){return(de=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,I,x,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.accountAddr,i=r.validatorAppID,n.next=3,o.accountInformation(s).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mge)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ge,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function he(){return he=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ae(n,r);case 6:return s=t.sent,i=oe(s,It(a,N.V1_1)),c=null,i&&(l=s["created-assets"][0],p=l.index,c={asset1ID:i[fe[N.V1_1].asset1],asset2ID:i[fe[N.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),he.apply(this,arguments)}var Te=Object.freeze({__proto__:null,getPoolInfo:Ie,getPoolReserves:function(t,e){return Ae.apply(this,arguments)},getPoolAssets:function(t){return he.apply(this,arguments)}});function ye(t){return we.apply(this,arguments)}function we(){return(we=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,o=e.asset2ID,s=re(N.V2),i=s.generateLogicSigAccountForPool(e),c=It(r,N.V2),p=i.address(),l=C(a,o),t.next=8,ae(n,p);case 8:return d=t.sent,f=oe(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:N.V2},f&&(m.asset1ProtocolFees=BigInt(f[fe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[fe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[fe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[fe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[fe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[fe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[fe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[fe.v2.totalFeeShare]),m.poolTokenID=Number(f[fe.v2.poolTokenID]),m.asset1ID=Number(f[fe.v2.asset1]),m.asset2ID=Number(f[fe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ve(){return(ve=c(u().mark((function t(e,n){var r,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ae(e,n.account.address());case 2:return r=t.sent,a=oe(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[fe.v2.asset1Reserves]),o.asset2=BigInt(a[fe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[fe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(){return(_e=c(u().mark((function t(e){var n,r,a,o,s,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,ae(n,r);case 3:return o=t.sent,s=oe(o,It(a,N.V2)),i=null,s&&(i={asset1ID:s[fe[N.V2].asset1],asset2ID:s[fe[N.V2].asset2],poolTokenID:s[fe[N.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Se=Object.freeze({__proto__:null,getPoolInfo:ye,getPoolReserves:function(t,e){return ve.apply(this,arguments)},getPoolAssets:function(t){return _e.apply(this,arguments)}});function Ee(t){return Boolean(t&&!(t.asset1+t.asset2))}var be,De,Oe=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Ee(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=et(t.asset1,e.asset1)/et(t.asset2,e.asset2)),r},isPoolEmpty:Ee,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Ie(t),ye(t)])}}),Ne=s((f(be={},N.V1_1,s(s({},Te),Oe)),f(be,N.V2,s(s({},Se),Oe)),be),Oe);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(De||(De={}));var Pe=96e4,ke=859e3;function Le(){return(Le=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,_,S,E,b,D,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.asset_1,s=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=V(o,s),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],I=g.id,x=g.unit_name,A=j(I),h=It(r,N.V1_1),T=$t.generateLogicSigAccountForPool({network:r,asset1ID:f,asset2ID:I}),y=T.address(),w=a.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:mt.getAppCallTxnNoteWithClientName(N.V1_1),appArgs:[lt("bootstrap"),a.default.encodeUint64(f),a.default.encodeUint64(I)],foreignAssets:A?[f]:[I],suggestedParams:c}),_=a.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:k.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),S=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Re(A),suggestedParams:c}),(b=[])[De.FUNDING_TXN]=E,b[De.VALIDATOR_APP_CALL]=w,b[De.POOL_TOKEN_CREATE]=_,b[De.ASSET1_OPT_IN]=S,A||(b[De.ASSET2_OPT_IN]=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:I,amount:0,suggestedParams:c})),D=a.default.assignGroupID(b),O=[{txn:D[De.FUNDING_TXN],signers:[i]},{txn:D[De.VALIDATOR_APP_CALL],signers:[y]},{txn:D[De.POOL_TOKEN_CREATE],signers:[y]},{txn:D[De.ASSET1_OPT_IN],signers:[y]}],D[De.ASSET2_OPT_IN]&&O.push({txn:D[De.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Re(t){return t?Pe:ke}function Fe(){return(Fe=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=C(s,i),f=v(d,2),m=f[0],g=f[1],I=$t.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===De.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,o=n.blob;return x.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(t){return Me.apply(this,arguments)}function Me(){return(Me=c(u().mark((function t(e){var n,r,a,o,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Z(n,a[De.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(s=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(s));case 10:return t.abrupt("return",{poolTokenID:s});case 13:throw t.prev=13,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Qe(){return(Qe=c(u().mark((function t(e){var n,r,a,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Be({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",Ne.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ve,Ce={generateTxns:function(t){return Le.apply(this,arguments)},signTxns:function(t){return Fe.apply(this,arguments)},execute:function(t){return Qe.apply(this,arguments)},getBootstrapFundingTxnAmount:Re};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ve||(Ve={}));var je=5,Xe=6;function qe(t,e){return Ue.apply(this,arguments)}function Ue(){return(Ue=c(u().mark((function e(n,r){var a,o,s;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return s=e.t0,e.abrupt("return",s);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ge(t,e){return We.apply(this,arguments)}function We(){return(We=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qe(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ze(){return(ze=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,_,S;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.network,s=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return p=e.sent,l=It(o,N.V2),d=t.getApplicationAddress(l),f=V(s,i),m=v(f,2),g=m[0].id,I=m[1].id,e.next=9,Ne.v2.getPoolInfo({client:r,network:o,asset1ID:g,asset2ID:I});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(s.unit_name,"-").concat(i.unit_name," already exists"));case 12:return x=ne.generateLogicSigAccountForPool({network:o,asset1ID:g,asset2ID:I}),A=x.address(),h=j(I),(T=a.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[lt("bootstrap")],note:mt.getAppCallTxnNoteWithClientName(N.V2),foreignAssets:[g,I],rekeyTo:d,suggestedParams:p})).fee=Ye(h),y=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Je(h),suggestedParams:p}),(w=[])[Ve.FUNDING_TXN]=y,w[Ve.VALIDATOR_APP_CALL]=T,_=a.default.assignGroupID(w),(S=[])[Ve.FUNDING_TXN]={txn:_[Ve.FUNDING_TXN],signers:[c]},S[Ve.VALIDATOR_APP_CALL]={txn:_[Ve.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",S);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Je(t){return function(t,e){var n=re(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(N.V2,t)+Ye(t)+1e5}function Ye(e){return((e?je:Xe)+1)*t.ALGORAND_MIN_TX_FEE}function Ke(){return(Ke=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=C(s,i),f=v(d,2),m=f[0],g=f[1],I=ne.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===Ve.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,o=n.blob;return x.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function He(){return(He=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,qe(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(fe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Ne.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 21:throw t.prev=21,t.t3=t.catch(1),new M(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Ze,$e={generateTxns:function(t){return ze.apply(this,arguments)},signTxns:function(t){return Ke.apply(this,arguments)},execute:function(t){return He.apply(this,arguments)},getBootstrapFundingTxnAmount:Je,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Je(e)}};var tn,en=(f(Ze={},N.V1_1,Ce),f(Ze,N.V2,$e),f(Ze,"generateTxns",(function(t){return t.contractVersion===N.V1_1?Ce.generateTxns(t):$e.generateTxns(t)})),f(Ze,"signTxns",(function(t){return t.contractVersion===N.V1_1?Ce.signTxns(t):$e.signTxns(t)})),f(Ze,"execute",(function(t){return t.contractVersion===N.V1_1?Ce.execute(t):$e.execute(t)})),f(Ze,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===N.V1_1?Ce.getBootstrapFundingTxnAmount(n):$e.getBootstrapFundingTxnAmount(n)})),Ze);exports.V1_1AddLiquidityTxnIndices=void 0,(tn=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[tn.FEE_TXN=0]="FEE_TXN",tn[tn.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",tn[tn.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",tn[tn.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",tn[tn.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var nn,rn,an,on,sn=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(on=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",on.FLEXIBLE="flexible",on.INITIAL="initial";var un,cn=(f(nn={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(nn,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(nn,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),nn),pn=(f(rn={},exports.V2AddLiquidityType.INITIAL,1),f(rn,exports.V2AddLiquidityType.SINGLE,2),f(rn,exports.V2AddLiquidityType.FLEXIBLE,2),rn),ln=(f(an={},exports.V2AddLiquidityType.INITIAL,3),f(an,exports.V2AddLiquidityType.FLEXIBLE,3),f(an,exports.V2AddLiquidityType.SINGLE,2),an);function dn(t){var e,n,r,a=t.reserves,o=t.totalFeeShare,s=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(s.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,I=BigInt(s.amount)-m,x=BigInt(u.amount)-g;if(I>x){var A=I;r=fn(A,o),e={id:s.id,amount:A+r,decimals:s.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=x;r=fn(h,o),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:s.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:s.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Wt({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function fn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function mn(e){return(pn[e]+1)*t.ALGORAND_MIN_TX_FEE}var gn=lt("add_liquidity"),In=(f(un={},N.V1_1,[lt("mint")]),f(un,N.V2,{INITIAL_LIQUIDITY:[lt("add_initial_liquidity")],SINGLE_ASSET_MODE:[gn,lt("single")],FLEXIBLE_MODE:[gn,lt("flexible")]}),un);function xn(){return(xn=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.poolAddress,s=e.asset1In,i=e.asset2In,c=e.poolTokenOut,p=e.slippage,l=e.initiatorAddr,d=tt("negative",p,c.amount),t.next=4,n.getTransactionParams().do();case 4:return f=t.sent,m=a.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:It(r,N.V1_1),appArgs:In.v1_1,accounts:[l],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==i.id?[s.id,c.id]:[s.id,i.id,c.id],suggestedParams:f}),g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:s.id,amount:s.amount,suggestedParams:f}),I=0===i.id?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:i.amount,suggestedParams:f}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),x=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:O,suggestedParams:f}),h=a.default.assignGroupID([A,m,g,I,x]),t.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function An(){return(An=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,s=n.account,t.next=4,o([r]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=r.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:a.default.signLogicSigTransactionObject(t.txn,s).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hn(){return(hn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.signedTxns,s=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,pe({client:n,pool:r,accountAddr:s});case 5:return c=t.sent,t.next=8,st(n,[o]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=it(a),I=ct(a),t.next=17,pe({client:n,pool:r,accountAddr:s});case 17:return x=t.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:I});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new M(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Tn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var o=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(o<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(o,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:o-BigInt(1e3),share:1}}var s=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=si)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Gt(d),priceImpact:Wt(s({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function zn(t){return Jn.apply(this,arguments)}function Jn(){return(Jn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:s.id,assetOutAmount:BigInt(s.amount)+A,excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yn(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap on a non-existent pool");var o,s,u,i=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,s=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,s=n.asset1,u=n.asset2}if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=s*u/(u-i)-s,p=c*qn/(qn-Xn),l=p-c,d=et(a.assetOut,Number(i))/et(a.assetIn,Number(p)),f=et(a.assetOut,Number(u))/et(a.assetIn,Number(s)),m=rt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:o,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function Kn(t){return Hn.apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:s.id,assetOutAmount:BigInt(s.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,r,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,o=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new M({pool:r,swapType:a,txGroup:o},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:o[Cn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:o[Cn.ASSET_IN_TXN_INDEX].txn.amount},l={id:o[Cn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:o[Cn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,zn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Kn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",s(s({},d),{},{groupID:ct(o),fees:it(o)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new M(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Cn||(Cn={}));var $n={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Wn({pool:e,reserves:n,assetIn:r,decimals:a}):Yn({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Wn,getFixedOutputSwapQuote:Yn,generateTxns:function(t){return Gn.apply(this,arguments)},signTxns:function(t){return Un.apply(this,arguments)},execute:function(t){return Zn.apply(this,arguments)},executeFixedOutputSwap:Kn},tr=4*t.ALGORAND_MIN_TX_FEE;function er(e){return(St[e]+2)*t.ALGORAND_MIN_TX_FEE}function nr(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:.003;return Math.ceil(1/t)}function rr(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:.003;return tn&&(e=a,n=o)}return e}function pr(){return(pr=c(u().mark((function t(e){var n,r,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Vt(s(s({},e),{},{route:e.quote.data.route})));case 2:if(n=e.client,r=e.initiatorAddr,o=e.slippage,i=e.swapType,c=e.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(I=[l.asset1ID,l.asset2ID]).includes(f)&&I.includes(m)&&f!==m){t.next=9;break}throw new M({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return x=t.sent,A=j(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:tt("positive",o,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:tt("negative",o,d.assetOutAmount),y=A?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,suggestedParams:x}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,assetIndex:f,suggestedParams:x}),(w=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:l.validatorAppID,appArgs:[Et,bt[i],a.default.encodeUint64(T)],note:mt.getAppCallTxnNoteWithClientName(N.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=lr(i),(v=[])[wt.INPUT_TXN]=y,v[wt.APP_CALL_TXN]=w,_=a.default.assignGroupID(v),t.abrupt("return",[{txn:_[wt.INPUT_TXN],signers:[r]},{txn:_[wt.APP_CALL_TXN],signers:[r]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function lr(e){return(St[e]+1)*t.ALGORAND_MIN_TX_FEE}function dr(){return(dr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,o=e.quote,s=e.txGroup,i=e.signedTxns,t.next=3,st(a,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Ge(a,s);case 10:return m=t.sent,g=zt(o),I=Jt(o).id,x=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===I})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:x&&{amount:BigInt(g.amount)-BigInt(x.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:I},quote:o,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fr(){return(fr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.amount,a=e.assetInID,o=e.assetOutID,s=e.decimals,i=e.network,c=e.isSwapRouterEnabled,p=e.pool,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,xr({assetInID:a,assetOutID:o,amount:r,decimals:s,isSwapRouterEnabled:c,network:i,pool:p});case 4:l=t.sent,t.next=10;break;case 7:return t.next=9,hr({amount:r,assetInID:a,assetOutID:o,decimals:s,isSwapRouterEnabled:c,network:i,pool:p});case 9:l=t.sent;case 10:return t.abrupt("return",l);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=t[0].reason;if(2===t.length){var n=t[1].reason;if(ar(e)&&!ar(n))throw e;throw n}throw e}return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function gr(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,o=t.pool;if(!Ne.isPoolReady(o))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");if(rr(Number(e)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");var s,u,i=BigInt(e),c=o.totalFeeShare;if(n===o.asset1ID)s=o.asset1Reserves,u=o.asset2Reserves;else{if(n!==o.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Input asset (#".concat(n,") doesn't belong to the pool ").concat(o.account.address(),"."));s=o.asset2Reserves,u=o.asset1Reserves}var p=yr({inputSupply:s,outputSupply:u,swapInputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapOutputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(l>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");return{assetInID:n,assetInAmount:i,assetOutID:r,assetOutAmount:l,swapFee:Number(d),rate:et(a.assetOut,Number(l))/et(a.assetIn,Number(i)),priceImpact:f}}function Ir(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,o=t.pool;if(!o||!Ne.isPoolReady(o))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");var s,u,i=BigInt(e),c=o.totalFeeShare;if(r===o.asset1ID)s=o.asset2Reserves,u=o.asset1Reserves;else{if(r!==o.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r,") doesn't belong to the pool ").concat(o.account.address(),"."));s=o.asset1Reserves,u=o.asset2Reserves}var p=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,o=t.decimals,s=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:s,totalFeeShare:a}),i=s+u,c=Wt({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:o.assetIn},assetOut:{amount:r,decimals:o.assetOut}});return{swapInputAmount:i,totalFeeAmount:u,priceImpact:c}}({inputSupply:s,outputSupply:u,swapOutputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapInputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(rr(Number(l)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");return{type:exports.SwapQuoteType.Direct,data:{pool:o,quote:{assetInID:n,assetInAmount:l,assetOutID:r,assetOutAmount:i,swapFee:Number(d),rate:et(a.assetOut,Number(i))/et(a.assetIn,Number(l)),priceImpact:f}}}}function xr(t){return Ar.apply(this,arguments)}function Ar(){return(Ar=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[],p?l.push(new Promise((function(t,e){try{var s=gr({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p});t({type:exports.SwapQuoteType.Direct,data:{pool:p,quote:s}})}catch(t){e(t)}}))):l.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools."))),s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedInput,network:i});case 2:if(!((e=t.sent).route.length<2)){t.next=5;break}throw new D(exports.SwapQuoteErrorType.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 5:return t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 6:case"end":return t.stop()}}),t)})))()),t.next=6,mr(l);case 6:return d=t.sent,t.abrupt("return",cr(d));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hr(t){return Tr.apply(this,arguments)}function Tr(){return(Tr=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[new Promise((function(t,e){try{t(Ir({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p}))}catch(t){e(t)}}))],s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedOutput,network:i});case 2:return e=t.sent,t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 4:case"end":return t.stop()}}),t)})))()),t.next=5,mr(l);case 5:return d=t.sent,t.abrupt("return",cr(d));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,o=t.decimals,s=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-s});return{swapOutputAmount:u,totalFeeAmount:s,priceImpact:Wt({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:o.assetIn},assetOut:{amount:u,decimals:o.assetOut}})}}var wr={getQuote:function(t){return fr.apply(this,arguments)},getFixedInputSwapQuote:xr,getFixedInputDirectSwapQuote:gr,getFixedOutputDirectSwapQuote:Ir,getFixedOutputSwapQuote:hr,generateTxns:function(t){return pr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return dr.apply(this,arguments)},calculateFixedInputSwap:yr};function vr(t,e){var n,r,a=BigInt(t),o=e.issuedLiquidity;return o>a+BigInt(1e3)?(n=a*e.asset1/o,r=a*e.asset2/o):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function _r(){return(_r=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.account.address(),m=o.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:s,suggestedParams:d}),(I=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(tt("negative",l,c)),a.default.encodeUint64(tt("negative",l,p))],accounts:[f],foreignAssets:[o.asset1ID,o.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(x=[])[Rn.ASSET_TRANSFER_TXN]=g,x[Rn.APP_CALL_TXN]=I,A=a.default.assignGroupID(x),e.abrupt("return",[{txn:A[Rn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[Rn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Sr(){return(Sr=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.asset1ID,m=o.asset2ID,g=o.account.address(),I=o.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(x=0,A=0,h=tt("negative",l,p),c!==f){e.next=17;break}x=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}x=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,assetIndex:I,amount:i,suggestedParams:d}),(y=a.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(x),a.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(w=[])[Rn.ASSET_TRANSFER_TXN]=T,w[Rn.APP_CALL_TXN]=y,v=a.default.assignGroupID(w),e.abrupt("return",[{txn:v[Rn.ASSET_TRANSFER_TXN],signers:[s]},{txn:v[Rn.APP_CALL_TXN],signers:[s]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Er(){return(Er=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,o=e.signedTxns,t.next=3,st(r,[o]);case 3:return s=t.sent,i=v(s,1),c=i[0].txnID,t.next=8,Ge(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var br,Dr,Or={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=vr(a,n),s=o.asset1OutputAmount,u=o.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:s},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,o=t.assetOutID,s=t.decimals,u=BigInt(a),i=vr(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(o===n.asset1ID){var d=wr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:s}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(o!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var I=wr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:s}),x=I.swapOutputAmount,A=I.totalFeeAmount,h=I.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return _r.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Sr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Er.apply(this,arguments)}},Nr=(f(br={},N.V1_1,jn),f(br,N.V2,Or),br),Pr=(f(Dr={},N.V1_1,$n),f(Dr,N.V2,wr),f(Dr,"getQuote",(function(t){var e=t.type,n=t.pools;if(!t.isSwapRouterEnabled&&n.every((function(t){return Ee(t.reserves)})))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");if(e===exports.SwapType.FixedInput)return function(t){return sr.apply(this,arguments)}(t);if(e===exports.SwapType.FixedOutput)return function(t){return ur.apply(this,arguments)}(t);throw new D(exports.SwapQuoteErrorType.InvalidSwapTypeError,"Invalid swap type")})),f(Dr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1?$n.generateTxns(s(s({},t),{},{quote:t.quote.data})):wr.generateTxns(t)})),f(Dr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1){var e=t.quote.data.pool;return $n.signTxns(s({pool:e},t))}return wr.signTxns(t)})),f(Dr,"execute",(function(t){return t.contractVersion===N.V1_1?$n.execute(t):wr.execute(t)})),Dr);function kr(){return(kr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,Lr({txGroup:a,pool:r,initiatorSigner:o});case 4:return s=t.sent,t.next=7,st(n,[s]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:it(a),confirmedRound:d,txnID:l,groupID:ct(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function Lr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.pool,o=e.initiatorSigner,t.next=3,o([n]);case 3:return s=t.sent,i=v(s,1),c=i[0],p=r.account.lsig,l=n.map((function(t,e){return 0===e?c:a.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Fr(){return Fr=c(u().mark((function t(e){var n,r,o,s,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.data,o=e.initiatorSigner,t.prev=1,s=r.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:it(e),groupID:ct(e),lsig:n.account.lsig}})),t.next=5,o(s.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(s.map((function(t,e){return new Promise(function(){var r=c(u().mark((function r(o,s){var c,p,l,d,f,m;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,c=t.txns.map((function(n,r){return 0===r?i[e]:a.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),r.next=4,st(n,[c]);case 4:p=r.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),r.next=15;break;case 12:r.prev=12,r.t0=r.catch(0),s(r.t0);case 15:case"end":return r.stop()}}),r,null,[[0,12]])})));return function(t,e){return r.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Fr.apply(this,arguments)}function Br(){return(Br=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,o=e.assetID,s=e.assetOut,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=r.account.address(),l=a.default.makeApplicationNoOpTxnFromObject({from:p,appIndex:r.validatorAppID,appArgs:[lt("redeem")],note:mt.getAppCallTxnNoteWithClientName(r.contractVersion),accounts:[i],foreignAssets:0==r.asset2ID?[r.asset1ID,r.poolTokenID]:[r.asset1ID,r.asset2ID,r.poolTokenID],suggestedParams:c}),d=0===o?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:i,amount:BigInt(s),suggestedParams:c}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:i,assetIndex:o,amount:BigInt(s),suggestedParams:c}),f=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:p,amount:l.fee+d.fee,note:O,suggestedParams:c}),m=a.default.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[i]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Mr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,o=e.liquidityAssetID,s=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(s.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[lt("commit"),i],note:U([lt("tinymanStaking/v1:b"),c,t.encodeUint64(o),i])})}function Qr(){return(Qr=c(u().mark((function e(n){var r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,s=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Mr({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof s){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[lt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=P,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Ln,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=en,exports.CONTRACT_VERSION=N,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=k,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Nr,exports.Swap=Pr,exports.SwapQuoteError=D,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Fn,exports.V2AddLiquidityTxnIndices=cn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=tt,exports.calculateAccountMinimumRequiredBalance=se,exports.calculatePriceImpact=Wt,exports.calculateSwapRate=Gt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=rr,exports.poolUtils=Ne,exports.prepareCommitTransactions=function(t){return Qr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Fr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return kr.apply(this,arguments)},exports.sendAndWaitRawTransaction=st,exports.sumUpTxnFees=it,exports.tinymanContract_v1_1=$t,exports.tinymanContract_v2=ne,exports.tinymanJSSDKConfig=mt; diff --git a/src/index.ts b/src/index.ts index 4c491f8d..862e6ec8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -118,6 +118,7 @@ export type { GenerateSwapTxnsParams } from "./swap/types"; export {getSwapTotalFee} from "./swap/utils"; +export * from "./swap/v2/util"; export {SwapType} from "./swap/constants"; export {Swap} from "./swap"; // eslint-disable-next-line no-duplicate-imports From 9929318ad501bcf1f4e38b197d51d800320f2f73 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Mon, 20 Mar 2023 16:09:09 +0300 Subject: [PATCH 25/39] chore(swap): fix error hierarchy --- dist/index.js | 2 +- src/swap/v2/index.ts | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/dist/index.js b/dist/index.js index f752d231..3bc6d3cc 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r,a=n(t);function o(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function s(t){for(var e=1;e=0;--a){var o=this.tryEntries[a],s=o.completion;if("root"===o.tryLoc)return r("end");if(o.tryLoc<=this.prev){var u=n.call(o,"catchLoc"),i=n.call(o,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),v(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;v(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:S(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,o,s){try{var u=t[o](s),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function s(t){i(o,r,a,s,u,"next",t)}function u(t){i(o,r,a,s,u,"throw",t)}s(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,s=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){u=!0,o=t},f:function(){try{s||null==n.return||n.return()}finally{if(u)throw o}}}}exports.SwapQuoteErrorType=void 0,(r=exports.SwapQuoteErrorType||(exports.SwapQuoteErrorType={})).SwapRouterStaleDataError="SwapRouterStaleDataError",r.SwapRouterNoRouteError="SwapRouterNoRouteError",r.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",r.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",r.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",r.NoAvailablePoolError="NoAvailablePoolError",r.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",r.UnknownError="UnknownError",r.LowSwapAmountError="LowSwapAmountError",r.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",r.InvalidSwapTypeError="InvalidSwapTypeError";var D=function(t){m(n,h(Error));var e=w(n);function n(t,r){var a;return p(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(T(a),n),a}return d(n)}(),O=Uint8Array.from([1]),N={V1_1:"v1_1",V2:"v2"},P={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},k={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},L="- would result negative",R="logic eval error:",F="exceeds schema integer count",B=/transaction \w+:/,M=function(t){m(n,h(Error));var e=w(n);function n(t,r){var a;p(this,n);for(var o=arguments.length,s=new Array(o>2?o-2:0),u=2;ur?[s(s({},t),{},{id:n}),s(s({},e),{},{id:r})]:[s(s({},e),{},{id:r}),s(s({},t),{},{id:n})]}function C(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function j(t){return 0===Number(t)}function X(t){return Number(t.id)}function q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,o=void 0!==a&&a,s={},u=b(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}s[o?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return s}function U(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,o=b(t);try{for(o.s();!(e=o.n()).done;){var s=e.value;r.set(s,a),a+=s.length}}catch(t){o.e(t)}finally{o.f()}return r}var G=100000n,W=100000n,z=100000n,J=25000n+25000n,Y=25000n+3500n;function K(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],o=t["created-apps"]||[],s=t.assets||[];return G+W*BigInt(s.length)+z*BigInt(o.length+a.length)+Y*r+J*n}function H(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,H(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function tt(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function et(t,e){var n=Number(t);return rt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function nt(t,e){return rt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function rt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(ot(e),2),o=a[0],s=a[1],u=v(ot(Math.round(Number(at(o,s+r)))),2),i=u[0],c=u[1];return Number(at(i,c-r))}return Math.round(e)}function at(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ot(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function st(t,e){return ut.apply(this,arguments)}function ut(){return(ut=c(u().mark((function t(e,n){var r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=b(n),t.prev=3,a.s();case 5:if((o=a.n()).done){t.next=18;break}return s=o.value,t.next=9,e.sendRawTransaction(s).do();case 9:return i=t.sent,c=i.txId,t.next=13,Z(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new M(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function it(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ct(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function pt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function lt(t){return(new TextEncoder).encode(t)}function dt(t){return Boolean(t)&&void 0!==t.fallback_message}var ft,mt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return lt("tinyman/".concat(t===N.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),gt=(f(ft={},N.V1_1,{testnet:62368684,mainnet:552635992}),f(ft,N.V2,{testnet:148607e3,mainnet:1002541853}),ft);function It(t,e){var n=gt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function xt(){return(xt=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationOptInTxnFromObject({from:s,appIndex:It(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var At;function ht(){return(ht=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationClearStateTxnFromObject({from:s,appIndex:It(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(At=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",At.FixedOutput="fixed-output";var Tt,yt,wt;!function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(wt||(wt={}));var vt,_t,St=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),Et=lt("swap"),bt=(f(yt={},exports.SwapType.FixedInput,lt("fixed-input")),f(yt,exports.SwapType.FixedOutput,lt("fixed-output")),yt),Dt={ASSET_OPT_IN:lt("asset_opt_in")},Ot={testnet:159521633,mainnet:0},Nt=(f(vt={},exports.SwapType.FixedInput,7),f(vt,exports.SwapType.FixedOutput,8),vt),Pt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function kt(t){var e=Bt(t),n=e.assetIn,r=e.assetOut;return et(r.asset.decimals,Number(r.amount))/et(n.asset.decimals,Number(n.amount))}function Lt(t){if(!Ot[t])throw new Error("Unknown network or network not supported: ".concat(t));return Ot[t]}function Rt(t){return t[t.length-1].quote.amount_out}function Ft(t){return t[0].quote.amount_in}function Bt(t){return{assetIn:Ft(t),assetOut:Rt(t)}}function Mt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function e(n){var r,o,s,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.routerAppID,s=n.assetIDs,i=n.accountAddress,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o,appArgs:[Dt.ASSET_OPT_IN],foreignAssets:s,suggestedParams:c}),l=s.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v,_,S,E,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,o=n.client,s=n.network,i=n.swapType,c=n.route,e.next=3,o.getTransactionParams().do();case 3:return p=e.sent,l=[X(c[0].quote.amount_in.asset),X(c[0].quote.amount_out.asset),X(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Ft(c).amount),Number(Rt(c).amount)],I=g[0],x=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=j(d),w=Lt(s),v=y?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(w),amount:I,suggestedParams:p}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(w),amount:I,assetIndex:d,suggestedParams:p}),(_=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:w,appArgs:[Et,bt[i],a.default.encodeUint64(x)],foreignApps:[It(s,N.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Nt[i]+1),S=[v,_],e.next=15,jt({client:o,routerAppID:w,assetIDs:[d,f,m]});case 15:if(!((E=e.sent).length>0)){e.next=21;break}return e.next=19,Mt({client:o,accountAddress:r,assetIDs:E,routerAppID:w});case 19:b=e.sent,S.unshift(b);case 21:return D=a.default.assignGroupID(S),e.abrupt("return",D.map((function(t){return{txn:t,signers:[r]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(u().mark((function e(n){var r,a,o,s,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,o=n.assetIDs,s=t.getApplicationAddress(a),e.next=4,r.accountInformation(s).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=o.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qt(t){return Ut.apply(this,arguments)}function Ut(){return(Ut=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.swapType,s=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Pt[s].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(!dt(p)){t.next=13;break}throw new D(p.type,p.fallback_message);case 13:throw new D(exports.SwapQuoteErrorType.UnknownError,"Unknown error");case 14:return t.abrupt("return",p);case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Gt(t){var e=t.assetIn,n=t.assetOut;return et(n.decimals,Number(n.amount))/et(e.decimals,Number(e.amount))}function Wt(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,o=Gt({assetIn:r,assetOut:a}),s=et(a.decimals,Number(n))/et(r.decimals,Number(e));return rt({decimalPlaces:5},Math.abs(o/s-1))}function zt(t){if(t.type===exports.SwapQuoteType.Router){var e=Ft(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function Jt(t){if(t.type===exports.SwapQuoteType.Router){var e=Rt(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function Yt(t){return t.type===exports.SwapQuoteType.Direct?t.data.pool.contractVersion:N.V2}exports.SwapQuoteType=void 0,(_t=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",_t.Router="router";var Kt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Ht={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Zt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),$t=new(function(n){m(a,Zt);var r=w(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=It(n.network,N.V1_1),a=v(C(n.asset1ID,n.asset2ID),2),o=a[0],s=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:o,asset_id_2:s,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(s(s({},i),{},{minimum_required_balance:se(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),o(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function oe(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function se(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ue,ie,ce=lt("e");function pe(t){return le.apply(this,arguments)}function le(){return(le=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v,_,S,E;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.pool,i=r.accountAddr,n.next=3,o.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=s.account.address(),g=b(p),n.prev=10,g.s();case 12:if((I=g.n()).done){n.next=31;break}if((x=I.value).id==s.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=x["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=q({stateArray:A}),T=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.asset1ID)])),y=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.asset2ID)])),w=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.poolTokenID)])),v=h[T],_=h[y],S=h[w],"bigint"==typeof v&&(l=v),"bigint"==typeof _&&(d=_),"bigint"==typeof S&&(f=S);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((E={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function de(){return(de=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,I,x,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.accountAddr,i=r.validatorAppID,n.next=3,o.accountInformation(s).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mge)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ge,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function he(){return he=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ae(n,r);case 6:return s=t.sent,i=oe(s,It(a,N.V1_1)),c=null,i&&(l=s["created-assets"][0],p=l.index,c={asset1ID:i[fe[N.V1_1].asset1],asset2ID:i[fe[N.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),he.apply(this,arguments)}var Te=Object.freeze({__proto__:null,getPoolInfo:Ie,getPoolReserves:function(t,e){return Ae.apply(this,arguments)},getPoolAssets:function(t){return he.apply(this,arguments)}});function ye(t){return we.apply(this,arguments)}function we(){return(we=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,o=e.asset2ID,s=re(N.V2),i=s.generateLogicSigAccountForPool(e),c=It(r,N.V2),p=i.address(),l=C(a,o),t.next=8,ae(n,p);case 8:return d=t.sent,f=oe(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:N.V2},f&&(m.asset1ProtocolFees=BigInt(f[fe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[fe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[fe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[fe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[fe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[fe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[fe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[fe.v2.totalFeeShare]),m.poolTokenID=Number(f[fe.v2.poolTokenID]),m.asset1ID=Number(f[fe.v2.asset1]),m.asset2ID=Number(f[fe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ve(){return(ve=c(u().mark((function t(e,n){var r,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ae(e,n.account.address());case 2:return r=t.sent,a=oe(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[fe.v2.asset1Reserves]),o.asset2=BigInt(a[fe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[fe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(){return(_e=c(u().mark((function t(e){var n,r,a,o,s,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,ae(n,r);case 3:return o=t.sent,s=oe(o,It(a,N.V2)),i=null,s&&(i={asset1ID:s[fe[N.V2].asset1],asset2ID:s[fe[N.V2].asset2],poolTokenID:s[fe[N.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Se=Object.freeze({__proto__:null,getPoolInfo:ye,getPoolReserves:function(t,e){return ve.apply(this,arguments)},getPoolAssets:function(t){return _e.apply(this,arguments)}});function Ee(t){return Boolean(t&&!(t.asset1+t.asset2))}var be,De,Oe=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Ee(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=et(t.asset1,e.asset1)/et(t.asset2,e.asset2)),r},isPoolEmpty:Ee,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Ie(t),ye(t)])}}),Ne=s((f(be={},N.V1_1,s(s({},Te),Oe)),f(be,N.V2,s(s({},Se),Oe)),be),Oe);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(De||(De={}));var Pe=96e4,ke=859e3;function Le(){return(Le=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,_,S,E,b,D,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.asset_1,s=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=V(o,s),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],I=g.id,x=g.unit_name,A=j(I),h=It(r,N.V1_1),T=$t.generateLogicSigAccountForPool({network:r,asset1ID:f,asset2ID:I}),y=T.address(),w=a.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:mt.getAppCallTxnNoteWithClientName(N.V1_1),appArgs:[lt("bootstrap"),a.default.encodeUint64(f),a.default.encodeUint64(I)],foreignAssets:A?[f]:[I],suggestedParams:c}),_=a.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:k.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),S=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Re(A),suggestedParams:c}),(b=[])[De.FUNDING_TXN]=E,b[De.VALIDATOR_APP_CALL]=w,b[De.POOL_TOKEN_CREATE]=_,b[De.ASSET1_OPT_IN]=S,A||(b[De.ASSET2_OPT_IN]=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:I,amount:0,suggestedParams:c})),D=a.default.assignGroupID(b),O=[{txn:D[De.FUNDING_TXN],signers:[i]},{txn:D[De.VALIDATOR_APP_CALL],signers:[y]},{txn:D[De.POOL_TOKEN_CREATE],signers:[y]},{txn:D[De.ASSET1_OPT_IN],signers:[y]}],D[De.ASSET2_OPT_IN]&&O.push({txn:D[De.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Re(t){return t?Pe:ke}function Fe(){return(Fe=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=C(s,i),f=v(d,2),m=f[0],g=f[1],I=$t.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===De.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,o=n.blob;return x.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(t){return Me.apply(this,arguments)}function Me(){return(Me=c(u().mark((function t(e){var n,r,a,o,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Z(n,a[De.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(s=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(s));case 10:return t.abrupt("return",{poolTokenID:s});case 13:throw t.prev=13,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Qe(){return(Qe=c(u().mark((function t(e){var n,r,a,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Be({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",Ne.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ve,Ce={generateTxns:function(t){return Le.apply(this,arguments)},signTxns:function(t){return Fe.apply(this,arguments)},execute:function(t){return Qe.apply(this,arguments)},getBootstrapFundingTxnAmount:Re};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ve||(Ve={}));var je=5,Xe=6;function qe(t,e){return Ue.apply(this,arguments)}function Ue(){return(Ue=c(u().mark((function e(n,r){var a,o,s;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return s=e.t0,e.abrupt("return",s);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ge(t,e){return We.apply(this,arguments)}function We(){return(We=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qe(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ze(){return(ze=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,_,S;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.network,s=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return p=e.sent,l=It(o,N.V2),d=t.getApplicationAddress(l),f=V(s,i),m=v(f,2),g=m[0].id,I=m[1].id,e.next=9,Ne.v2.getPoolInfo({client:r,network:o,asset1ID:g,asset2ID:I});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(s.unit_name,"-").concat(i.unit_name," already exists"));case 12:return x=ne.generateLogicSigAccountForPool({network:o,asset1ID:g,asset2ID:I}),A=x.address(),h=j(I),(T=a.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[lt("bootstrap")],note:mt.getAppCallTxnNoteWithClientName(N.V2),foreignAssets:[g,I],rekeyTo:d,suggestedParams:p})).fee=Ye(h),y=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Je(h),suggestedParams:p}),(w=[])[Ve.FUNDING_TXN]=y,w[Ve.VALIDATOR_APP_CALL]=T,_=a.default.assignGroupID(w),(S=[])[Ve.FUNDING_TXN]={txn:_[Ve.FUNDING_TXN],signers:[c]},S[Ve.VALIDATOR_APP_CALL]={txn:_[Ve.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",S);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Je(t){return function(t,e){var n=re(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(N.V2,t)+Ye(t)+1e5}function Ye(e){return((e?je:Xe)+1)*t.ALGORAND_MIN_TX_FEE}function Ke(){return(Ke=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=C(s,i),f=v(d,2),m=f[0],g=f[1],I=ne.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===Ve.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,o=n.blob;return x.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function He(){return(He=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,qe(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(fe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Ne.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 21:throw t.prev=21,t.t3=t.catch(1),new M(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Ze,$e={generateTxns:function(t){return ze.apply(this,arguments)},signTxns:function(t){return Ke.apply(this,arguments)},execute:function(t){return He.apply(this,arguments)},getBootstrapFundingTxnAmount:Je,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Je(e)}};var tn,en=(f(Ze={},N.V1_1,Ce),f(Ze,N.V2,$e),f(Ze,"generateTxns",(function(t){return t.contractVersion===N.V1_1?Ce.generateTxns(t):$e.generateTxns(t)})),f(Ze,"signTxns",(function(t){return t.contractVersion===N.V1_1?Ce.signTxns(t):$e.signTxns(t)})),f(Ze,"execute",(function(t){return t.contractVersion===N.V1_1?Ce.execute(t):$e.execute(t)})),f(Ze,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===N.V1_1?Ce.getBootstrapFundingTxnAmount(n):$e.getBootstrapFundingTxnAmount(n)})),Ze);exports.V1_1AddLiquidityTxnIndices=void 0,(tn=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[tn.FEE_TXN=0]="FEE_TXN",tn[tn.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",tn[tn.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",tn[tn.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",tn[tn.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var nn,rn,an,on,sn=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(on=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",on.FLEXIBLE="flexible",on.INITIAL="initial";var un,cn=(f(nn={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(nn,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(nn,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),nn),pn=(f(rn={},exports.V2AddLiquidityType.INITIAL,1),f(rn,exports.V2AddLiquidityType.SINGLE,2),f(rn,exports.V2AddLiquidityType.FLEXIBLE,2),rn),ln=(f(an={},exports.V2AddLiquidityType.INITIAL,3),f(an,exports.V2AddLiquidityType.FLEXIBLE,3),f(an,exports.V2AddLiquidityType.SINGLE,2),an);function dn(t){var e,n,r,a=t.reserves,o=t.totalFeeShare,s=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(s.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,I=BigInt(s.amount)-m,x=BigInt(u.amount)-g;if(I>x){var A=I;r=fn(A,o),e={id:s.id,amount:A+r,decimals:s.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=x;r=fn(h,o),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:s.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:s.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Wt({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function fn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function mn(e){return(pn[e]+1)*t.ALGORAND_MIN_TX_FEE}var gn=lt("add_liquidity"),In=(f(un={},N.V1_1,[lt("mint")]),f(un,N.V2,{INITIAL_LIQUIDITY:[lt("add_initial_liquidity")],SINGLE_ASSET_MODE:[gn,lt("single")],FLEXIBLE_MODE:[gn,lt("flexible")]}),un);function xn(){return(xn=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.poolAddress,s=e.asset1In,i=e.asset2In,c=e.poolTokenOut,p=e.slippage,l=e.initiatorAddr,d=tt("negative",p,c.amount),t.next=4,n.getTransactionParams().do();case 4:return f=t.sent,m=a.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:It(r,N.V1_1),appArgs:In.v1_1,accounts:[l],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==i.id?[s.id,c.id]:[s.id,i.id,c.id],suggestedParams:f}),g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:s.id,amount:s.amount,suggestedParams:f}),I=0===i.id?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:i.amount,suggestedParams:f}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),x=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:O,suggestedParams:f}),h=a.default.assignGroupID([A,m,g,I,x]),t.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function An(){return(An=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,s=n.account,t.next=4,o([r]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=r.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:a.default.signLogicSigTransactionObject(t.txn,s).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hn(){return(hn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.signedTxns,s=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,pe({client:n,pool:r,accountAddr:s});case 5:return c=t.sent,t.next=8,st(n,[o]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=it(a),I=ct(a),t.next=17,pe({client:n,pool:r,accountAddr:s});case 17:return x=t.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:I});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new M(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Tn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var o=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(o<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(o,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:o-BigInt(1e3),share:1}}var s=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=si)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Gt(d),priceImpact:Wt(s({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function zn(t){return Jn.apply(this,arguments)}function Jn(){return(Jn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:s.id,assetOutAmount:BigInt(s.amount)+A,excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yn(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap on a non-existent pool");var o,s,u,i=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,s=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,s=n.asset1,u=n.asset2}if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=s*u/(u-i)-s,p=c*qn/(qn-Xn),l=p-c,d=et(a.assetOut,Number(i))/et(a.assetIn,Number(p)),f=et(a.assetOut,Number(u))/et(a.assetIn,Number(s)),m=rt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:o,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function Kn(t){return Hn.apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:s.id,assetOutAmount:BigInt(s.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,r,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,o=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new M({pool:r,swapType:a,txGroup:o},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:o[Cn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:o[Cn.ASSET_IN_TXN_INDEX].txn.amount},l={id:o[Cn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:o[Cn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,zn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Kn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",s(s({},d),{},{groupID:ct(o),fees:it(o)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new M(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Cn||(Cn={}));var $n={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Wn({pool:e,reserves:n,assetIn:r,decimals:a}):Yn({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Wn,getFixedOutputSwapQuote:Yn,generateTxns:function(t){return Gn.apply(this,arguments)},signTxns:function(t){return Un.apply(this,arguments)},execute:function(t){return Zn.apply(this,arguments)},executeFixedOutputSwap:Kn},tr=4*t.ALGORAND_MIN_TX_FEE;function er(e){return(St[e]+2)*t.ALGORAND_MIN_TX_FEE}function nr(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:.003;return Math.ceil(1/t)}function rr(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:.003;return tn&&(e=a,n=o)}return e}function pr(){return(pr=c(u().mark((function t(e){var n,r,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Vt(s(s({},e),{},{route:e.quote.data.route})));case 2:if(n=e.client,r=e.initiatorAddr,o=e.slippage,i=e.swapType,c=e.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(I=[l.asset1ID,l.asset2ID]).includes(f)&&I.includes(m)&&f!==m){t.next=9;break}throw new M({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return x=t.sent,A=j(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:tt("positive",o,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:tt("negative",o,d.assetOutAmount),y=A?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,suggestedParams:x}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,assetIndex:f,suggestedParams:x}),(w=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:l.validatorAppID,appArgs:[Et,bt[i],a.default.encodeUint64(T)],note:mt.getAppCallTxnNoteWithClientName(N.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=lr(i),(v=[])[wt.INPUT_TXN]=y,v[wt.APP_CALL_TXN]=w,_=a.default.assignGroupID(v),t.abrupt("return",[{txn:_[wt.INPUT_TXN],signers:[r]},{txn:_[wt.APP_CALL_TXN],signers:[r]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function lr(e){return(St[e]+1)*t.ALGORAND_MIN_TX_FEE}function dr(){return(dr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,o=e.quote,s=e.txGroup,i=e.signedTxns,t.next=3,st(a,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Ge(a,s);case 10:return m=t.sent,g=zt(o),I=Jt(o).id,x=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===I})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:x&&{amount:BigInt(g.amount)-BigInt(x.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:I},quote:o,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fr(){return(fr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.amount,a=e.assetInID,o=e.assetOutID,s=e.decimals,i=e.network,c=e.isSwapRouterEnabled,p=e.pool,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,xr({assetInID:a,assetOutID:o,amount:r,decimals:s,isSwapRouterEnabled:c,network:i,pool:p});case 4:l=t.sent,t.next=10;break;case 7:return t.next=9,hr({amount:r,assetInID:a,assetOutID:o,decimals:s,isSwapRouterEnabled:c,network:i,pool:p});case 9:l=t.sent;case 10:return t.abrupt("return",l);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=t[0].reason;if(2===t.length){var n=t[1].reason;if(ar(e)&&!ar(n))throw e;throw n}throw e}return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function gr(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,o=t.pool;if(!Ne.isPoolReady(o))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");if(rr(Number(e)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");var s,u,i=BigInt(e),c=o.totalFeeShare;if(n===o.asset1ID)s=o.asset1Reserves,u=o.asset2Reserves;else{if(n!==o.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Input asset (#".concat(n,") doesn't belong to the pool ").concat(o.account.address(),"."));s=o.asset2Reserves,u=o.asset1Reserves}var p=yr({inputSupply:s,outputSupply:u,swapInputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapOutputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(l>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");return{assetInID:n,assetInAmount:i,assetOutID:r,assetOutAmount:l,swapFee:Number(d),rate:et(a.assetOut,Number(l))/et(a.assetIn,Number(i)),priceImpact:f}}function Ir(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,o=t.pool;if(!o||!Ne.isPoolReady(o))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");var s,u,i=BigInt(e),c=o.totalFeeShare;if(r===o.asset1ID)s=o.asset2Reserves,u=o.asset1Reserves;else{if(r!==o.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r,") doesn't belong to the pool ").concat(o.account.address(),"."));s=o.asset1Reserves,u=o.asset2Reserves}var p=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,o=t.decimals,s=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:s,totalFeeShare:a}),i=s+u,c=Wt({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:o.assetIn},assetOut:{amount:r,decimals:o.assetOut}});return{swapInputAmount:i,totalFeeAmount:u,priceImpact:c}}({inputSupply:s,outputSupply:u,swapOutputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapInputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(rr(Number(l)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");return{type:exports.SwapQuoteType.Direct,data:{pool:o,quote:{assetInID:n,assetInAmount:l,assetOutID:r,assetOutAmount:i,swapFee:Number(d),rate:et(a.assetOut,Number(i))/et(a.assetIn,Number(l)),priceImpact:f}}}}function xr(t){return Ar.apply(this,arguments)}function Ar(){return(Ar=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[],p?l.push(new Promise((function(t,e){try{var s=gr({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p});t({type:exports.SwapQuoteType.Direct,data:{pool:p,quote:s}})}catch(t){e(t)}}))):l.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools."))),s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedInput,network:i});case 2:if(!((e=t.sent).route.length<2)){t.next=5;break}throw new D(exports.SwapQuoteErrorType.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 5:return t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 6:case"end":return t.stop()}}),t)})))()),t.next=6,mr(l);case 6:return d=t.sent,t.abrupt("return",cr(d));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hr(t){return Tr.apply(this,arguments)}function Tr(){return(Tr=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[new Promise((function(t,e){try{t(Ir({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p}))}catch(t){e(t)}}))],s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedOutput,network:i});case 2:return e=t.sent,t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 4:case"end":return t.stop()}}),t)})))()),t.next=5,mr(l);case 5:return d=t.sent,t.abrupt("return",cr(d));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,o=t.decimals,s=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-s});return{swapOutputAmount:u,totalFeeAmount:s,priceImpact:Wt({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:o.assetIn},assetOut:{amount:u,decimals:o.assetOut}})}}var wr={getQuote:function(t){return fr.apply(this,arguments)},getFixedInputSwapQuote:xr,getFixedInputDirectSwapQuote:gr,getFixedOutputDirectSwapQuote:Ir,getFixedOutputSwapQuote:hr,generateTxns:function(t){return pr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return dr.apply(this,arguments)},calculateFixedInputSwap:yr};function vr(t,e){var n,r,a=BigInt(t),o=e.issuedLiquidity;return o>a+BigInt(1e3)?(n=a*e.asset1/o,r=a*e.asset2/o):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function _r(){return(_r=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.account.address(),m=o.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:s,suggestedParams:d}),(I=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(tt("negative",l,c)),a.default.encodeUint64(tt("negative",l,p))],accounts:[f],foreignAssets:[o.asset1ID,o.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(x=[])[Rn.ASSET_TRANSFER_TXN]=g,x[Rn.APP_CALL_TXN]=I,A=a.default.assignGroupID(x),e.abrupt("return",[{txn:A[Rn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[Rn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Sr(){return(Sr=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.asset1ID,m=o.asset2ID,g=o.account.address(),I=o.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(x=0,A=0,h=tt("negative",l,p),c!==f){e.next=17;break}x=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}x=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,assetIndex:I,amount:i,suggestedParams:d}),(y=a.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(x),a.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(w=[])[Rn.ASSET_TRANSFER_TXN]=T,w[Rn.APP_CALL_TXN]=y,v=a.default.assignGroupID(w),e.abrupt("return",[{txn:v[Rn.ASSET_TRANSFER_TXN],signers:[s]},{txn:v[Rn.APP_CALL_TXN],signers:[s]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Er(){return(Er=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,o=e.signedTxns,t.next=3,st(r,[o]);case 3:return s=t.sent,i=v(s,1),c=i[0].txnID,t.next=8,Ge(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var br,Dr,Or={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=vr(a,n),s=o.asset1OutputAmount,u=o.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:s},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,o=t.assetOutID,s=t.decimals,u=BigInt(a),i=vr(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(o===n.asset1ID){var d=wr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:s}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(o!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var I=wr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:s}),x=I.swapOutputAmount,A=I.totalFeeAmount,h=I.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return _r.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Sr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Er.apply(this,arguments)}},Nr=(f(br={},N.V1_1,jn),f(br,N.V2,Or),br),Pr=(f(Dr={},N.V1_1,$n),f(Dr,N.V2,wr),f(Dr,"getQuote",(function(t){var e=t.type,n=t.pools;if(!t.isSwapRouterEnabled&&n.every((function(t){return Ee(t.reserves)})))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");if(e===exports.SwapType.FixedInput)return function(t){return sr.apply(this,arguments)}(t);if(e===exports.SwapType.FixedOutput)return function(t){return ur.apply(this,arguments)}(t);throw new D(exports.SwapQuoteErrorType.InvalidSwapTypeError,"Invalid swap type")})),f(Dr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1?$n.generateTxns(s(s({},t),{},{quote:t.quote.data})):wr.generateTxns(t)})),f(Dr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1){var e=t.quote.data.pool;return $n.signTxns(s({pool:e},t))}return wr.signTxns(t)})),f(Dr,"execute",(function(t){return t.contractVersion===N.V1_1?$n.execute(t):wr.execute(t)})),Dr);function kr(){return(kr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,Lr({txGroup:a,pool:r,initiatorSigner:o});case 4:return s=t.sent,t.next=7,st(n,[s]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:it(a),confirmedRound:d,txnID:l,groupID:ct(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function Lr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.pool,o=e.initiatorSigner,t.next=3,o([n]);case 3:return s=t.sent,i=v(s,1),c=i[0],p=r.account.lsig,l=n.map((function(t,e){return 0===e?c:a.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Fr(){return Fr=c(u().mark((function t(e){var n,r,o,s,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.data,o=e.initiatorSigner,t.prev=1,s=r.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:it(e),groupID:ct(e),lsig:n.account.lsig}})),t.next=5,o(s.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(s.map((function(t,e){return new Promise(function(){var r=c(u().mark((function r(o,s){var c,p,l,d,f,m;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,c=t.txns.map((function(n,r){return 0===r?i[e]:a.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),r.next=4,st(n,[c]);case 4:p=r.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),r.next=15;break;case 12:r.prev=12,r.t0=r.catch(0),s(r.t0);case 15:case"end":return r.stop()}}),r,null,[[0,12]])})));return function(t,e){return r.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Fr.apply(this,arguments)}function Br(){return(Br=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,o=e.assetID,s=e.assetOut,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=r.account.address(),l=a.default.makeApplicationNoOpTxnFromObject({from:p,appIndex:r.validatorAppID,appArgs:[lt("redeem")],note:mt.getAppCallTxnNoteWithClientName(r.contractVersion),accounts:[i],foreignAssets:0==r.asset2ID?[r.asset1ID,r.poolTokenID]:[r.asset1ID,r.asset2ID,r.poolTokenID],suggestedParams:c}),d=0===o?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:i,amount:BigInt(s),suggestedParams:c}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:i,assetIndex:o,amount:BigInt(s),suggestedParams:c}),f=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:p,amount:l.fee+d.fee,note:O,suggestedParams:c}),m=a.default.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[i]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Mr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,o=e.liquidityAssetID,s=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(s.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[lt("commit"),i],note:U([lt("tinymanStaking/v1:b"),c,t.encodeUint64(o),i])})}function Qr(){return(Qr=c(u().mark((function e(n){var r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,s=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Mr({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof s){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[lt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=P,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Ln,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=en,exports.CONTRACT_VERSION=N,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=k,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Nr,exports.Swap=Pr,exports.SwapQuoteError=D,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Fn,exports.V2AddLiquidityTxnIndices=cn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=tt,exports.calculateAccountMinimumRequiredBalance=se,exports.calculatePriceImpact=Wt,exports.calculateSwapRate=Gt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=rr,exports.poolUtils=Ne,exports.prepareCommitTransactions=function(t){return Qr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Fr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return kr.apply(this,arguments)},exports.sendAndWaitRawTransaction=st,exports.sumUpTxnFees=it,exports.tinymanContract_v1_1=$t,exports.tinymanContract_v2=ne,exports.tinymanJSSDKConfig=mt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",o=r.asyncIterator||"@@asyncIterator",s=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,o=Object.create(a.prototype),s=new _(r||[]);return o._invoke=function(t,e,n){var r="suspendedStart";return function(a,o){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw o;return E()}for(n.method=a,n.arg=o;;){var s=n.delegate;if(s){var u=y(s,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,s),o}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var I=Object.getPrototypeOf,x=I&&I(I(S([])));x&&x!==e&&n.call(x,a)&&(g=x);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,o,s,u){var i=p(t[a],t,o);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,s,u)}),(function(t){r("throw",t,s,u)})):e.resolve(l).then((function(t){c.value=t,s(c)}),(function(t){return r("throw",t,s,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function o(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(o,o):o()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function w(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function v(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function _(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(w,this),this.reset(!0)}function S(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,o=function e(){for(;++r=0;--a){var o=this.tryEntries[a],s=o.completion;if("root"===o.tryLoc)return r("end");if(o.tryLoc<=this.prev){var u=n.call(o,"catchLoc"),i=n.call(o,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),v(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;v(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:S(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,o,s){try{var u=t[o](s),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function s(t){i(o,r,a,s,u,"next",t)}function u(t){i(o,r,a,s,u,"throw",t)}s(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,s=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){u=!0,o=t},f:function(){try{s||null==n.return||n.return()}finally{if(u)throw o}}}}exports.SwapQuoteErrorType=void 0,(r=exports.SwapQuoteErrorType||(exports.SwapQuoteErrorType={})).SwapRouterStaleDataError="SwapRouterStaleDataError",r.SwapRouterNoRouteError="SwapRouterNoRouteError",r.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",r.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",r.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",r.NoAvailablePoolError="NoAvailablePoolError",r.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",r.UnknownError="UnknownError",r.LowSwapAmountError="LowSwapAmountError",r.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",r.InvalidSwapTypeError="InvalidSwapTypeError";var D=function(t){m(n,h(Error));var e=w(n);function n(t,r){var a;return p(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(T(a),n),a}return d(n)}(),O=Uint8Array.from([1]),N={V1_1:"v1_1",V2:"v2"},P={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},k={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},L="- would result negative",R="logic eval error:",F="exceeds schema integer count",B=/transaction \w+:/,M=function(t){m(n,h(Error));var e=w(n);function n(t,r){var a;p(this,n);for(var o=arguments.length,s=new Array(o>2?o-2:0),u=2;ur?[s(s({},t),{},{id:n}),s(s({},e),{},{id:r})]:[s(s({},e),{},{id:r}),s(s({},t),{},{id:n})]}function C(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function j(t){return 0===Number(t)}function X(t){return Number(t.id)}function q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,o=void 0!==a&&a,s={},u=b(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}s[o?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return s}function U(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,o=b(t);try{for(o.s();!(e=o.n()).done;){var s=e.value;r.set(s,a),a+=s.length}}catch(t){o.e(t)}finally{o.f()}return r}var G=100000n,W=100000n,z=100000n,J=25000n+25000n,Y=25000n+3500n;function K(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],o=t["created-apps"]||[],s=t.assets||[];return G+W*BigInt(s.length)+z*BigInt(o.length+a.length)+Y*r+J*n}function H(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,H(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function tt(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function et(t,e){var n=Number(t);return rt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function nt(t,e){return rt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function rt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(ot(e),2),o=a[0],s=a[1],u=v(ot(Math.round(Number(at(o,s+r)))),2),i=u[0],c=u[1];return Number(at(i,c-r))}return Math.round(e)}function at(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ot(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function st(t,e){return ut.apply(this,arguments)}function ut(){return(ut=c(u().mark((function t(e,n){var r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=b(n),t.prev=3,a.s();case 5:if((o=a.n()).done){t.next=18;break}return s=o.value,t.next=9,e.sendRawTransaction(s).do();case 9:return i=t.sent,c=i.txId,t.next=13,Z(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new M(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function it(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ct(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function pt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function lt(t){return(new TextEncoder).encode(t)}function dt(t){return Boolean(t)&&void 0!==t.fallback_message}var ft,mt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return lt("tinyman/".concat(t===N.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),gt=(f(ft={},N.V1_1,{testnet:62368684,mainnet:552635992}),f(ft,N.V2,{testnet:148607e3,mainnet:1002541853}),ft);function It(t,e){var n=gt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function xt(){return(xt=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationOptInTxnFromObject({from:s,appIndex:It(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var At;function ht(){return(ht=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationClearStateTxnFromObject({from:s,appIndex:It(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(At=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",At.FixedOutput="fixed-output";var Tt,yt,wt;!function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(wt||(wt={}));var vt,_t,St=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),Et=lt("swap"),bt=(f(yt={},exports.SwapType.FixedInput,lt("fixed-input")),f(yt,exports.SwapType.FixedOutput,lt("fixed-output")),yt),Dt={ASSET_OPT_IN:lt("asset_opt_in")},Ot={testnet:159521633,mainnet:0},Nt=(f(vt={},exports.SwapType.FixedInput,7),f(vt,exports.SwapType.FixedOutput,8),vt),Pt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function kt(t){var e=Bt(t),n=e.assetIn,r=e.assetOut;return et(r.asset.decimals,Number(r.amount))/et(n.asset.decimals,Number(n.amount))}function Lt(t){if(!Ot[t])throw new Error("Unknown network or network not supported: ".concat(t));return Ot[t]}function Rt(t){return t[t.length-1].quote.amount_out}function Ft(t){return t[0].quote.amount_in}function Bt(t){return{assetIn:Ft(t),assetOut:Rt(t)}}function Mt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function e(n){var r,o,s,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.routerAppID,s=n.assetIDs,i=n.accountAddress,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o,appArgs:[Dt.ASSET_OPT_IN],foreignAssets:s,suggestedParams:c}),l=s.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v,_,S,E,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,o=n.client,s=n.network,i=n.swapType,c=n.route,e.next=3,o.getTransactionParams().do();case 3:return p=e.sent,l=[X(c[0].quote.amount_in.asset),X(c[0].quote.amount_out.asset),X(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Ft(c).amount),Number(Rt(c).amount)],I=g[0],x=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=j(d),w=Lt(s),v=y?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(w),amount:I,suggestedParams:p}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(w),amount:I,assetIndex:d,suggestedParams:p}),(_=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:w,appArgs:[Et,bt[i],a.default.encodeUint64(x)],foreignApps:[It(s,N.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Nt[i]+1),S=[v,_],e.next=15,jt({client:o,routerAppID:w,assetIDs:[d,f,m]});case 15:if(!((E=e.sent).length>0)){e.next=21;break}return e.next=19,Mt({client:o,accountAddress:r,assetIDs:E,routerAppID:w});case 19:b=e.sent,S.unshift(b);case 21:return D=a.default.assignGroupID(S),e.abrupt("return",D.map((function(t){return{txn:t,signers:[r]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(u().mark((function e(n){var r,a,o,s,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,o=n.assetIDs,s=t.getApplicationAddress(a),e.next=4,r.accountInformation(s).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=o.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qt(t){return Ut.apply(this,arguments)}function Ut(){return(Ut=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.swapType,s=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Pt[s].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(!dt(p)){t.next=13;break}throw new D(p.type,p.fallback_message);case 13:throw new D(exports.SwapQuoteErrorType.UnknownError,"Unknown error");case 14:return t.abrupt("return",p);case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Gt(t){var e=t.assetIn,n=t.assetOut;return et(n.decimals,Number(n.amount))/et(e.decimals,Number(e.amount))}function Wt(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,o=Gt({assetIn:r,assetOut:a}),s=et(a.decimals,Number(n))/et(r.decimals,Number(e));return rt({decimalPlaces:5},Math.abs(o/s-1))}function zt(t){if(t.type===exports.SwapQuoteType.Router){var e=Ft(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function Jt(t){if(t.type===exports.SwapQuoteType.Router){var e=Rt(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function Yt(t){return t.type===exports.SwapQuoteType.Direct?t.data.pool.contractVersion:N.V2}exports.SwapQuoteType=void 0,(_t=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",_t.Router="router";var Kt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Ht={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Zt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),$t=new(function(n){m(a,Zt);var r=w(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=It(n.network,N.V1_1),a=v(C(n.asset1ID,n.asset2ID),2),o=a[0],s=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:o,asset_id_2:s,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(s(s({},i),{},{minimum_required_balance:se(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),o(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function oe(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function se(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ue,ie,ce=lt("e");function pe(t){return le.apply(this,arguments)}function le(){return(le=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v,_,S,E;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.pool,i=r.accountAddr,n.next=3,o.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=s.account.address(),g=b(p),n.prev=10,g.s();case 12:if((I=g.n()).done){n.next=31;break}if((x=I.value).id==s.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=x["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=q({stateArray:A}),T=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.asset1ID)])),y=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.asset2ID)])),w=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.poolTokenID)])),v=h[T],_=h[y],S=h[w],"bigint"==typeof v&&(l=v),"bigint"==typeof _&&(d=_),"bigint"==typeof S&&(f=S);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((E={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function de(){return(de=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,I,x,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.accountAddr,i=r.validatorAppID,n.next=3,o.accountInformation(s).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mge)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ge,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function he(){return he=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ae(n,r);case 6:return s=t.sent,i=oe(s,It(a,N.V1_1)),c=null,i&&(l=s["created-assets"][0],p=l.index,c={asset1ID:i[fe[N.V1_1].asset1],asset2ID:i[fe[N.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),he.apply(this,arguments)}var Te=Object.freeze({__proto__:null,getPoolInfo:Ie,getPoolReserves:function(t,e){return Ae.apply(this,arguments)},getPoolAssets:function(t){return he.apply(this,arguments)}});function ye(t){return we.apply(this,arguments)}function we(){return(we=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,o=e.asset2ID,s=re(N.V2),i=s.generateLogicSigAccountForPool(e),c=It(r,N.V2),p=i.address(),l=C(a,o),t.next=8,ae(n,p);case 8:return d=t.sent,f=oe(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:N.V2},f&&(m.asset1ProtocolFees=BigInt(f[fe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[fe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[fe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[fe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[fe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[fe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[fe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[fe.v2.totalFeeShare]),m.poolTokenID=Number(f[fe.v2.poolTokenID]),m.asset1ID=Number(f[fe.v2.asset1]),m.asset2ID=Number(f[fe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ve(){return(ve=c(u().mark((function t(e,n){var r,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ae(e,n.account.address());case 2:return r=t.sent,a=oe(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[fe.v2.asset1Reserves]),o.asset2=BigInt(a[fe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[fe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(){return(_e=c(u().mark((function t(e){var n,r,a,o,s,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,ae(n,r);case 3:return o=t.sent,s=oe(o,It(a,N.V2)),i=null,s&&(i={asset1ID:s[fe[N.V2].asset1],asset2ID:s[fe[N.V2].asset2],poolTokenID:s[fe[N.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Se=Object.freeze({__proto__:null,getPoolInfo:ye,getPoolReserves:function(t,e){return ve.apply(this,arguments)},getPoolAssets:function(t){return _e.apply(this,arguments)}});function Ee(t){return Boolean(t&&!(t.asset1+t.asset2))}var be,De,Oe=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Ee(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=et(t.asset1,e.asset1)/et(t.asset2,e.asset2)),r},isPoolEmpty:Ee,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Ie(t),ye(t)])}}),Ne=s((f(be={},N.V1_1,s(s({},Te),Oe)),f(be,N.V2,s(s({},Se),Oe)),be),Oe);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(De||(De={}));var Pe=96e4,ke=859e3;function Le(){return(Le=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,_,S,E,b,D,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.asset_1,s=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=V(o,s),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],I=g.id,x=g.unit_name,A=j(I),h=It(r,N.V1_1),T=$t.generateLogicSigAccountForPool({network:r,asset1ID:f,asset2ID:I}),y=T.address(),w=a.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:mt.getAppCallTxnNoteWithClientName(N.V1_1),appArgs:[lt("bootstrap"),a.default.encodeUint64(f),a.default.encodeUint64(I)],foreignAssets:A?[f]:[I],suggestedParams:c}),_=a.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:k.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),S=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Re(A),suggestedParams:c}),(b=[])[De.FUNDING_TXN]=E,b[De.VALIDATOR_APP_CALL]=w,b[De.POOL_TOKEN_CREATE]=_,b[De.ASSET1_OPT_IN]=S,A||(b[De.ASSET2_OPT_IN]=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:I,amount:0,suggestedParams:c})),D=a.default.assignGroupID(b),O=[{txn:D[De.FUNDING_TXN],signers:[i]},{txn:D[De.VALIDATOR_APP_CALL],signers:[y]},{txn:D[De.POOL_TOKEN_CREATE],signers:[y]},{txn:D[De.ASSET1_OPT_IN],signers:[y]}],D[De.ASSET2_OPT_IN]&&O.push({txn:D[De.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Re(t){return t?Pe:ke}function Fe(){return(Fe=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=C(s,i),f=v(d,2),m=f[0],g=f[1],I=$t.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===De.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,o=n.blob;return x.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(t){return Me.apply(this,arguments)}function Me(){return(Me=c(u().mark((function t(e){var n,r,a,o,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Z(n,a[De.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(s=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(s));case 10:return t.abrupt("return",{poolTokenID:s});case 13:throw t.prev=13,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Qe(){return(Qe=c(u().mark((function t(e){var n,r,a,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Be({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",Ne.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ve,Ce={generateTxns:function(t){return Le.apply(this,arguments)},signTxns:function(t){return Fe.apply(this,arguments)},execute:function(t){return Qe.apply(this,arguments)},getBootstrapFundingTxnAmount:Re};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ve||(Ve={}));var je=5,Xe=6;function qe(t,e){return Ue.apply(this,arguments)}function Ue(){return(Ue=c(u().mark((function e(n,r){var a,o,s;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return s=e.t0,e.abrupt("return",s);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ge(t,e){return We.apply(this,arguments)}function We(){return(We=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qe(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ze(){return(ze=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,_,S;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.network,s=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return p=e.sent,l=It(o,N.V2),d=t.getApplicationAddress(l),f=V(s,i),m=v(f,2),g=m[0].id,I=m[1].id,e.next=9,Ne.v2.getPoolInfo({client:r,network:o,asset1ID:g,asset2ID:I});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(s.unit_name,"-").concat(i.unit_name," already exists"));case 12:return x=ne.generateLogicSigAccountForPool({network:o,asset1ID:g,asset2ID:I}),A=x.address(),h=j(I),(T=a.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[lt("bootstrap")],note:mt.getAppCallTxnNoteWithClientName(N.V2),foreignAssets:[g,I],rekeyTo:d,suggestedParams:p})).fee=Ye(h),y=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Je(h),suggestedParams:p}),(w=[])[Ve.FUNDING_TXN]=y,w[Ve.VALIDATOR_APP_CALL]=T,_=a.default.assignGroupID(w),(S=[])[Ve.FUNDING_TXN]={txn:_[Ve.FUNDING_TXN],signers:[c]},S[Ve.VALIDATOR_APP_CALL]={txn:_[Ve.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",S);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Je(t){return function(t,e){var n=re(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(N.V2,t)+Ye(t)+1e5}function Ye(e){return((e?je:Xe)+1)*t.ALGORAND_MIN_TX_FEE}function Ke(){return(Ke=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=C(s,i),f=v(d,2),m=f[0],g=f[1],I=ne.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===Ve.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,o=n.blob;return x.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function He(){return(He=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,qe(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(fe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Ne.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 21:throw t.prev=21,t.t3=t.catch(1),new M(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Ze,$e={generateTxns:function(t){return ze.apply(this,arguments)},signTxns:function(t){return Ke.apply(this,arguments)},execute:function(t){return He.apply(this,arguments)},getBootstrapFundingTxnAmount:Je,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Je(e)}};var tn,en=(f(Ze={},N.V1_1,Ce),f(Ze,N.V2,$e),f(Ze,"generateTxns",(function(t){return t.contractVersion===N.V1_1?Ce.generateTxns(t):$e.generateTxns(t)})),f(Ze,"signTxns",(function(t){return t.contractVersion===N.V1_1?Ce.signTxns(t):$e.signTxns(t)})),f(Ze,"execute",(function(t){return t.contractVersion===N.V1_1?Ce.execute(t):$e.execute(t)})),f(Ze,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===N.V1_1?Ce.getBootstrapFundingTxnAmount(n):$e.getBootstrapFundingTxnAmount(n)})),Ze);exports.V1_1AddLiquidityTxnIndices=void 0,(tn=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[tn.FEE_TXN=0]="FEE_TXN",tn[tn.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",tn[tn.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",tn[tn.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",tn[tn.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var nn,rn,an,on,sn=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(on=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",on.FLEXIBLE="flexible",on.INITIAL="initial";var un,cn=(f(nn={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(nn,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(nn,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),nn),pn=(f(rn={},exports.V2AddLiquidityType.INITIAL,1),f(rn,exports.V2AddLiquidityType.SINGLE,2),f(rn,exports.V2AddLiquidityType.FLEXIBLE,2),rn),ln=(f(an={},exports.V2AddLiquidityType.INITIAL,3),f(an,exports.V2AddLiquidityType.FLEXIBLE,3),f(an,exports.V2AddLiquidityType.SINGLE,2),an);function dn(t){var e,n,r,a=t.reserves,o=t.totalFeeShare,s=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(s.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,I=BigInt(s.amount)-m,x=BigInt(u.amount)-g;if(I>x){var A=I;r=fn(A,o),e={id:s.id,amount:A+r,decimals:s.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=x;r=fn(h,o),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:s.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:s.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Wt({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function fn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function mn(e){return(pn[e]+1)*t.ALGORAND_MIN_TX_FEE}var gn=lt("add_liquidity"),In=(f(un={},N.V1_1,[lt("mint")]),f(un,N.V2,{INITIAL_LIQUIDITY:[lt("add_initial_liquidity")],SINGLE_ASSET_MODE:[gn,lt("single")],FLEXIBLE_MODE:[gn,lt("flexible")]}),un);function xn(){return(xn=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.poolAddress,s=e.asset1In,i=e.asset2In,c=e.poolTokenOut,p=e.slippage,l=e.initiatorAddr,d=tt("negative",p,c.amount),t.next=4,n.getTransactionParams().do();case 4:return f=t.sent,m=a.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:It(r,N.V1_1),appArgs:In.v1_1,accounts:[l],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==i.id?[s.id,c.id]:[s.id,i.id,c.id],suggestedParams:f}),g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:s.id,amount:s.amount,suggestedParams:f}),I=0===i.id?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:i.amount,suggestedParams:f}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),x=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:O,suggestedParams:f}),h=a.default.assignGroupID([A,m,g,I,x]),t.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function An(){return(An=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,s=n.account,t.next=4,o([r]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=r.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:a.default.signLogicSigTransactionObject(t.txn,s).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hn(){return(hn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.signedTxns,s=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,pe({client:n,pool:r,accountAddr:s});case 5:return c=t.sent,t.next=8,st(n,[o]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=it(a),I=ct(a),t.next=17,pe({client:n,pool:r,accountAddr:s});case 17:return x=t.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:I});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new M(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Tn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var o=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(o<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(o,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:o-BigInt(1e3),share:1}}var s=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=si)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Gt(d),priceImpact:Wt(s({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function zn(t){return Jn.apply(this,arguments)}function Jn(){return(Jn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:s.id,assetOutAmount:BigInt(s.amount)+A,excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yn(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap on a non-existent pool");var o,s,u,i=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,s=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,s=n.asset1,u=n.asset2}if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=s*u/(u-i)-s,p=c*qn/(qn-Xn),l=p-c,d=et(a.assetOut,Number(i))/et(a.assetIn,Number(p)),f=et(a.assetOut,Number(u))/et(a.assetIn,Number(s)),m=rt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:o,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function Kn(t){return Hn.apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:s.id,assetOutAmount:BigInt(s.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,r,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,o=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new M({pool:r,swapType:a,txGroup:o},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:o[Cn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:o[Cn.ASSET_IN_TXN_INDEX].txn.amount},l={id:o[Cn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:o[Cn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,zn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Kn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",s(s({},d),{},{groupID:ct(o),fees:it(o)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new M(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Cn||(Cn={}));var $n={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Wn({pool:e,reserves:n,assetIn:r,decimals:a}):Yn({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Wn,getFixedOutputSwapQuote:Yn,generateTxns:function(t){return Gn.apply(this,arguments)},signTxns:function(t){return Un.apply(this,arguments)},execute:function(t){return Zn.apply(this,arguments)},executeFixedOutputSwap:Kn},tr=4*t.ALGORAND_MIN_TX_FEE;function er(e){return(St[e]+2)*t.ALGORAND_MIN_TX_FEE}function nr(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:.003;return Math.ceil(1/t)}function rr(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:.003;return tn&&(e=a,n=o)}return e}function pr(){return(pr=c(u().mark((function t(e){var n,r,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Vt(s(s({},e),{},{route:e.quote.data.route})));case 2:if(n=e.client,r=e.initiatorAddr,o=e.slippage,i=e.swapType,c=e.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(I=[l.asset1ID,l.asset2ID]).includes(f)&&I.includes(m)&&f!==m){t.next=9;break}throw new M({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return x=t.sent,A=j(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:tt("positive",o,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:tt("negative",o,d.assetOutAmount),y=A?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,suggestedParams:x}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,assetIndex:f,suggestedParams:x}),(w=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:l.validatorAppID,appArgs:[Et,bt[i],a.default.encodeUint64(T)],note:mt.getAppCallTxnNoteWithClientName(N.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=lr(i),(v=[])[wt.INPUT_TXN]=y,v[wt.APP_CALL_TXN]=w,_=a.default.assignGroupID(v),t.abrupt("return",[{txn:_[wt.INPUT_TXN],signers:[r]},{txn:_[wt.APP_CALL_TXN],signers:[r]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function lr(e){return(St[e]+1)*t.ALGORAND_MIN_TX_FEE}function dr(){return(dr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,o=e.quote,s=e.txGroup,i=e.signedTxns,t.next=3,st(a,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Ge(a,s);case 10:return m=t.sent,g=zt(o),I=Jt(o).id,x=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===I})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:x&&{amount:BigInt(g.amount)-BigInt(x.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:I},quote:o,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fr(){return(fr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.amount,a=e.assetInID,o=e.assetOutID,s=e.decimals,i=e.network,c=e.isSwapRouterEnabled,p=e.pool,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,xr({assetInID:a,assetOutID:o,amount:r,decimals:s,isSwapRouterEnabled:c,network:i,pool:p});case 4:l=t.sent,t.next=10;break;case 7:return t.next=9,hr({amount:r,assetInID:a,assetOutID:o,decimals:s,isSwapRouterEnabled:c,network:i,pool:p});case 9:l=t.sent;case 10:return t.abrupt("return",l);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=t[0].reason;if(2===t.length){var n=t[1].reason;if(ar(e)&&!ar(n))throw e;throw n}throw e}return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function gr(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,o=t.pool;if(!Ne.isPoolReady(o))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");var s,u,i=BigInt(e),c=o.totalFeeShare;if(n===o.asset1ID)s=o.asset1Reserves,u=o.asset2Reserves;else{if(n!==o.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Input asset (#".concat(n,") doesn't belong to the pool ").concat(o.account.address(),"."));s=o.asset2Reserves,u=o.asset1Reserves}var p=yr({inputSupply:s,outputSupply:u,swapInputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapOutputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(l>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(rr(Number(e)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");return{assetInID:n,assetInAmount:i,assetOutID:r,assetOutAmount:l,swapFee:Number(d),rate:et(a.assetOut,Number(l))/et(a.assetIn,Number(i)),priceImpact:f}}function Ir(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,o=t.pool;if(!o||!Ne.isPoolReady(o))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");var s,u,i=BigInt(e),c=o.totalFeeShare;if(r===o.asset1ID)s=o.asset2Reserves,u=o.asset1Reserves;else{if(r!==o.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r,") doesn't belong to the pool ").concat(o.account.address(),"."));s=o.asset1Reserves,u=o.asset2Reserves}var p=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,o=t.decimals,s=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:s,totalFeeShare:a}),i=s+u,c=Wt({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:o.assetIn},assetOut:{amount:r,decimals:o.assetOut}});return{swapInputAmount:i,totalFeeAmount:u,priceImpact:c}}({inputSupply:s,outputSupply:u,swapOutputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapInputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(rr(Number(l)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");return{type:exports.SwapQuoteType.Direct,data:{pool:o,quote:{assetInID:n,assetInAmount:l,assetOutID:r,assetOutAmount:i,swapFee:Number(d),rate:et(a.assetOut,Number(i))/et(a.assetIn,Number(l)),priceImpact:f}}}}function xr(t){return Ar.apply(this,arguments)}function Ar(){return(Ar=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[],p?l.push(new Promise((function(t,e){try{var s=gr({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p});t({type:exports.SwapQuoteType.Direct,data:{pool:p,quote:s}})}catch(t){e(t)}}))):l.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools."))),s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedInput,network:i});case 2:if(!((e=t.sent).route.length<2)){t.next=5;break}throw new D(exports.SwapQuoteErrorType.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 5:return t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 6:case"end":return t.stop()}}),t)})))()),t.next=6,mr(l);case 6:return d=t.sent,t.abrupt("return",cr(d));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hr(t){return Tr.apply(this,arguments)}function Tr(){return(Tr=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[new Promise((function(t,e){try{t(Ir({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p}))}catch(t){e(t)}}))],s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedOutput,network:i});case 2:return e=t.sent,t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 4:case"end":return t.stop()}}),t)})))()),t.next=5,mr(l);case 5:return d=t.sent,t.abrupt("return",cr(d));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,o=t.decimals,s=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-s});return{swapOutputAmount:u,totalFeeAmount:s,priceImpact:Wt({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:o.assetIn},assetOut:{amount:u,decimals:o.assetOut}})}}var wr={getQuote:function(t){return fr.apply(this,arguments)},getFixedInputSwapQuote:xr,getFixedInputDirectSwapQuote:gr,getFixedOutputDirectSwapQuote:Ir,getFixedOutputSwapQuote:hr,generateTxns:function(t){return pr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return dr.apply(this,arguments)},calculateFixedInputSwap:yr};function vr(t,e){var n,r,a=BigInt(t),o=e.issuedLiquidity;return o>a+BigInt(1e3)?(n=a*e.asset1/o,r=a*e.asset2/o):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function _r(){return(_r=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.account.address(),m=o.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:s,suggestedParams:d}),(I=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(tt("negative",l,c)),a.default.encodeUint64(tt("negative",l,p))],accounts:[f],foreignAssets:[o.asset1ID,o.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(x=[])[Rn.ASSET_TRANSFER_TXN]=g,x[Rn.APP_CALL_TXN]=I,A=a.default.assignGroupID(x),e.abrupt("return",[{txn:A[Rn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[Rn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Sr(){return(Sr=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.asset1ID,m=o.asset2ID,g=o.account.address(),I=o.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(x=0,A=0,h=tt("negative",l,p),c!==f){e.next=17;break}x=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}x=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,assetIndex:I,amount:i,suggestedParams:d}),(y=a.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(x),a.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(w=[])[Rn.ASSET_TRANSFER_TXN]=T,w[Rn.APP_CALL_TXN]=y,v=a.default.assignGroupID(w),e.abrupt("return",[{txn:v[Rn.ASSET_TRANSFER_TXN],signers:[s]},{txn:v[Rn.APP_CALL_TXN],signers:[s]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Er(){return(Er=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,o=e.signedTxns,t.next=3,st(r,[o]);case 3:return s=t.sent,i=v(s,1),c=i[0].txnID,t.next=8,Ge(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var br,Dr,Or={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=vr(a,n),s=o.asset1OutputAmount,u=o.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:s},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,o=t.assetOutID,s=t.decimals,u=BigInt(a),i=vr(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(o===n.asset1ID){var d=wr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:s}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(o!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var I=wr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:s}),x=I.swapOutputAmount,A=I.totalFeeAmount,h=I.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return _r.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Sr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Er.apply(this,arguments)}},Nr=(f(br={},N.V1_1,jn),f(br,N.V2,Or),br),Pr=(f(Dr={},N.V1_1,$n),f(Dr,N.V2,wr),f(Dr,"getQuote",(function(t){var e=t.type,n=t.pools;if(!t.isSwapRouterEnabled&&n.every((function(t){return Ee(t.reserves)})))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");if(e===exports.SwapType.FixedInput)return function(t){return sr.apply(this,arguments)}(t);if(e===exports.SwapType.FixedOutput)return function(t){return ur.apply(this,arguments)}(t);throw new D(exports.SwapQuoteErrorType.InvalidSwapTypeError,"Invalid swap type")})),f(Dr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1?$n.generateTxns(s(s({},t),{},{quote:t.quote.data})):wr.generateTxns(t)})),f(Dr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1){var e=t.quote.data.pool;return $n.signTxns(s({pool:e},t))}return wr.signTxns(t)})),f(Dr,"execute",(function(t){return t.contractVersion===N.V1_1?$n.execute(t):wr.execute(t)})),Dr);function kr(){return(kr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,Lr({txGroup:a,pool:r,initiatorSigner:o});case 4:return s=t.sent,t.next=7,st(n,[s]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:it(a),confirmedRound:d,txnID:l,groupID:ct(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function Lr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.pool,o=e.initiatorSigner,t.next=3,o([n]);case 3:return s=t.sent,i=v(s,1),c=i[0],p=r.account.lsig,l=n.map((function(t,e){return 0===e?c:a.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Fr(){return Fr=c(u().mark((function t(e){var n,r,o,s,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.data,o=e.initiatorSigner,t.prev=1,s=r.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:it(e),groupID:ct(e),lsig:n.account.lsig}})),t.next=5,o(s.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(s.map((function(t,e){return new Promise(function(){var r=c(u().mark((function r(o,s){var c,p,l,d,f,m;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,c=t.txns.map((function(n,r){return 0===r?i[e]:a.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),r.next=4,st(n,[c]);case 4:p=r.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),r.next=15;break;case 12:r.prev=12,r.t0=r.catch(0),s(r.t0);case 15:case"end":return r.stop()}}),r,null,[[0,12]])})));return function(t,e){return r.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Fr.apply(this,arguments)}function Br(){return(Br=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,o=e.assetID,s=e.assetOut,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=r.account.address(),l=a.default.makeApplicationNoOpTxnFromObject({from:p,appIndex:r.validatorAppID,appArgs:[lt("redeem")],note:mt.getAppCallTxnNoteWithClientName(r.contractVersion),accounts:[i],foreignAssets:0==r.asset2ID?[r.asset1ID,r.poolTokenID]:[r.asset1ID,r.asset2ID,r.poolTokenID],suggestedParams:c}),d=0===o?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:i,amount:BigInt(s),suggestedParams:c}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:i,assetIndex:o,amount:BigInt(s),suggestedParams:c}),f=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:p,amount:l.fee+d.fee,note:O,suggestedParams:c}),m=a.default.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[i]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Mr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,o=e.liquidityAssetID,s=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(s.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[lt("commit"),i],note:U([lt("tinymanStaking/v1:b"),c,t.encodeUint64(o),i])})}function Qr(){return(Qr=c(u().mark((function e(n){var r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,s=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Mr({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof s){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[lt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=P,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Ln,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=en,exports.CONTRACT_VERSION=N,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=k,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Nr,exports.Swap=Pr,exports.SwapQuoteError=D,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Fn,exports.V2AddLiquidityTxnIndices=cn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=tt,exports.calculateAccountMinimumRequiredBalance=se,exports.calculatePriceImpact=Wt,exports.calculateSwapRate=Gt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=rr,exports.poolUtils=Ne,exports.prepareCommitTransactions=function(t){return Qr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Fr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return kr.apply(this,arguments)},exports.sendAndWaitRawTransaction=st,exports.sumUpTxnFees=it,exports.tinymanContract_v1_1=$t,exports.tinymanContract_v2=ne,exports.tinymanJSSDKConfig=mt; diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index d4db7893..aa6a0944 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -307,13 +307,6 @@ function getFixedInputDirectSwapQuote({ ); } - if (isSwapAssetInAmountLow(Number(amount))) { - throw new SwapQuoteError( - SwapQuoteErrorType.LowSwapAmountError, - "Swap amount is too low." - ); - } - const assetInAmount = BigInt(amount); const totalFeeShare = pool.totalFeeShare!; @@ -348,6 +341,13 @@ function getFixedInputDirectSwapQuote({ ); } + if (isSwapAssetInAmountLow(Number(amount))) { + throw new SwapQuoteError( + SwapQuoteErrorType.LowSwapAmountError, + "Swap amount is too low." + ); + } + return { assetInID, assetInAmount, @@ -407,17 +407,17 @@ function getFixedOutputDirectSwapQuote({ decimals }); - if (isSwapAssetInAmountLow(Number(swapInputAmount))) { + if (assetOutAmount > outputSupply) { throw new SwapQuoteError( - SwapQuoteErrorType.LowSwapAmountError, - "Swap amount is too low." + SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError, + "Output amount exceeds available liquidity." ); } - if (assetOutAmount > outputSupply) { + if (isSwapAssetInAmountLow(Number(swapInputAmount))) { throw new SwapQuoteError( - SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError, - "Output amount exceeds available liquidity." + SwapQuoteErrorType.LowSwapAmountError, + "Swap amount is too low." ); } From 5c0b09d3b61311997bb4a0438887cb45fdbb10d1 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Tue, 21 Mar 2023 17:10:07 +0300 Subject: [PATCH 26/39] refactor: re-organize files, naming changes --- dist/index.d.ts | 1 - dist/index.js | 2 +- dist/swap/common/utils.d.ts | 23 ++++++++- dist/swap/types.d.ts | 10 +++- dist/swap/utils.d.ts | 33 ++----------- dist/swap/v2/util.d.ts | 1 + src/index.ts | 1 - src/swap/common/utils.ts | 85 +++++++++++++++++++++++++++++++- src/swap/types.ts | 11 ++++- src/swap/utils.ts | 97 ++++--------------------------------- src/swap/v2/index.ts | 19 +++----- src/swap/v2/util.ts | 7 +++ 12 files changed, 151 insertions(+), 139 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index 38a11ceb..14c5123d 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -30,7 +30,6 @@ export { V1_1_REMOVE_LIQUIDITY_TXN_COUNT } from "./remove-liquidity/v1_1/constan export { V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT } from "./remove-liquidity/v2/constants"; export { RemoveLiquidity } from "./remove-liquidity"; export type { SwapQuote, V1SwapExecution, V2SwapExecution, DirectSwapQuote, SwapRoute, GenerateSwapTxnsParams } from "./swap/types"; -export { getSwapTotalFee } from "./swap/utils"; export * from "./swap/v2/util"; export { SwapType } from "./swap/constants"; export { Swap } from "./swap"; diff --git a/dist/index.js b/dist/index.js index 3bc6d3cc..0bd4e307 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r,a=n(t);function o(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function s(t){for(var e=1;e=0;--a){var o=this.tryEntries[a],s=o.completion;if("root"===o.tryLoc)return r("end");if(o.tryLoc<=this.prev){var u=n.call(o,"catchLoc"),i=n.call(o,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),v(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;v(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:S(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,o,s){try{var u=t[o](s),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function s(t){i(o,r,a,s,u,"next",t)}function u(t){i(o,r,a,s,u,"throw",t)}s(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,s=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){u=!0,o=t},f:function(){try{s||null==n.return||n.return()}finally{if(u)throw o}}}}exports.SwapQuoteErrorType=void 0,(r=exports.SwapQuoteErrorType||(exports.SwapQuoteErrorType={})).SwapRouterStaleDataError="SwapRouterStaleDataError",r.SwapRouterNoRouteError="SwapRouterNoRouteError",r.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",r.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",r.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",r.NoAvailablePoolError="NoAvailablePoolError",r.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",r.UnknownError="UnknownError",r.LowSwapAmountError="LowSwapAmountError",r.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",r.InvalidSwapTypeError="InvalidSwapTypeError";var D=function(t){m(n,h(Error));var e=w(n);function n(t,r){var a;return p(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(T(a),n),a}return d(n)}(),O=Uint8Array.from([1]),N={V1_1:"v1_1",V2:"v2"},P={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},k={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},L="- would result negative",R="logic eval error:",F="exceeds schema integer count",B=/transaction \w+:/,M=function(t){m(n,h(Error));var e=w(n);function n(t,r){var a;p(this,n);for(var o=arguments.length,s=new Array(o>2?o-2:0),u=2;ur?[s(s({},t),{},{id:n}),s(s({},e),{},{id:r})]:[s(s({},e),{},{id:r}),s(s({},t),{},{id:n})]}function C(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function j(t){return 0===Number(t)}function X(t){return Number(t.id)}function q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,o=void 0!==a&&a,s={},u=b(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}s[o?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return s}function U(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,o=b(t);try{for(o.s();!(e=o.n()).done;){var s=e.value;r.set(s,a),a+=s.length}}catch(t){o.e(t)}finally{o.f()}return r}var G=100000n,W=100000n,z=100000n,J=25000n+25000n,Y=25000n+3500n;function K(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],o=t["created-apps"]||[],s=t.assets||[];return G+W*BigInt(s.length)+z*BigInt(o.length+a.length)+Y*r+J*n}function H(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,H(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function tt(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function et(t,e){var n=Number(t);return rt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function nt(t,e){return rt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function rt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(ot(e),2),o=a[0],s=a[1],u=v(ot(Math.round(Number(at(o,s+r)))),2),i=u[0],c=u[1];return Number(at(i,c-r))}return Math.round(e)}function at(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ot(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function st(t,e){return ut.apply(this,arguments)}function ut(){return(ut=c(u().mark((function t(e,n){var r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=b(n),t.prev=3,a.s();case 5:if((o=a.n()).done){t.next=18;break}return s=o.value,t.next=9,e.sendRawTransaction(s).do();case 9:return i=t.sent,c=i.txId,t.next=13,Z(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new M(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function it(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ct(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function pt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function lt(t){return(new TextEncoder).encode(t)}function dt(t){return Boolean(t)&&void 0!==t.fallback_message}var ft,mt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return lt("tinyman/".concat(t===N.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),gt=(f(ft={},N.V1_1,{testnet:62368684,mainnet:552635992}),f(ft,N.V2,{testnet:148607e3,mainnet:1002541853}),ft);function It(t,e){var n=gt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function xt(){return(xt=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationOptInTxnFromObject({from:s,appIndex:It(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var At;function ht(){return(ht=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationClearStateTxnFromObject({from:s,appIndex:It(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(At=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",At.FixedOutput="fixed-output";var Tt,yt,wt;!function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(wt||(wt={}));var vt,_t,St=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),Et=lt("swap"),bt=(f(yt={},exports.SwapType.FixedInput,lt("fixed-input")),f(yt,exports.SwapType.FixedOutput,lt("fixed-output")),yt),Dt={ASSET_OPT_IN:lt("asset_opt_in")},Ot={testnet:159521633,mainnet:0},Nt=(f(vt={},exports.SwapType.FixedInput,7),f(vt,exports.SwapType.FixedOutput,8),vt),Pt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function kt(t){var e=Bt(t),n=e.assetIn,r=e.assetOut;return et(r.asset.decimals,Number(r.amount))/et(n.asset.decimals,Number(n.amount))}function Lt(t){if(!Ot[t])throw new Error("Unknown network or network not supported: ".concat(t));return Ot[t]}function Rt(t){return t[t.length-1].quote.amount_out}function Ft(t){return t[0].quote.amount_in}function Bt(t){return{assetIn:Ft(t),assetOut:Rt(t)}}function Mt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function e(n){var r,o,s,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.routerAppID,s=n.assetIDs,i=n.accountAddress,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o,appArgs:[Dt.ASSET_OPT_IN],foreignAssets:s,suggestedParams:c}),l=s.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v,_,S,E,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,o=n.client,s=n.network,i=n.swapType,c=n.route,e.next=3,o.getTransactionParams().do();case 3:return p=e.sent,l=[X(c[0].quote.amount_in.asset),X(c[0].quote.amount_out.asset),X(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Ft(c).amount),Number(Rt(c).amount)],I=g[0],x=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=j(d),w=Lt(s),v=y?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(w),amount:I,suggestedParams:p}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(w),amount:I,assetIndex:d,suggestedParams:p}),(_=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:w,appArgs:[Et,bt[i],a.default.encodeUint64(x)],foreignApps:[It(s,N.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Nt[i]+1),S=[v,_],e.next=15,jt({client:o,routerAppID:w,assetIDs:[d,f,m]});case 15:if(!((E=e.sent).length>0)){e.next=21;break}return e.next=19,Mt({client:o,accountAddress:r,assetIDs:E,routerAppID:w});case 19:b=e.sent,S.unshift(b);case 21:return D=a.default.assignGroupID(S),e.abrupt("return",D.map((function(t){return{txn:t,signers:[r]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(u().mark((function e(n){var r,a,o,s,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,o=n.assetIDs,s=t.getApplicationAddress(a),e.next=4,r.accountInformation(s).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=o.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qt(t){return Ut.apply(this,arguments)}function Ut(){return(Ut=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.swapType,s=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Pt[s].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(!dt(p)){t.next=13;break}throw new D(p.type,p.fallback_message);case 13:throw new D(exports.SwapQuoteErrorType.UnknownError,"Unknown error");case 14:return t.abrupt("return",p);case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Gt(t){var e=t.assetIn,n=t.assetOut;return et(n.decimals,Number(n.amount))/et(e.decimals,Number(e.amount))}function Wt(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,o=Gt({assetIn:r,assetOut:a}),s=et(a.decimals,Number(n))/et(r.decimals,Number(e));return rt({decimalPlaces:5},Math.abs(o/s-1))}function zt(t){if(t.type===exports.SwapQuoteType.Router){var e=Ft(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function Jt(t){if(t.type===exports.SwapQuoteType.Router){var e=Rt(t.data.route);return{id:X(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function Yt(t){return t.type===exports.SwapQuoteType.Direct?t.data.pool.contractVersion:N.V2}exports.SwapQuoteType=void 0,(_t=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",_t.Router="router";var Kt={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},Ht={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},Zt=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),$t=new(function(n){m(a,Zt);var r=w(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=It(n.network,N.V1_1),a=v(C(n.asset1ID,n.asset2ID),2),o=a[0],s=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:o,asset_id_2:s,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(s(s({},i),{},{minimum_required_balance:se(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),o(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function oe(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function se(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ue,ie,ce=lt("e");function pe(t){return le.apply(this,arguments)}function le(){return(le=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v,_,S,E;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.pool,i=r.accountAddr,n.next=3,o.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=s.account.address(),g=b(p),n.prev=10,g.s();case 12:if((I=g.n()).done){n.next=31;break}if((x=I.value).id==s.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=x["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=q({stateArray:A}),T=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.asset1ID)])),y=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.asset2ID)])),w=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,ce,a.default.encodeUint64(s.poolTokenID)])),v=h[T],_=h[y],S=h[w],"bigint"==typeof v&&(l=v),"bigint"==typeof _&&(d=_),"bigint"==typeof S&&(f=S);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((E={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function de(){return(de=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,I,x,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.accountAddr,i=r.validatorAppID,n.next=3,o.accountInformation(s).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mge)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ge,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function he(){return he=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ae(n,r);case 6:return s=t.sent,i=oe(s,It(a,N.V1_1)),c=null,i&&(l=s["created-assets"][0],p=l.index,c={asset1ID:i[fe[N.V1_1].asset1],asset2ID:i[fe[N.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),he.apply(this,arguments)}var Te=Object.freeze({__proto__:null,getPoolInfo:Ie,getPoolReserves:function(t,e){return Ae.apply(this,arguments)},getPoolAssets:function(t){return he.apply(this,arguments)}});function ye(t){return we.apply(this,arguments)}function we(){return(we=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,o=e.asset2ID,s=re(N.V2),i=s.generateLogicSigAccountForPool(e),c=It(r,N.V2),p=i.address(),l=C(a,o),t.next=8,ae(n,p);case 8:return d=t.sent,f=oe(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:N.V2},f&&(m.asset1ProtocolFees=BigInt(f[fe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[fe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[fe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[fe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[fe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[fe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[fe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[fe.v2.totalFeeShare]),m.poolTokenID=Number(f[fe.v2.poolTokenID]),m.asset1ID=Number(f[fe.v2.asset1]),m.asset2ID=Number(f[fe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ve(){return(ve=c(u().mark((function t(e,n){var r,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ae(e,n.account.address());case 2:return r=t.sent,a=oe(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[fe.v2.asset1Reserves]),o.asset2=BigInt(a[fe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[fe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _e(){return(_e=c(u().mark((function t(e){var n,r,a,o,s,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,ae(n,r);case 3:return o=t.sent,s=oe(o,It(a,N.V2)),i=null,s&&(i={asset1ID:s[fe[N.V2].asset1],asset2ID:s[fe[N.V2].asset2],poolTokenID:s[fe[N.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Se=Object.freeze({__proto__:null,getPoolInfo:ye,getPoolReserves:function(t,e){return ve.apply(this,arguments)},getPoolAssets:function(t){return _e.apply(this,arguments)}});function Ee(t){return Boolean(t&&!(t.asset1+t.asset2))}var be,De,Oe=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Ee(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=et(t.asset1,e.asset1)/et(t.asset2,e.asset2)),r},isPoolEmpty:Ee,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Ie(t),ye(t)])}}),Ne=s((f(be={},N.V1_1,s(s({},Te),Oe)),f(be,N.V2,s(s({},Se),Oe)),be),Oe);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(De||(De={}));var Pe=96e4,ke=859e3;function Le(){return(Le=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,_,S,E,b,D,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.asset_1,s=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=V(o,s),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],I=g.id,x=g.unit_name,A=j(I),h=It(r,N.V1_1),T=$t.generateLogicSigAccountForPool({network:r,asset1ID:f,asset2ID:I}),y=T.address(),w=a.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:mt.getAppCallTxnNoteWithClientName(N.V1_1),appArgs:[lt("bootstrap"),a.default.encodeUint64(f),a.default.encodeUint64(I)],foreignAssets:A?[f]:[I],suggestedParams:c}),_=a.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:k.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),S=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Re(A),suggestedParams:c}),(b=[])[De.FUNDING_TXN]=E,b[De.VALIDATOR_APP_CALL]=w,b[De.POOL_TOKEN_CREATE]=_,b[De.ASSET1_OPT_IN]=S,A||(b[De.ASSET2_OPT_IN]=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:I,amount:0,suggestedParams:c})),D=a.default.assignGroupID(b),O=[{txn:D[De.FUNDING_TXN],signers:[i]},{txn:D[De.VALIDATOR_APP_CALL],signers:[y]},{txn:D[De.POOL_TOKEN_CREATE],signers:[y]},{txn:D[De.ASSET1_OPT_IN],signers:[y]}],D[De.ASSET2_OPT_IN]&&O.push({txn:D[De.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Re(t){return t?Pe:ke}function Fe(){return(Fe=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=C(s,i),f=v(d,2),m=f[0],g=f[1],I=$t.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===De.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,o=n.blob;return x.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Be(t){return Me.apply(this,arguments)}function Me(){return(Me=c(u().mark((function t(e){var n,r,a,o,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Z(n,a[De.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(s=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(s));case 10:return t.abrupt("return",{poolTokenID:s});case 13:throw t.prev=13,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function Qe(){return(Qe=c(u().mark((function t(e){var n,r,a,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Be({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",Ne.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ve,Ce={generateTxns:function(t){return Le.apply(this,arguments)},signTxns:function(t){return Fe.apply(this,arguments)},execute:function(t){return Qe.apply(this,arguments)},getBootstrapFundingTxnAmount:Re};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(Ve||(Ve={}));var je=5,Xe=6;function qe(t,e){return Ue.apply(this,arguments)}function Ue(){return(Ue=c(u().mark((function e(n,r){var a,o,s;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return s=e.t0,e.abrupt("return",s);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ge(t,e){return We.apply(this,arguments)}function We(){return(We=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qe(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ze(){return(ze=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,_,S;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.network,s=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return p=e.sent,l=It(o,N.V2),d=t.getApplicationAddress(l),f=V(s,i),m=v(f,2),g=m[0].id,I=m[1].id,e.next=9,Ne.v2.getPoolInfo({client:r,network:o,asset1ID:g,asset2ID:I});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(s.unit_name,"-").concat(i.unit_name," already exists"));case 12:return x=ne.generateLogicSigAccountForPool({network:o,asset1ID:g,asset2ID:I}),A=x.address(),h=j(I),(T=a.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[lt("bootstrap")],note:mt.getAppCallTxnNoteWithClientName(N.V2),foreignAssets:[g,I],rekeyTo:d,suggestedParams:p})).fee=Ye(h),y=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:Je(h),suggestedParams:p}),(w=[])[Ve.FUNDING_TXN]=y,w[Ve.VALIDATOR_APP_CALL]=T,_=a.default.assignGroupID(w),(S=[])[Ve.FUNDING_TXN]={txn:_[Ve.FUNDING_TXN],signers:[c]},S[Ve.VALIDATOR_APP_CALL]={txn:_[Ve.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",S);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Je(t){return function(t,e){var n=re(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(N.V2,t)+Ye(t)+1e5}function Ye(e){return((e?je:Xe)+1)*t.ALGORAND_MIN_TX_FEE}function Ke(){return(Ke=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=C(s,i),f=v(d,2),m=f[0],g=f[1],I=ne.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),x=[],A=n.map((function(t,e){if(e===Ve.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,I),r=n.txID,o=n.blob;return x.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function He(){return(He=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,qe(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(fe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Ne.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 21:throw t.prev=21,t.t3=t.catch(1),new M(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Ze,$e={generateTxns:function(t){return ze.apply(this,arguments)},signTxns:function(t){return Ke.apply(this,arguments)},execute:function(t){return He.apply(this,arguments)},getBootstrapFundingTxnAmount:Je,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+Je(e)}};var tn,en=(f(Ze={},N.V1_1,Ce),f(Ze,N.V2,$e),f(Ze,"generateTxns",(function(t){return t.contractVersion===N.V1_1?Ce.generateTxns(t):$e.generateTxns(t)})),f(Ze,"signTxns",(function(t){return t.contractVersion===N.V1_1?Ce.signTxns(t):$e.signTxns(t)})),f(Ze,"execute",(function(t){return t.contractVersion===N.V1_1?Ce.execute(t):$e.execute(t)})),f(Ze,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===N.V1_1?Ce.getBootstrapFundingTxnAmount(n):$e.getBootstrapFundingTxnAmount(n)})),Ze);exports.V1_1AddLiquidityTxnIndices=void 0,(tn=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[tn.FEE_TXN=0]="FEE_TXN",tn[tn.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",tn[tn.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",tn[tn.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",tn[tn.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var nn,rn,an,on,sn=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(on=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",on.FLEXIBLE="flexible",on.INITIAL="initial";var un,cn=(f(nn={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(nn,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(nn,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),nn),pn=(f(rn={},exports.V2AddLiquidityType.INITIAL,1),f(rn,exports.V2AddLiquidityType.SINGLE,2),f(rn,exports.V2AddLiquidityType.FLEXIBLE,2),rn),ln=(f(an={},exports.V2AddLiquidityType.INITIAL,3),f(an,exports.V2AddLiquidityType.FLEXIBLE,3),f(an,exports.V2AddLiquidityType.SINGLE,2),an);function dn(t){var e,n,r,a=t.reserves,o=t.totalFeeShare,s=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(s.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,I=BigInt(s.amount)-m,x=BigInt(u.amount)-g;if(I>x){var A=I;r=fn(A,o),e={id:s.id,amount:A+r,decimals:s.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=x;r=fn(h,o),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:s.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:s.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Wt({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function fn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function mn(e){return(pn[e]+1)*t.ALGORAND_MIN_TX_FEE}var gn=lt("add_liquidity"),In=(f(un={},N.V1_1,[lt("mint")]),f(un,N.V2,{INITIAL_LIQUIDITY:[lt("add_initial_liquidity")],SINGLE_ASSET_MODE:[gn,lt("single")],FLEXIBLE_MODE:[gn,lt("flexible")]}),un);function xn(){return(xn=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.poolAddress,s=e.asset1In,i=e.asset2In,c=e.poolTokenOut,p=e.slippage,l=e.initiatorAddr,d=tt("negative",p,c.amount),t.next=4,n.getTransactionParams().do();case 4:return f=t.sent,m=a.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:It(r,N.V1_1),appArgs:In.v1_1,accounts:[l],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==i.id?[s.id,c.id]:[s.id,i.id,c.id],suggestedParams:f}),g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:s.id,amount:s.amount,suggestedParams:f}),I=0===i.id?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:i.amount,suggestedParams:f}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),x=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:O,suggestedParams:f}),h=a.default.assignGroupID([A,m,g,I,x]),t.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function An(){return(An=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,s=n.account,t.next=4,o([r]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=r.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:a.default.signLogicSigTransactionObject(t.txn,s).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hn(){return(hn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.signedTxns,s=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,pe({client:n,pool:r,accountAddr:s});case 5:return c=t.sent,t.next=8,st(n,[o]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=it(a),I=ct(a),t.next=17,pe({client:n,pool:r,accountAddr:s});case 17:return x=t.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:I});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new M(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Tn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var o=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(o<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(o,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:o-BigInt(1e3),share:1}}var s=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=si)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Gt(d),priceImpact:Wt(s({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function zn(t){return Jn.apply(this,arguments)}function Jn(){return(Jn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:s.id,assetOutAmount:BigInt(s.amount)+A,excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yn(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap on a non-existent pool");var o,s,u,i=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,s=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,s=n.asset1,u=n.asset2}if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=s*u/(u-i)-s,p=c*qn/(qn-Xn),l=p-c,d=et(a.assetOut,Number(i))/et(a.assetIn,Number(p)),f=et(a.assetOut,Number(u))/et(a.assetIn,Number(s)),m=rt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:o,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function Kn(t){return Hn.apply(this,arguments)}function Hn(){return(Hn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,pe({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,pe({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(I=c.excessAsset1,x=g.excessAsset1):(I=c.excessAsset2,x=g.excessAsset2),(A=x-I)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:s.id,assetOutAmount:BigInt(s.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return(Zn=c(u().mark((function t(e){var n,r,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,o=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new M({pool:r,swapType:a,txGroup:o},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:o[Cn.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:o[Cn.ASSET_IN_TXN_INDEX].txn.amount},l={id:o[Cn.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:o[Cn.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,zn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Kn({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",s(s({},d),{},{groupID:ct(o),fees:it(o)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new M(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(Cn||(Cn={}));var $n={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Wn({pool:e,reserves:n,assetIn:r,decimals:a}):Yn({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Wn,getFixedOutputSwapQuote:Yn,generateTxns:function(t){return Gn.apply(this,arguments)},signTxns:function(t){return Un.apply(this,arguments)},execute:function(t){return Zn.apply(this,arguments)},executeFixedOutputSwap:Kn},tr=4*t.ALGORAND_MIN_TX_FEE;function er(e){return(St[e]+2)*t.ALGORAND_MIN_TX_FEE}function nr(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:.003;return Math.ceil(1/t)}function rr(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:.003;return tn&&(e=a,n=o)}return e}function pr(){return(pr=c(u().mark((function t(e){var n,r,o,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v,_;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.quote.type!==exports.SwapQuoteType.Router){t.next=2;break}return t.abrupt("return",Vt(s(s({},e),{},{route:e.quote.data.route})));case 2:if(n=e.client,r=e.initiatorAddr,o=e.slippage,i=e.swapType,c=e.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,g=l.account.address(),(I=[l.asset1ID,l.asset2ID]).includes(f)&&I.includes(m)&&f!==m){t.next=9;break}throw new M({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(g,"."));case 9:return t.next=11,n.getTransactionParams().do();case 11:return x=t.sent,A=j(f),h=i===exports.SwapType.FixedInput?d.assetInAmount:tt("positive",o,d.assetInAmount),T=i===exports.SwapType.FixedOutput?d.assetOutAmount:tt("negative",o,d.assetOutAmount),y=A?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,suggestedParams:x}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:g,amount:h,assetIndex:f,suggestedParams:x}),(w=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:l.validatorAppID,appArgs:[Et,bt[i],a.default.encodeUint64(T)],note:mt.getAppCallTxnNoteWithClientName(N.V2),accounts:[g],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=lr(i),(v=[])[wt.INPUT_TXN]=y,v[wt.APP_CALL_TXN]=w,_=a.default.assignGroupID(v),t.abrupt("return",[{txn:_[wt.INPUT_TXN],signers:[r]},{txn:_[wt.APP_CALL_TXN],signers:[r]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function lr(e){return(St[e]+1)*t.ALGORAND_MIN_TX_FEE}function dr(){return(dr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.client,o=e.quote,s=e.txGroup,i=e.signedTxns,t.next=3,st(a,[i]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=l.confirmedRound,f=l.txnID,t.next=10,Ge(a,s);case 10:return m=t.sent,g=zt(o),I=Jt(o).id,x=null==m||null===(n=m.find((function(t){return t.txn.txn.xaid===g.id})))||void 0===n?void 0:n.txn.txn,A=null==m||null===(r=m.find((function(t){return t.txn.txn.xaid===I})))||void 0===r?void 0:r.txn.txn,t.abrupt("return",{round:d,assetIn:x&&{amount:BigInt(g.amount)-BigInt(x.aamt||0),id:g.id},assetOut:A&&{amount:A.aamt,id:I},quote:o,txnID:f});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fr(){return(fr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.type,r=e.amount,a=e.assetInID,o=e.assetOutID,s=e.decimals,i=e.network,c=e.isSwapRouterEnabled,p=e.pool,n!==exports.SwapType.FixedInput){t.next=7;break}return t.next=4,xr({assetInID:a,assetOutID:o,amount:r,decimals:s,isSwapRouterEnabled:c,network:i,pool:p});case 4:l=t.sent,t.next=10;break;case 7:return t.next=9,hr({amount:r,assetInID:a,assetOutID:o,decimals:s,isSwapRouterEnabled:c,network:i,pool:p});case 9:l=t.sent;case 10:return t.abrupt("return",l);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=t[0].reason;if(2===t.length){var n=t[1].reason;if(ar(e)&&!ar(n))throw e;throw n}throw e}return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function gr(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,o=t.pool;if(!Ne.isPoolReady(o))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");var s,u,i=BigInt(e),c=o.totalFeeShare;if(n===o.asset1ID)s=o.asset1Reserves,u=o.asset2Reserves;else{if(n!==o.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Input asset (#".concat(n,") doesn't belong to the pool ").concat(o.account.address(),"."));s=o.asset2Reserves,u=o.asset1Reserves}var p=yr({inputSupply:s,outputSupply:u,swapInputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapOutputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(l>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(rr(Number(e)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");return{assetInID:n,assetInAmount:i,assetOutID:r,assetOutAmount:l,swapFee:Number(d),rate:et(a.assetOut,Number(l))/et(a.assetIn,Number(i)),priceImpact:f}}function Ir(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,o=t.pool;if(!o||!Ne.isPoolReady(o))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");var s,u,i=BigInt(e),c=o.totalFeeShare;if(r===o.asset1ID)s=o.asset2Reserves,u=o.asset1Reserves;else{if(r!==o.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r,") doesn't belong to the pool ").concat(o.account.address(),"."));s=o.asset1Reserves,u=o.asset2Reserves}var p=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,o=t.decimals,s=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:s,totalFeeShare:a}),i=s+u,c=Wt({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:o.assetIn},assetOut:{amount:r,decimals:o.assetOut}});return{swapInputAmount:i,totalFeeAmount:u,priceImpact:c}}({inputSupply:s,outputSupply:u,swapOutputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapInputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(rr(Number(l)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");return{type:exports.SwapQuoteType.Direct,data:{pool:o,quote:{assetInID:n,assetInAmount:l,assetOutID:r,assetOutAmount:i,swapFee:Number(d),rate:et(a.assetOut,Number(i))/et(a.assetIn,Number(l)),priceImpact:f}}}}function xr(t){return Ar.apply(this,arguments)}function Ar(){return(Ar=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[],p?l.push(new Promise((function(t,e){try{var s=gr({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p});t({type:exports.SwapQuoteType.Direct,data:{pool:p,quote:s}})}catch(t){e(t)}}))):l.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools."))),s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedInput,network:i});case 2:if(!((e=t.sent).route.length<2)){t.next=5;break}throw new D(exports.SwapQuoteErrorType.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 5:return t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 6:case"end":return t.stop()}}),t)})))()),t.next=6,mr(l);case 6:return d=t.sent,t.abrupt("return",cr(d));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hr(t){return Tr.apply(this,arguments)}function Tr(){return(Tr=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[new Promise((function(t,e){try{t(Ir({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p}))}catch(t){e(t)}}))],s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedOutput,network:i});case 2:return e=t.sent,t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 4:case"end":return t.stop()}}),t)})))()),t.next=5,mr(l);case 5:return d=t.sent,t.abrupt("return",cr(d));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,o=t.decimals,s=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-s});return{swapOutputAmount:u,totalFeeAmount:s,priceImpact:Wt({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:o.assetIn},assetOut:{amount:u,decimals:o.assetOut}})}}var wr={getQuote:function(t){return fr.apply(this,arguments)},getFixedInputSwapQuote:xr,getFixedInputDirectSwapQuote:gr,getFixedOutputDirectSwapQuote:Ir,getFixedOutputSwapQuote:hr,generateTxns:function(t){return pr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return dr.apply(this,arguments)},calculateFixedInputSwap:yr};function vr(t,e){var n,r,a=BigInt(t),o=e.issuedLiquidity;return o>a+BigInt(1e3)?(n=a*e.asset1/o,r=a*e.asset2/o):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function _r(){return(_r=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.account.address(),m=o.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:s,suggestedParams:d}),(I=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(tt("negative",l,c)),a.default.encodeUint64(tt("negative",l,p))],accounts:[f],foreignAssets:[o.asset1ID,o.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(x=[])[Rn.ASSET_TRANSFER_TXN]=g,x[Rn.APP_CALL_TXN]=I,A=a.default.assignGroupID(x),e.abrupt("return",[{txn:A[Rn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[Rn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Sr(){return(Sr=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,I,x,A,h,T,y,w,v;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.asset1ID,m=o.asset2ID,g=o.account.address(),I=o.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(x=0,A=0,h=tt("negative",l,p),c!==f){e.next=17;break}x=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}x=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,assetIndex:I,amount:i,suggestedParams:d}),(y=a.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Bn,a.default.encodeUint64(x),a.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(w=[])[Rn.ASSET_TRANSFER_TXN]=T,w[Rn.APP_CALL_TXN]=y,v=a.default.assignGroupID(w),e.abrupt("return",[{txn:v[Rn.ASSET_TRANSFER_TXN],signers:[s]},{txn:v[Rn.APP_CALL_TXN],signers:[s]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Er(){return(Er=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,o=e.signedTxns,t.next=3,st(r,[o]);case 3:return s=t.sent,i=v(s,1),c=i[0].txnID,t.next=8,Ge(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var br,Dr,Or={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=vr(a,n),s=o.asset1OutputAmount,u=o.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:s},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,o=t.assetOutID,s=t.decimals,u=BigInt(a),i=vr(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(o===n.asset1ID){var d=wr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:s}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(o!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var I=wr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:s}),x=I.swapOutputAmount,A=I.totalFeeAmount,h=I.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return _r.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Sr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Er.apply(this,arguments)}},Nr=(f(br={},N.V1_1,jn),f(br,N.V2,Or),br),Pr=(f(Dr={},N.V1_1,$n),f(Dr,N.V2,wr),f(Dr,"getQuote",(function(t){var e=t.type,n=t.pools;if(!t.isSwapRouterEnabled&&n.every((function(t){return Ee(t.reserves)})))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");if(e===exports.SwapType.FixedInput)return function(t){return sr.apply(this,arguments)}(t);if(e===exports.SwapType.FixedOutput)return function(t){return ur.apply(this,arguments)}(t);throw new D(exports.SwapQuoteErrorType.InvalidSwapTypeError,"Invalid swap type")})),f(Dr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1?$n.generateTxns(s(s({},t),{},{quote:t.quote.data})):wr.generateTxns(t)})),f(Dr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&Yt(t.quote)===N.V1_1){var e=t.quote.data.pool;return $n.signTxns(s({pool:e},t))}return wr.signTxns(t)})),f(Dr,"execute",(function(t){return t.contractVersion===N.V1_1?$n.execute(t):wr.execute(t)})),Dr);function kr(){return(kr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,Lr({txGroup:a,pool:r,initiatorSigner:o});case 4:return s=t.sent,t.next=7,st(n,[s]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:it(a),confirmedRound:d,txnID:l,groupID:ct(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function Lr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.pool,o=e.initiatorSigner,t.next=3,o([n]);case 3:return s=t.sent,i=v(s,1),c=i[0],p=r.account.lsig,l=n.map((function(t,e){return 0===e?c:a.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Fr(){return Fr=c(u().mark((function t(e){var n,r,o,s,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.data,o=e.initiatorSigner,t.prev=1,s=r.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:it(e),groupID:ct(e),lsig:n.account.lsig}})),t.next=5,o(s.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(s.map((function(t,e){return new Promise(function(){var r=c(u().mark((function r(o,s){var c,p,l,d,f,m;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,c=t.txns.map((function(n,r){return 0===r?i[e]:a.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),r.next=4,st(n,[c]);case 4:p=r.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),r.next=15;break;case 12:r.prev=12,r.t0=r.catch(0),s(r.t0);case 15:case"end":return r.stop()}}),r,null,[[0,12]])})));return function(t,e){return r.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Fr.apply(this,arguments)}function Br(){return(Br=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,o=e.assetID,s=e.assetOut,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=r.account.address(),l=a.default.makeApplicationNoOpTxnFromObject({from:p,appIndex:r.validatorAppID,appArgs:[lt("redeem")],note:mt.getAppCallTxnNoteWithClientName(r.contractVersion),accounts:[i],foreignAssets:0==r.asset2ID?[r.asset1ID,r.poolTokenID]:[r.asset1ID,r.asset2ID,r.poolTokenID],suggestedParams:c}),d=0===o?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:i,amount:BigInt(s),suggestedParams:c}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:i,assetIndex:o,amount:BigInt(s),suggestedParams:c}),f=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:p,amount:l.fee+d.fee,note:O,suggestedParams:c}),m=a.default.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[i]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Mr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,o=e.liquidityAssetID,s=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(s.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[lt("commit"),i],note:U([lt("tinymanStaking/v1:b"),c,t.encodeUint64(o),i])})}function Qr(){return(Qr=c(u().mark((function e(n){var r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,s=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Mr({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof s){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[lt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=P,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Ln,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=en,exports.CONTRACT_VERSION=N,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=k,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Nr,exports.Swap=Pr,exports.SwapQuoteError=D,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Fn,exports.V2AddLiquidityTxnIndices=cn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=tt,exports.calculateAccountMinimumRequiredBalance=se,exports.calculatePriceImpact=Wt,exports.calculateSwapRate=Gt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=rr,exports.poolUtils=Ne,exports.prepareCommitTransactions=function(t){return Qr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Fr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return kr.apply(this,arguments)},exports.sendAndWaitRawTransaction=st,exports.sumUpTxnFees=it,exports.tinymanContract_v1_1=$t,exports.tinymanContract_v2=ne,exports.tinymanJSSDKConfig=mt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",o=r.asyncIterator||"@@asyncIterator",s=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,o=Object.create(a.prototype),s=new _(r||[]);return o._invoke=function(t,e,n){var r="suspendedStart";return function(a,o){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw o;return E()}for(n.method=a,n.arg=o;;){var s=n.delegate;if(s){var u=y(s,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,s),o}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var x=Object.getPrototypeOf,I=x&&x(x(S([])));I&&I!==e&&n.call(I,a)&&(g=I);var A=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,o,s,u){var i=p(t[a],t,o);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,s,u)}),(function(t){r("throw",t,s,u)})):e.resolve(l).then((function(t){c.value=t,s(c)}),(function(t){return r("throw",t,s,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function o(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(o,o):o()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function w(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function v(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function _(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(w,this),this.reset(!0)}function S(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,o=function e(){for(;++r=0;--a){var o=this.tryEntries[a],s=o.completion;if("root"===o.tryLoc)return r("end");if(o.tryLoc<=this.prev){var u=n.call(o,"catchLoc"),i=n.call(o,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),v(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;v(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:S(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,o,s){try{var u=t[o](s),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function s(t){i(o,r,a,s,u,"next",t)}function u(t){i(o,r,a,s,u,"throw",t)}s(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,s=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){u=!0,o=t},f:function(){try{s||null==n.return||n.return()}finally{if(u)throw o}}}}exports.SwapQuoteErrorType=void 0,(r=exports.SwapQuoteErrorType||(exports.SwapQuoteErrorType={})).SwapRouterStaleDataError="SwapRouterStaleDataError",r.SwapRouterNoRouteError="SwapRouterNoRouteError",r.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",r.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",r.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",r.NoAvailablePoolError="NoAvailablePoolError",r.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",r.UnknownError="UnknownError",r.LowSwapAmountError="LowSwapAmountError",r.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",r.InvalidSwapTypeError="InvalidSwapTypeError";var D=function(t){m(n,h(Error));var e=w(n);function n(t,r){var a;return p(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(T(a),n),a}return d(n)}(),O=Uint8Array.from([1]),N={V1_1:"v1_1",V2:"v2"},P={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},k={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},L="- would result negative",R="logic eval error:",F="exceeds schema integer count",B=/transaction \w+:/,M=function(t){m(n,h(Error));var e=w(n);function n(t,r){var a;p(this,n);for(var o=arguments.length,s=new Array(o>2?o-2:0),u=2;ur?[s(s({},t),{},{id:n}),s(s({},e),{},{id:r})]:[s(s({},e),{},{id:r}),s(s({},t),{},{id:n})]}function C(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function j(t){return 0===Number(t)}function X(t){return Number(t.id)}function q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,o=void 0!==a&&a,s={},u=b(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}s[o?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return s}function U(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,o=b(t);try{for(o.s();!(e=o.n()).done;){var s=e.value;r.set(s,a),a+=s.length}}catch(t){o.e(t)}finally{o.f()}return r}var G=100000n,W=100000n,z=100000n,J=25000n+25000n,Y=25000n+3500n;function K(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],o=t["created-apps"]||[],s=t.assets||[];return G+W*BigInt(s.length)+z*BigInt(o.length+a.length)+Y*r+J*n}function H(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,H(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function tt(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function et(t,e){var n=Number(t);return rt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function nt(t,e){return rt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function rt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(ot(e),2),o=a[0],s=a[1],u=v(ot(Math.round(Number(at(o,s+r)))),2),i=u[0],c=u[1];return Number(at(i,c-r))}return Math.round(e)}function at(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ot(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function st(t,e){return ut.apply(this,arguments)}function ut(){return(ut=c(u().mark((function t(e,n){var r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=b(n),t.prev=3,a.s();case 5:if((o=a.n()).done){t.next=18;break}return s=o.value,t.next=9,e.sendRawTransaction(s).do();case 9:return i=t.sent,c=i.txId,t.next=13,Z(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new M(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function it(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ct(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function pt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function lt(t){return(new TextEncoder).encode(t)}function dt(t){return Boolean(t)&&void 0!==t.fallback_message}var ft,mt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return lt("tinyman/".concat(t===N.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),gt=(f(ft={},N.V1_1,{testnet:62368684,mainnet:552635992}),f(ft,N.V2,{testnet:148607e3,mainnet:1002541853}),ft);function xt(t,e){var n=gt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function It(){return(It=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationOptInTxnFromObject({from:s,appIndex:xt(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var At;function ht(){return(ht=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationClearStateTxnFromObject({from:s,appIndex:xt(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(At=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",At.FixedOutput="fixed-output";var Tt,yt,wt;!function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(wt||(wt={}));var vt,_t,St=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),Et=lt("swap"),bt=(f(yt={},exports.SwapType.FixedInput,lt("fixed-input")),f(yt,exports.SwapType.FixedOutput,lt("fixed-output")),yt),Dt={ASSET_OPT_IN:lt("asset_opt_in")},Ot={testnet:159521633,mainnet:0},Nt=(f(vt={},exports.SwapType.FixedInput,7),f(vt,exports.SwapType.FixedOutput,8),vt),Pt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function kt(t){var e=Bt(t),n=e.assetIn,r=e.assetOut;return et(r.asset.decimals,Number(r.amount))/et(n.asset.decimals,Number(n.amount))}function Lt(t){if(!Ot[t])throw new Error("Unknown network or network not supported: ".concat(t));return Ot[t]}function Rt(t){return t[t.length-1].quote.amount_out}function Ft(t){return t[0].quote.amount_in}function Bt(t){return{assetIn:Ft(t),assetOut:Rt(t)}}function Mt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function e(n){var r,o,s,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.routerAppID,s=n.assetIDs,i=n.accountAddress,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o,appArgs:[Dt.ASSET_OPT_IN],foreignAssets:s,suggestedParams:c}),l=s.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,v,_,S,E,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,o=n.client,s=n.network,i=n.swapType,c=n.route,e.next=3,o.getTransactionParams().do();case 3:return p=e.sent,l=[X(c[0].quote.amount_in.asset),X(c[0].quote.amount_out.asset),X(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Ft(c).amount),Number(Rt(c).amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=j(d),w=Lt(s),v=y?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(w),amount:x,suggestedParams:p}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(w),amount:x,assetIndex:d,suggestedParams:p}),(_=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:w,appArgs:[Et,bt[i],a.default.encodeUint64(I)],foreignApps:[xt(s,N.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Nt[i]+1),S=[v,_],e.next=15,jt({client:o,routerAppID:w,assetIDs:[d,f,m]});case 15:if(!((E=e.sent).length>0)){e.next=21;break}return e.next=19,Mt({client:o,accountAddress:r,assetIDs:E,routerAppID:w});case 19:b=e.sent,S.unshift(b);case 21:return D=a.default.assignGroupID(S),e.abrupt("return",D.map((function(t){return{txn:t,signers:[r]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(u().mark((function e(n){var r,a,o,s,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,o=n.assetIDs,s=t.getApplicationAddress(a),e.next=4,r.accountInformation(s).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=o.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qt(t){return Ut.apply(this,arguments)}function Ut(){return(Ut=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.swapType,s=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Pt[s].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(!dt(p)){t.next=13;break}throw new D(p.type,p.fallback_message);case 13:throw new D(exports.SwapQuoteErrorType.UnknownError,"Unknown error");case 14:return t.abrupt("return",p);case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapQuoteType=void 0,(_t=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",_t.Router="router";var Gt=4*t.ALGORAND_MIN_TX_FEE;function Wt(e){return(St[e]+2)*t.ALGORAND_MIN_TX_FEE}function zt(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:.003;return Math.ceil(1/t)}function Jt(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:.003;return tn&&(e=a,n=o)}return e}function re(t){return t instanceof D&&[exports.SwapQuoteErrorType.SwapRouterInsufficientReservesError,exports.SwapQuoteErrorType.SwapRouterLowSwapAmountError,exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,exports.SwapQuoteErrorType.LowSwapAmountError].includes(t.type)}var ae={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},oe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},se=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ue=new(function(n){m(a,se);var r=w(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=xt(n.network,N.V1_1),a=v(C(n.asset1ID,n.asset2ID),2),o=a[0],s=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:o,asset_id_2:s,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(s(s({},i),{},{minimum_required_balance:me(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),o(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function fe(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function me(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ge,xe,Ie=lt("e");function Ae(t){return he.apply(this,arguments)}function he(){return(he=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,v,_,S,E;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.pool,i=r.accountAddr,n.next=3,o.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=s.account.address(),g=b(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==s.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=q({stateArray:A}),T=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ie,a.default.encodeUint64(s.asset1ID)])),y=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ie,a.default.encodeUint64(s.asset2ID)])),w=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ie,a.default.encodeUint64(s.poolTokenID)])),v=h[T],_=h[y],S=h[w],"bigint"==typeof v&&(l=v),"bigint"==typeof _&&(d=_),"bigint"==typeof S&&(f=S);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((E={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Te(){return(Te=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.accountAddr,i=r.validatorAppID,n.next=3,o.accountInformation(s).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mve)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ve,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function be(){return be=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,de(n,r);case 6:return s=t.sent,i=fe(s,xt(a,N.V1_1)),c=null,i&&(l=s["created-assets"][0],p=l.index,c={asset1ID:i[ye[N.V1_1].asset1],asset2ID:i[ye[N.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),be.apply(this,arguments)}var De=Object.freeze({__proto__:null,getPoolInfo:_e,getPoolReserves:function(t,e){return Ee.apply(this,arguments)},getPoolAssets:function(t){return be.apply(this,arguments)}});function Oe(t){return Ne.apply(this,arguments)}function Ne(){return(Ne=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,o=e.asset2ID,s=le(N.V2),i=s.generateLogicSigAccountForPool(e),c=xt(r,N.V2),p=i.address(),l=C(a,o),t.next=8,de(n,p);case 8:return d=t.sent,f=fe(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:N.V2},f&&(m.asset1ProtocolFees=BigInt(f[ye.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ye.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ye.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ye.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ye.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ye.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ye.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ye.v2.totalFeeShare]),m.poolTokenID=Number(f[ye.v2.poolTokenID]),m.asset1ID=Number(f[ye.v2.asset1]),m.asset2ID=Number(f[ye.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pe(){return(Pe=c(u().mark((function t(e,n){var r,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,de(e,n.account.address());case 2:return r=t.sent,a=fe(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[ye.v2.asset1Reserves]),o.asset2=BigInt(a[ye.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[ye.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ke(){return(ke=c(u().mark((function t(e){var n,r,a,o,s,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,de(n,r);case 3:return o=t.sent,s=fe(o,xt(a,N.V2)),i=null,s&&(i={asset1ID:s[ye[N.V2].asset1],asset2ID:s[ye[N.V2].asset2],poolTokenID:s[ye[N.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Le=Object.freeze({__proto__:null,getPoolInfo:Oe,getPoolReserves:function(t,e){return Pe.apply(this,arguments)},getPoolAssets:function(t){return ke.apply(this,arguments)}});function Re(t){return Boolean(t&&!(t.asset1+t.asset2))}var Fe,Be,Me=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Re(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=et(t.asset1,e.asset1)/et(t.asset2,e.asset2)),r},isPoolEmpty:Re,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([_e(t),Oe(t)])}}),Qe=s((f(Fe={},N.V1_1,s(s({},De),Me)),f(Fe,N.V2,s(s({},Le),Me)),Fe),Me);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Be||(Be={}));var Ve=96e4,Ce=859e3;function je(){return(je=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,S,E,b,D,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.asset_1,s=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=V(o,s),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=j(x),h=xt(r,N.V1_1),T=ue.generateLogicSigAccountForPool({network:r,asset1ID:f,asset2ID:x}),y=T.address(),w=a.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:mt.getAppCallTxnNoteWithClientName(N.V1_1),appArgs:[lt("bootstrap"),a.default.encodeUint64(f),a.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),_=a.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:k.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),S=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Xe(A),suggestedParams:c}),(b=[])[Be.FUNDING_TXN]=E,b[Be.VALIDATOR_APP_CALL]=w,b[Be.POOL_TOKEN_CREATE]=_,b[Be.ASSET1_OPT_IN]=S,A||(b[Be.ASSET2_OPT_IN]=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),D=a.default.assignGroupID(b),O=[{txn:D[Be.FUNDING_TXN],signers:[i]},{txn:D[Be.VALIDATOR_APP_CALL],signers:[y]},{txn:D[Be.POOL_TOKEN_CREATE],signers:[y]},{txn:D[Be.ASSET1_OPT_IN],signers:[y]}],D[Be.ASSET2_OPT_IN]&&O.push({txn:D[Be.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(t){return t?Ve:Ce}function qe(){return(qe=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=C(s,i),f=v(d,2),m=f[0],g=f[1],x=ue.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Be.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,x),r=n.txID,o=n.blob;return I.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ue(t){return Ge.apply(this,arguments)}function Ge(){return(Ge=c(u().mark((function t(e){var n,r,a,o,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Z(n,a[Be.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(s=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(s));case 10:return t.abrupt("return",{poolTokenID:s});case 13:throw t.prev=13,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function We(){return(We=c(u().mark((function t(e){var n,r,a,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Ue({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",Qe.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ze,Je={generateTxns:function(t){return je.apply(this,arguments)},signTxns:function(t){return qe.apply(this,arguments)},execute:function(t){return We.apply(this,arguments)},getBootstrapFundingTxnAmount:Xe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(ze||(ze={}));var Ye=5,Ke=6;function He(t,e){return Ze.apply(this,arguments)}function Ze(){return(Ze=c(u().mark((function e(n,r){var a,o,s;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return s=e.t0,e.abrupt("return",s);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function $e(t,e){return tn.apply(this,arguments)}function tn(){return(tn=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,He(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(){return(en=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,S;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.network,s=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return p=e.sent,l=xt(o,N.V2),d=t.getApplicationAddress(l),f=V(s,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,Qe.v2.getPoolInfo({client:r,network:o,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(s.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=pe.generateLogicSigAccountForPool({network:o,asset1ID:g,asset2ID:x}),A=I.address(),h=j(x),(T=a.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[lt("bootstrap")],note:mt.getAppCallTxnNoteWithClientName(N.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=rn(h),y=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:nn(h),suggestedParams:p}),(w=[])[ze.FUNDING_TXN]=y,w[ze.VALIDATOR_APP_CALL]=T,_=a.default.assignGroupID(w),(S=[])[ze.FUNDING_TXN]={txn:_[ze.FUNDING_TXN],signers:[c]},S[ze.VALIDATOR_APP_CALL]={txn:_[ze.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",S);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function nn(t){return function(t,e){var n=le(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(N.V2,t)+rn(t)+1e5}function rn(e){return((e?Ye:Ke)+1)*t.ALGORAND_MIN_TX_FEE}function an(){return(an=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=C(s,i),f=v(d,2),m=f[0],g=f[1],x=pe.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===ze.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,x),r=n.txID,o=n.blob;return I.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function on(){return(on=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,He(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ye.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Qe.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 21:throw t.prev=21,t.t3=t.catch(1),new M(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var sn,un={generateTxns:function(t){return en.apply(this,arguments)},signTxns:function(t){return an.apply(this,arguments)},execute:function(t){return on.apply(this,arguments)},getBootstrapFundingTxnAmount:nn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+nn(e)}};var cn,pn=(f(sn={},N.V1_1,Je),f(sn,N.V2,un),f(sn,"generateTxns",(function(t){return t.contractVersion===N.V1_1?Je.generateTxns(t):un.generateTxns(t)})),f(sn,"signTxns",(function(t){return t.contractVersion===N.V1_1?Je.signTxns(t):un.signTxns(t)})),f(sn,"execute",(function(t){return t.contractVersion===N.V1_1?Je.execute(t):un.execute(t)})),f(sn,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===N.V1_1?Je.getBootstrapFundingTxnAmount(n):un.getBootstrapFundingTxnAmount(n)})),sn);exports.V1_1AddLiquidityTxnIndices=void 0,(cn=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[cn.FEE_TXN=0]="FEE_TXN",cn[cn.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",cn[cn.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",cn[cn.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",cn[cn.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var ln,dn,fn,mn,gn=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(mn=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",mn.FLEXIBLE="flexible",mn.INITIAL="initial";var xn,In=(f(ln={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(ln,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(ln,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),ln),An=(f(dn={},exports.V2AddLiquidityType.INITIAL,1),f(dn,exports.V2AddLiquidityType.SINGLE,2),f(dn,exports.V2AddLiquidityType.FLEXIBLE,2),dn),hn=(f(fn={},exports.V2AddLiquidityType.INITIAL,3),f(fn,exports.V2AddLiquidityType.FLEXIBLE,3),f(fn,exports.V2AddLiquidityType.SINGLE,2),fn);function Tn(t){var e,n,r,a=t.reserves,o=t.totalFeeShare,s=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(s.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(s.amount)-m,I=BigInt(u.amount)-g;if(x>I){var A=x;r=yn(A,o),e={id:s.id,amount:A+r,decimals:s.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=yn(h,o),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:s.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:s.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Ht({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function yn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function wn(e){return(An[e]+1)*t.ALGORAND_MIN_TX_FEE}var vn=lt("add_liquidity"),_n=(f(xn={},N.V1_1,[lt("mint")]),f(xn,N.V2,{INITIAL_LIQUIDITY:[lt("add_initial_liquidity")],SINGLE_ASSET_MODE:[vn,lt("single")],FLEXIBLE_MODE:[vn,lt("flexible")]}),xn);function Sn(){return(Sn=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.poolAddress,s=e.asset1In,i=e.asset2In,c=e.poolTokenOut,p=e.slippage,l=e.initiatorAddr,d=tt("negative",p,c.amount),t.next=4,n.getTransactionParams().do();case 4:return f=t.sent,m=a.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:xt(r,N.V1_1),appArgs:_n.v1_1,accounts:[l],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==i.id?[s.id,c.id]:[s.id,i.id,c.id],suggestedParams:f}),g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:s.id,amount:s.amount,suggestedParams:f}),x=0===i.id?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:i.amount,suggestedParams:f}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),I=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:O,suggestedParams:f}),h=a.default.assignGroupID([A,m,g,x,I]),t.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,s=n.account,t.next=4,o([r]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=r.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:a.default.signLogicSigTransactionObject(t.txn,s).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function bn(){return(bn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.signedTxns,s=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Ae({client:n,pool:r,accountAddr:s});case 5:return c=t.sent,t.next=8,st(n,[o]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=it(a),x=ct(a),t.next=17,Ae({client:n,pool:r,accountAddr:s});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new M(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Dn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var o=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(o<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(o,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:o-BigInt(1e3),share:1}}var s=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=su)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Jt(Number(e)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");return{assetInID:n,assetInAmount:i,assetOutID:r,assetOutAmount:l,swapFee:Number(d),rate:et(a.assetOut,Number(l))/et(a.assetIn,Number(i)),priceImpact:f}}function $n(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,o=t.pool;if(!o||!Qe.isPoolReady(o))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");var s,u,i=BigInt(e),c=o.totalFeeShare;if(r===o.asset1ID)s=o.asset2Reserves,u=o.asset1Reserves;else{if(r!==o.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r,") doesn't belong to the pool ").concat(o.account.address(),"."));s=o.asset1Reserves,u=o.asset2Reserves}var p=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,o=t.decimals,s=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:s,totalFeeShare:a}),i=s+u,c=Ht({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:o.assetIn},assetOut:{amount:r,decimals:o.assetOut}});return{swapInputAmount:i,totalFeeAmount:u,priceImpact:c}}({inputSupply:s,outputSupply:u,swapOutputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapInputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Jt(Number(l)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");return{type:exports.SwapQuoteType.Direct,data:{pool:o,quote:{assetInID:n,assetInAmount:l,assetOutID:r,assetOutAmount:i,swapFee:Number(d),rate:et(a.assetOut,Number(i))/et(a.assetIn,Number(l)),priceImpact:f}}}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[],p?l.push(new Promise((function(t,e){try{var s=Zn({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p});t({type:exports.SwapQuoteType.Direct,data:{pool:p,quote:s}})}catch(t){e(t)}}))):l.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools."))),s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedInput,network:i});case 2:if(!((e=t.sent).route.length<2)){t.next=5;break}throw new D(exports.SwapQuoteErrorType.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 5:return t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 6:case"end":return t.stop()}}),t)})))()),t.next=6,Hn(l);case 6:return d=t.sent,t.abrupt("return",ne(d));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(t){return rr.apply(this,arguments)}function rr(){return(rr=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[new Promise((function(t,e){try{t($n({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p}))}catch(t){e(t)}}))],s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedOutput,network:i});case 2:return e=t.sent,t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 4:case"end":return t.stop()}}),t)})))()),t.next=5,Hn(l);case 5:return d=t.sent,t.abrupt("return",ne(d));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,o=t.decimals,s=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-s});return{swapOutputAmount:u,totalFeeAmount:s,priceImpact:Ht({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:o.assetIn},assetOut:{amount:u,decimals:o.assetOut}})}}!function(t){t[t.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(Xn||(Xn={}));var or={getQuote:function(t){return Kn.apply(this,arguments)},getFixedInputSwapQuote:tr,getFixedInputDirectSwapQuote:Zn,getFixedOutputDirectSwapQuote:$n,getFixedOutputSwapQuote:nr,generateTxns:function(t){return Jn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Yn.apply(this,arguments)},calculateFixedInputSwap:ar};function sr(t,e){var n,r,a=BigInt(t),o=e.issuedLiquidity;return o>a+BigInt(1e3)?(n=a*e.asset1/o,r=a*e.asset2/o):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function ur(){return(ur=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.account.address(),m=o.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:s,suggestedParams:d}),(x=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Un,a.default.encodeUint64(tt("negative",l,c)),a.default.encodeUint64(tt("negative",l,p))],accounts:[f],foreignAssets:[o.asset1ID,o.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[Xn.ASSET_TRANSFER_TXN]=g,I[Xn.APP_CALL_TXN]=x,A=a.default.assignGroupID(I),e.abrupt("return",[{txn:A[Xn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[Xn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ir(){return(ir=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,v;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.asset1ID,m=o.asset2ID,g=o.account.address(),x=o.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=tt("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=a.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Un,a.default.encodeUint64(I),a.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(w=[])[Xn.ASSET_TRANSFER_TXN]=T,w[Xn.APP_CALL_TXN]=y,v=a.default.assignGroupID(w),e.abrupt("return",[{txn:v[Xn.ASSET_TRANSFER_TXN],signers:[s]},{txn:v[Xn.APP_CALL_TXN],signers:[s]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function cr(){return(cr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,o=e.signedTxns,t.next=3,st(r,[o]);case 3:return s=t.sent,i=v(s,1),c=i[0].txnID,t.next=8,$e(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pr,lr,dr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=sr(a,n),s=o.asset1OutputAmount,u=o.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:s},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,o=t.assetOutID,s=t.decimals,u=BigInt(a),i=sr(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(o===n.asset1ID){var d=or.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:s}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(o!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=or.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:s}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return ur.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return ir.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return cr.apply(this,arguments)}},fr=(f(pr={},N.V1_1,{generateTxns:function(t){return Gn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,s=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:e.poolTokenID,poolTokenIn:a,asset1ID:e.asset1ID,asset1Out:o,asset2ID:e.asset2ID,asset2Out:s}},signTxns:function(t){return Wn.apply(this,arguments)},execute:function(t){return zn.apply(this,arguments)}}),f(pr,N.V2,dr),pr),mr=3n,gr=1000n;function xr(){return(xr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,t.next=3,o([r]);case 3:return s=t.sent,i=v(s,2),c=i[0],p=i[1],l=r.map((function(t,e){return e===lr.FEE_TXN_INDEX?c:e===lr.ASSET_IN_TXN_INDEX?p:a.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ir(){return(Ir=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,v;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.quote,o=e.swapType,s=e.slippage,i=e.initiatorAddr,c=r.pool,p=r.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new M({pool:c,quote:r},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return g=t.sent,x=[lt("swap"),o===exports.SwapType.FixedInput?lt("fi"):lt("fo")],I=a.default.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:x,accounts:[i],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==c.asset2ID?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),A=o===exports.SwapType.FixedOutput?tt("positive",s,p.assetInAmount):p.assetInAmount,h=0===l?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:f,amount:A,suggestedParams:g}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:l,amount:A,suggestedParams:g}),T=o===exports.SwapType.FixedInput?tt("negative",s,p.assetOutAmount):p.assetOutAmount,y=0===d?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:i,amount:T,suggestedParams:g}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:i,assetIndex:d,amount:T,suggestedParams:g}),w=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:f,amount:I.fee+y.fee,note:O,suggestedParams:g}),v=a.default.assignGroupID([w,I,h,y]),t.abrupt("return",[{txn:v[0],signers:[i]},{txn:v[1],signers:[f]},{txn:v[2],signers:[i]},{txn:v[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap on a non-existent pool");var o,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*mr/gr,l=i-u*i/(u+(c-p));if(l>i)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Kt(d),priceImpact:Ht(s({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function hr(t){return Tr.apply(this,arguments)}function Tr(){return(Tr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,Ae({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ae({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:s.id,assetOutAmount:BigInt(s.amount)+A,excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap on a non-existent pool");var o,s,u,i=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,s=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,s=n.asset1,u=n.asset2}if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=s*u/(u-i)-s,p=c*gr/(gr-mr),l=p-c,d=et(a.assetOut,Number(i))/et(a.assetIn,Number(p)),f=et(a.assetOut,Number(u))/et(a.assetIn,Number(s)),m=rt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:o,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function wr(t){return vr.apply(this,arguments)}function vr(){return(vr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,Ae({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ae({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:s.id,assetOutAmount:BigInt(s.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _r(){return(_r=c(u().mark((function t(e){var n,r,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,o=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new M({pool:r,swapType:a,txGroup:o},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:o[lr.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:o[lr.ASSET_IN_TXN_INDEX].txn.amount},l={id:o[lr.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:o[lr.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,hr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,wr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",s(s({},d),{},{groupID:ct(o),fees:it(o)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new M(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(lr||(lr={}));var Sr,Er={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Ar({pool:e,reserves:n,assetIn:r,decimals:a}):yr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Ar,getFixedOutputSwapQuote:yr,generateTxns:function(t){return Ir.apply(this,arguments)},signTxns:function(t){return xr.apply(this,arguments)},execute:function(t){return _r.apply(this,arguments)},executeFixedOutputSwap:wr};function br(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=v(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(re(n)&&!re(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function Dr(){return(Dr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,a=e.assetIn,o=e.assetOut,s=e.isSwapRouterEnabled,i=e.pools,c=[],(p=i.find((function(t){return t.info.contractVersion===N.V1_1})))?(l={pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves},c.push(new Promise((function(t,e){try{t(Er.getFixedInputSwapQuote(l))}catch(t){e(t)}})))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap from non-existent pool"))),d=i.find((function(t){return t.info.contractVersion===N.V2})),c.push(or.getFixedInputSwapQuote({amount:r,assetInID:X(a),assetOutID:X(o),pool:null!==(n=null==d?void 0:d.info)&&void 0!==n?n:null,decimals:{assetIn:a.decimals,assetOut:o.decimals},isSwapRouterEnabled:s,network:e.network})),t.next=8,br(c);case 8:return f=t.sent,t.abrupt("return",ne(f));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,a=e.assetIn,o=e.assetOut,s=e.pools,i=e.isSwapRouterEnabled,c=[],(p=s.find((function(t){return t.info.contractVersion===N.V1_1})))?(l={pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves},c.push(new Promise((function(t,e){try{t(Er.getFixedOutputSwapQuote(l))}catch(t){e(t)}})))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap from non-existent pool"))),d=s.find((function(t){return t.info.contractVersion===N.V2})),c.push(or.getFixedOutputSwapQuote({amount:r,assetInID:X(a),assetOutID:X(o),pool:null!==(n=null==d?void 0:d.info)&&void 0!==n?n:null,decimals:{assetIn:a.decimals,assetOut:o.decimals},isSwapRouterEnabled:i,network:e.network})),t.next=8,br(c);case 8:return f=t.sent,t.abrupt("return",ne(f));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Nr=(f(Sr={},N.V1_1,Er),f(Sr,N.V2,or),f(Sr,"getQuote",(function(t){var e=t.type,n=t.pools;if(!t.isSwapRouterEnabled&&n.every((function(t){return Re(t.reserves)})))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");if(e===exports.SwapType.FixedInput)return function(t){return Dr.apply(this,arguments)}(t);if(e===exports.SwapType.FixedOutput)return function(t){return Or.apply(this,arguments)}(t);throw new D(exports.SwapQuoteErrorType.InvalidSwapTypeError,"Invalid swap type")})),f(Sr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&te(t.quote)===N.V1_1?Er.generateTxns(s(s({},t),{},{quote:t.quote.data})):or.generateTxns(t)})),f(Sr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&te(t.quote)===N.V1_1){var e=t.quote.data.pool;return Er.signTxns(s({pool:e},t))}return or.signTxns(t)})),f(Sr,"execute",(function(t){return t.contractVersion===N.V1_1?Er.execute(t):or.execute(t)})),Sr);function Pr(){return(Pr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,kr({txGroup:a,pool:r,initiatorSigner:o});case 4:return s=t.sent,t.next=7,st(n,[s]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:it(a),confirmedRound:d,txnID:l,groupID:ct(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function kr(t){return Lr.apply(this,arguments)}function Lr(){return(Lr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.pool,o=e.initiatorSigner,t.next=3,o([n]);case 3:return s=t.sent,i=v(s,1),c=i[0],p=r.account.lsig,l=n.map((function(t,e){return 0===e?c:a.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rr(){return Rr=c(u().mark((function t(e){var n,r,o,s,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.data,o=e.initiatorSigner,t.prev=1,s=r.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:it(e),groupID:ct(e),lsig:n.account.lsig}})),t.next=5,o(s.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(s.map((function(t,e){return new Promise(function(){var r=c(u().mark((function r(o,s){var c,p,l,d,f,m;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,c=t.txns.map((function(n,r){return 0===r?i[e]:a.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),r.next=4,st(n,[c]);case 4:p=r.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),r.next=15;break;case 12:r.prev=12,r.t0=r.catch(0),s(r.t0);case 15:case"end":return r.stop()}}),r,null,[[0,12]])})));return function(t,e){return r.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Rr.apply(this,arguments)}function Fr(){return(Fr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,o=e.assetID,s=e.assetOut,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=r.account.address(),l=a.default.makeApplicationNoOpTxnFromObject({from:p,appIndex:r.validatorAppID,appArgs:[lt("redeem")],note:mt.getAppCallTxnNoteWithClientName(r.contractVersion),accounts:[i],foreignAssets:0==r.asset2ID?[r.asset1ID,r.poolTokenID]:[r.asset1ID,r.asset2ID,r.poolTokenID],suggestedParams:c}),d=0===o?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:i,amount:BigInt(s),suggestedParams:c}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:i,assetIndex:o,amount:BigInt(s),suggestedParams:c}),f=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:p,amount:l.fee+d.fee,note:O,suggestedParams:c}),m=a.default.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[i]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,o=e.liquidityAssetID,s=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(s.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[lt("commit"),i],note:U([lt("tinymanStaking/v1:b"),c,t.encodeUint64(o),i])})}function Mr(){return(Mr=c(u().mark((function e(n){var r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,s=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Br({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof s){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[lt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=P,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=jn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=pn,exports.CONTRACT_VERSION=N,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=k,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=fr,exports.Swap=Nr,exports.SwapQuoteError=D,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=qn,exports.V2AddLiquidityTxnIndices=In,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=tt,exports.calculateAccountMinimumRequiredBalance=me,exports.calculatePriceImpact=Ht,exports.calculateSwapRate=Kt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=Jt,exports.isSwapQuoteErrorCausedByAmount=re,exports.poolUtils=Qe,exports.prepareCommitTransactions=function(t){return Mr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Rr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Pr.apply(this,arguments)},exports.sendAndWaitRawTransaction=st,exports.sumUpTxnFees=it,exports.tinymanContract_v1_1=ue,exports.tinymanContract_v2=pe,exports.tinymanJSSDKConfig=mt; diff --git a/dist/swap/common/utils.d.ts b/dist/swap/common/utils.d.ts index 726b7b3a..6574cb21 100644 --- a/dist/swap/common/utils.d.ts +++ b/dist/swap/common/utils.d.ts @@ -1,5 +1,7 @@ +import { CONTRACT_VERSION } from "../../contract/constants"; import { ContractVersionValue } from "../../contract/types"; import { AssetWithAmountAndDecimals, AssetWithIdAndAmount } from "../../util/asset/assetModels"; +import { SwapType } from "../constants"; import { SwapQuote } from "../types"; declare function calculateSwapRate({ assetIn, assetOut }: { assetIn: AssetWithAmountAndDecimals; @@ -19,4 +21,23 @@ declare function getAssetInAndAssetOutFromSwapQuote(quote: SwapQuote): { assetOut: AssetWithIdAndAmount; }; declare function getSwapQuoteContractVersion(quote: SwapQuote): ContractVersionValue; -export { calculateSwapRate, calculatePriceImpact, getSwapQuotePriceImpact, getAssetInFromSwapQuote, getAssetOutFromSwapQuote, getAssetInAndAssetOutFromSwapQuote, getSwapQuoteContractVersion }; +/** + * @returns the total fee that will be paid by the user + * for the swap transaction with given parameters + */ +declare function getSwapTotalFee(params: { + version: typeof CONTRACT_VERSION.V1_1; +} | { + version: typeof CONTRACT_VERSION.V2; + type: SwapType; +}): number; +/** + * @returns The asset amount ratio for the given quote + */ +declare function getSwapQuoteRate(quote: SwapQuote): number; +/** + * Compares the given quotes and returns the best one (with the highest rate). + */ +declare function getBestQuote(quotes: SwapQuote[]): SwapQuote; +declare function isSwapQuoteErrorCausedByAmount(error: Error): boolean; +export { calculateSwapRate, calculatePriceImpact, getSwapQuotePriceImpact, getAssetInFromSwapQuote, getAssetOutFromSwapQuote, getAssetInAndAssetOutFromSwapQuote, getSwapQuoteContractVersion, getSwapTotalFee, getSwapQuoteRate, getBestQuote, isSwapQuoteErrorCausedByAmount }; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index e3621e94..8be133fb 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -1,6 +1,6 @@ import { Algodv2 } from "algosdk"; import { AssetWithIdAndAmount, TinymanAnalyticsApiAsset } from "../util/asset/assetModels"; -import { SupportedNetwork } from "../util/commonTypes"; +import { SignerTransaction, SupportedNetwork } from "../util/commonTypes"; import { PoolReserves, V1PoolInfo, V2PoolInfo } from "../util/pool/poolTypes"; import { SwapType } from "./constants"; export declare enum SwapQuoteType { @@ -156,3 +156,11 @@ export interface V2SwapExecution { txnID: string; round: number; } +export interface ExecuteSwapCommonParams { + swapType: SwapType; + client: Algodv2; + network: SupportedNetwork; + txGroup: SignerTransaction[]; + signedTxns: Uint8Array[]; + assetIn: AssetWithIdAndAmount; +} diff --git a/dist/swap/utils.d.ts b/dist/swap/utils.d.ts index 1bcaea29..f5b55d60 100644 --- a/dist/swap/utils.d.ts +++ b/dist/swap/utils.d.ts @@ -1,15 +1,11 @@ -import { Algodv2 } from "algosdk"; import { CONTRACT_VERSION } from "../contract/constants"; -import { AssetWithIdAndAmount } from "../util/asset/assetModels"; -import { InitiatorSigner, SignerTransaction, SupportedNetwork } from "../util/commonTypes"; +import { InitiatorSigner, SignerTransaction } from "../util/commonTypes"; import { V1PoolInfo } from "../util/pool/poolTypes"; -import { GetSwapQuoteBySwapTypeParams, GenerateSwapTxnsParams, GetSwapQuoteParams, SwapQuote } from "./types"; -import { SwapType } from "./constants"; +import { GetSwapQuoteBySwapTypeParams, GenerateSwapTxnsParams, GetSwapQuoteParams, SwapQuote, ExecuteSwapCommonParams } from "./types"; /** * Gets the best quote for swap from the pools and swap router and returns the best option. */ export declare function getQuote(params: GetSwapQuoteParams): Promise; -export declare function isSwapQuoteErrorCausedByAmount(error: Error): boolean; /** * Gets quotes for fixed input swap the pools and swap router, * and returns the best quote (with the highest rate). @@ -20,24 +16,12 @@ export declare function getFixedInputSwapQuote(params: GetSwapQuoteBySwapTypePar * and returns the best quote (with the highest rate). */ export declare function getFixedOutputSwapQuote(params: GetSwapQuoteBySwapTypeParams): Promise; -/** - * Compares the given quotes and returns the best one (with the highest rate). - */ -export declare function getBestQuote(quotes: SwapQuote[]): SwapQuote; export declare function generateTxns(params: GenerateSwapTxnsParams): Promise; export declare function signTxns(params: { quote: SwapQuote; txGroup: SignerTransaction[]; initiatorSigner: InitiatorSigner; }): Promise; -interface ExecuteCommonParams { - swapType: SwapType; - client: Algodv2; - network: SupportedNetwork; - txGroup: SignerTransaction[]; - signedTxns: Uint8Array[]; - assetIn: AssetWithIdAndAmount; -} export declare function execute(params: ({ contractVersion: typeof CONTRACT_VERSION.V1_1; initiatorAddr: string; @@ -45,15 +29,4 @@ export declare function execute(params: ({ } | { contractVersion: typeof CONTRACT_VERSION.V2; quote: SwapQuote; -}) & ExecuteCommonParams): Promise | Promise; -/** - * @returns the total fee that will be paid by the user - * for the swap transaction with given parameters - */ -export declare function getSwapTotalFee(params: { - version: typeof CONTRACT_VERSION.V1_1; -} | { - version: typeof CONTRACT_VERSION.V2; - type: SwapType; -}): number; -export {}; +}) & ExecuteSwapCommonParams): Promise | Promise; diff --git a/dist/swap/v2/util.d.ts b/dist/swap/v2/util.d.ts index 3e4582f0..b99ee743 100644 --- a/dist/swap/v2/util.d.ts +++ b/dist/swap/v2/util.d.ts @@ -11,3 +11,4 @@ export declare function getV2MinSwapAssetInAmount(feeRate?: number): number; * @returns true if the amount of assetIn is less than the minimum possible amount of assetIn that can be used for swap in V2 pools */ export declare function isSwapAssetInAmountLow(amount: number, feeRate?: number): boolean; +export declare function getSwapAppCallFeeAmount(swapType: SwapType): number; diff --git a/src/index.ts b/src/index.ts index 862e6ec8..1a98b2b0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -117,7 +117,6 @@ export type { SwapRoute, GenerateSwapTxnsParams } from "./swap/types"; -export {getSwapTotalFee} from "./swap/utils"; export * from "./swap/v2/util"; export {SwapType} from "./swap/constants"; export {Swap} from "./swap"; diff --git a/src/swap/common/utils.ts b/src/swap/common/utils.ts index b6ac14fb..12ae0211 100644 --- a/src/swap/common/utils.ts +++ b/src/swap/common/utils.ts @@ -5,9 +5,17 @@ import { AssetWithIdAndAmount } from "../../util/asset/assetModels"; import {getAssetId} from "../../util/asset/assetUtils"; +import SwapQuoteError, {SwapQuoteErrorType} from "../../util/error/SwapQuoteError"; import {convertFromBaseUnits, roundNumber} from "../../util/util"; +import {SwapType} from "../constants"; import {SwapQuote, SwapQuoteType} from "../types"; -import {getAssetInFromSwapRoute, getAssetOutFromSwapRoute} from "../v2/router/util"; +import {V1_1_SWAP_TOTAL_FEE} from "../v1_1/constants"; +import { + getAssetInFromSwapRoute, + getAssetOutFromSwapRoute, + getSwapRouteRate +} from "../v2/router/util"; +import {getV2SwapTotalFee} from "../v2/util"; function calculateSwapRate({ assetIn, @@ -96,6 +104,75 @@ function getSwapQuoteContractVersion(quote: SwapQuote): ContractVersionValue { return CONTRACT_VERSION.V2; } +/** + * @returns the total fee that will be paid by the user + * for the swap transaction with given parameters + */ +function getSwapTotalFee( + params: + | { + version: typeof CONTRACT_VERSION.V1_1; + } + | { + version: typeof CONTRACT_VERSION.V2; + type: SwapType; + } +) { + switch (params.version) { + case CONTRACT_VERSION.V1_1: + return V1_1_SWAP_TOTAL_FEE; + + case CONTRACT_VERSION.V2: + return getV2SwapTotalFee(params.type); + + default: + throw new Error("Provided contract version was not valid."); + } +} + +/** + * @returns The asset amount ratio for the given quote + */ +function getSwapQuoteRate(quote: SwapQuote): number { + if (quote.type === SwapQuoteType.Direct) { + return quote.data.quote.rate; + } + + return getSwapRouteRate(quote.data.route); +} + +/** + * Compares the given quotes and returns the best one (with the highest rate). + */ +function getBestQuote(quotes: SwapQuote[]): SwapQuote { + let bestQuote: SwapQuote = quotes[0]; + let bestQuoteRate = getSwapQuoteRate(bestQuote); + + for (let index = 1; index < quotes.length; index++) { + const quote = quotes[index]; + const currentRate = getSwapQuoteRate(quote); + + if (currentRate > bestQuoteRate) { + bestQuote = quote; + bestQuoteRate = currentRate; + } + } + + return bestQuote; +} + +function isSwapQuoteErrorCausedByAmount(error: Error): boolean { + return ( + error instanceof SwapQuoteError && + [ + SwapQuoteErrorType.SwapRouterInsufficientReservesError, + SwapQuoteErrorType.SwapRouterLowSwapAmountError, + SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError, + SwapQuoteErrorType.LowSwapAmountError + ].includes(error.type) + ); +} + export { calculateSwapRate, calculatePriceImpact, @@ -103,5 +180,9 @@ export { getAssetInFromSwapQuote, getAssetOutFromSwapQuote, getAssetInAndAssetOutFromSwapQuote, - getSwapQuoteContractVersion + getSwapQuoteContractVersion, + getSwapTotalFee, + getSwapQuoteRate, + getBestQuote, + isSwapQuoteErrorCausedByAmount }; diff --git a/src/swap/types.ts b/src/swap/types.ts index 373c2927..8c6ebfb9 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -1,7 +1,7 @@ import {Algodv2} from "algosdk"; import {AssetWithIdAndAmount, TinymanAnalyticsApiAsset} from "../util/asset/assetModels"; -import {SupportedNetwork} from "../util/commonTypes"; +import {SignerTransaction, SupportedNetwork} from "../util/commonTypes"; import {PoolReserves, V1PoolInfo, V2PoolInfo} from "../util/pool/poolTypes"; import {SwapType} from "./constants"; @@ -176,3 +176,12 @@ export interface V2SwapExecution { txnID: string; round: number; } + +export interface ExecuteSwapCommonParams { + swapType: SwapType; + client: Algodv2; + network: SupportedNetwork; + txGroup: SignerTransaction[]; + signedTxns: Uint8Array[]; + assetIn: AssetWithIdAndAmount; +} diff --git a/src/swap/utils.ts b/src/swap/utils.ts index 0fc4ba02..7eac35ad 100644 --- a/src/swap/utils.ts +++ b/src/swap/utils.ts @@ -1,23 +1,22 @@ -import {Algodv2} from "algosdk"; - import {CONTRACT_VERSION} from "../contract/constants"; -import {AssetWithIdAndAmount} from "../util/asset/assetModels"; -import {InitiatorSigner, SignerTransaction, SupportedNetwork} from "../util/commonTypes"; +import {InitiatorSigner, SignerTransaction} from "../util/commonTypes"; import {V1PoolInfo} from "../util/pool/poolTypes"; import { GetSwapQuoteBySwapTypeParams, GenerateSwapTxnsParams, GetSwapQuoteParams, SwapQuote, - SwapQuoteType + SwapQuoteType, + ExecuteSwapCommonParams } from "./types"; import {SwapType} from "./constants"; import {SwapV1_1} from "./v1_1"; import {SwapV2} from "./v2"; -import {V1_1_SWAP_TOTAL_FEE} from "./v1_1/constants"; -import {getV2SwapTotalFee} from "./v2/util"; -import {getSwapRouteRate} from "./v2/router/util"; -import {getSwapQuoteContractVersion} from "./common/utils"; +import { + getBestQuote, + getSwapQuoteContractVersion, + isSwapQuoteErrorCausedByAmount +} from "./common/utils"; import {getAssetId} from "../util/asset/assetUtils"; import {isPoolEmpty} from "../util/pool/common"; import SwapQuoteError, {SwapQuoteErrorType} from "../util/error/SwapQuoteError"; @@ -44,18 +43,6 @@ export function getQuote(params: GetSwapQuoteParams): Promise { throw new SwapQuoteError(SwapQuoteErrorType.InvalidSwapTypeError, "Invalid swap type"); } -export function isSwapQuoteErrorCausedByAmount(error: Error): boolean { - return ( - error instanceof SwapQuoteError && - [ - SwapQuoteErrorType.SwapRouterInsufficientReservesError, - SwapQuoteErrorType.SwapRouterLowSwapAmountError, - SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError, - SwapQuoteErrorType.LowSwapAmountError - ].includes(error.type) - ); -} - /** * Given a list of swap quotes, it filters out the invalid quotes. * Validity of the quote is checked by if the promise was successfully resolved @@ -212,37 +199,6 @@ export async function getFixedOutputSwapQuote( return getBestQuote(validQuotes); } -/** - * @returns The asset amount ratio for the given quote - */ -function getSwapQuoteRate(quote: SwapQuote): number { - if (quote.type === SwapQuoteType.Direct) { - return quote.data.quote.rate; - } - - return getSwapRouteRate(quote.data.route); -} - -/** - * Compares the given quotes and returns the best one (with the highest rate). - */ -export function getBestQuote(quotes: SwapQuote[]): SwapQuote { - let bestQuote: SwapQuote = quotes[0]; - let bestQuoteRate = getSwapQuoteRate(bestQuote); - - for (let index = 1; index < quotes.length; index++) { - const quote = quotes[index]; - const currentRate = getSwapQuoteRate(quote); - - if (currentRate > bestQuoteRate) { - bestQuote = quote; - bestQuoteRate = currentRate; - } - } - - return bestQuote; -} - export function generateTxns( params: GenerateSwapTxnsParams ): Promise { @@ -275,15 +231,6 @@ export function signTxns(params: { return SwapV2.signTxns(params); } -interface ExecuteCommonParams { - swapType: SwapType; - client: Algodv2; - network: SupportedNetwork; - txGroup: SignerTransaction[]; - signedTxns: Uint8Array[]; - assetIn: AssetWithIdAndAmount; -} - export function execute( params: ( | { @@ -293,7 +240,7 @@ export function execute( } | {contractVersion: typeof CONTRACT_VERSION.V2; quote: SwapQuote} ) & - ExecuteCommonParams + ExecuteSwapCommonParams ) { if (params.contractVersion === CONTRACT_VERSION.V1_1) { return SwapV1_1.execute(params); @@ -301,29 +248,3 @@ export function execute( return SwapV2.execute(params); } - -/** - * @returns the total fee that will be paid by the user - * for the swap transaction with given parameters - */ -export function getSwapTotalFee( - params: - | { - version: typeof CONTRACT_VERSION.V1_1; - } - | { - version: typeof CONTRACT_VERSION.V2; - type: SwapType; - } -) { - switch (params.version) { - case CONTRACT_VERSION.V1_1: - return V1_1_SWAP_TOTAL_FEE; - - case CONTRACT_VERSION.V2: - return getV2SwapTotalFee(params.type); - - default: - throw new Error("Provided contract version was not valid."); - } -} diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index aa6a0944..e34e8659 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -1,4 +1,4 @@ -import algosdk, {Algodv2, ALGORAND_MIN_TX_FEE, Transaction} from "algosdk"; +import algosdk, {Algodv2, Transaction} from "algosdk"; import { applySlippageToAmount, @@ -23,23 +23,23 @@ import {SwapType} from "../constants"; import { V2_SWAP_APP_CALL_ARG_ENCODED, V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED, - V2SwapTxnGroupIndices, - V2_SWAP_APP_CALL_INNER_TXN_COUNT + V2SwapTxnGroupIndices } from "./constants"; import {isAlgo} from "../../util/asset/assetUtils"; import { calculatePriceImpact, getAssetInFromSwapQuote, - getAssetOutFromSwapQuote + getAssetOutFromSwapQuote, + getBestQuote, + isSwapQuoteErrorCausedByAmount } from "../common/utils"; import {getAppCallInnerTxns} from "../../util/transaction/transactionUtils"; import {tinymanJSSDKConfig} from "../../config"; import {CONTRACT_VERSION} from "../../contract/constants"; import {generateSwapRouterTxns, getSwapRoute} from "./router/swap-router"; import {poolUtils} from "../../util/pool"; -import {getBestQuote, isSwapQuoteErrorCausedByAmount} from "../utils"; import SwapQuoteError, {SwapQuoteErrorType} from "../../util/error/SwapQuoteError"; -import {isSwapAssetInAmountLow} from "./util"; +import {getSwapAppCallFeeAmount, isSwapAssetInAmountLow} from "./util"; async function generateTxns( params: GenerateSwapTxnsParams @@ -143,13 +143,6 @@ function signTxns({ return initiatorSigner([txGroup]); } -function getSwapAppCallFeeAmount(swapType: SwapType) { - // Add +1 to account for the outer txn fee - const totalTxnCount = V2_SWAP_APP_CALL_INNER_TXN_COUNT[swapType] + 1; - - return totalTxnCount * ALGORAND_MIN_TX_FEE; -} - /** * Executes a swap with the desired quantities. */ diff --git a/src/swap/v2/util.ts b/src/swap/v2/util.ts index 269e04de..1c462d22 100644 --- a/src/swap/v2/util.ts +++ b/src/swap/v2/util.ts @@ -28,3 +28,10 @@ export function isSwapAssetInAmountLow( ) { return amount < getV2MinSwapAssetInAmount(feeRate); } + +export function getSwapAppCallFeeAmount(swapType: SwapType) { + // Add +1 to account for the outer txn fee + const totalTxnCount = V2_SWAP_APP_CALL_INNER_TXN_COUNT[swapType] + 1; + + return totalTxnCount * ALGORAND_MIN_TX_FEE; +} From ab0c5a70e0f412f0b964c5ffdec87ada5c225864 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Wed, 22 Mar 2023 14:37:40 +0300 Subject: [PATCH 27/39] refactor: code improvements --- dist/index.js | 2 +- dist/swap/index.d.ts | 52 +++------ dist/swap/types.d.ts | 9 +- dist/swap/v1_1/index.d.ts | 2 +- dist/swap/v2/index.d.ts | 60 ++++------ dist/swap/v2/router/swap-router.d.ts | 23 ++-- dist/util/asset/assetModels.d.ts | 4 + src/swap/types.ts | 10 +- src/swap/utils.ts | 70 ++++++------ src/swap/v1_1/index.ts | 20 ++-- src/swap/v2/index.ts | 165 ++++++++++++--------------- src/swap/v2/router/swap-router.ts | 66 +++++++---- src/swap/v2/router/util.ts | 2 +- src/util/asset/assetModels.ts | 5 + 14 files changed, 230 insertions(+), 260 deletions(-) diff --git a/dist/index.js b/dist/index.js index 0bd4e307..de9f5f0a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,3 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r,a=n(t);function o(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function s(t){for(var e=1;e=0;--a){var o=this.tryEntries[a],s=o.completion;if("root"===o.tryLoc)return r("end");if(o.tryLoc<=this.prev){var u=n.call(o,"catchLoc"),i=n.call(o,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),v(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;v(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:S(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,o,s){try{var u=t[o](s),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function s(t){i(o,r,a,s,u,"next",t)}function u(t){i(o,r,a,s,u,"throw",t)}s(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,s=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){u=!0,o=t},f:function(){try{s||null==n.return||n.return()}finally{if(u)throw o}}}}exports.SwapQuoteErrorType=void 0,(r=exports.SwapQuoteErrorType||(exports.SwapQuoteErrorType={})).SwapRouterStaleDataError="SwapRouterStaleDataError",r.SwapRouterNoRouteError="SwapRouterNoRouteError",r.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",r.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",r.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",r.NoAvailablePoolError="NoAvailablePoolError",r.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",r.UnknownError="UnknownError",r.LowSwapAmountError="LowSwapAmountError",r.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",r.InvalidSwapTypeError="InvalidSwapTypeError";var D=function(t){m(n,h(Error));var e=w(n);function n(t,r){var a;return p(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(T(a),n),a}return d(n)}(),O=Uint8Array.from([1]),N={V1_1:"v1_1",V2:"v2"},P={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},k={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},L="- would result negative",R="logic eval error:",F="exceeds schema integer count",B=/transaction \w+:/,M=function(t){m(n,h(Error));var e=w(n);function n(t,r){var a;p(this,n);for(var o=arguments.length,s=new Array(o>2?o-2:0),u=2;ur?[s(s({},t),{},{id:n}),s(s({},e),{},{id:r})]:[s(s({},e),{},{id:r}),s(s({},t),{},{id:n})]}function C(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function j(t){return 0===Number(t)}function X(t){return Number(t.id)}function q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,o=void 0!==a&&a,s={},u=b(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}s[o?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return s}function U(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,o=b(t);try{for(o.s();!(e=o.n()).done;){var s=e.value;r.set(s,a),a+=s.length}}catch(t){o.e(t)}finally{o.f()}return r}var G=100000n,W=100000n,z=100000n,J=25000n+25000n,Y=25000n+3500n;function K(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],o=t["created-apps"]||[],s=t.assets||[];return G+W*BigInt(s.length)+z*BigInt(o.length+a.length)+Y*r+J*n}function H(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,H(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function tt(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function et(t,e){var n=Number(t);return rt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function nt(t,e){return rt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function rt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(ot(e),2),o=a[0],s=a[1],u=v(ot(Math.round(Number(at(o,s+r)))),2),i=u[0],c=u[1];return Number(at(i,c-r))}return Math.round(e)}function at(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ot(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function st(t,e){return ut.apply(this,arguments)}function ut(){return(ut=c(u().mark((function t(e,n){var r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=b(n),t.prev=3,a.s();case 5:if((o=a.n()).done){t.next=18;break}return s=o.value,t.next=9,e.sendRawTransaction(s).do();case 9:return i=t.sent,c=i.txId,t.next=13,Z(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new M(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function it(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ct(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function pt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function lt(t){return(new TextEncoder).encode(t)}function dt(t){return Boolean(t)&&void 0!==t.fallback_message}var ft,mt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return lt("tinyman/".concat(t===N.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),gt=(f(ft={},N.V1_1,{testnet:62368684,mainnet:552635992}),f(ft,N.V2,{testnet:148607e3,mainnet:1002541853}),ft);function xt(t,e){var n=gt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function It(){return(It=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationOptInTxnFromObject({from:s,appIndex:xt(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var At;function ht(){return(ht=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationClearStateTxnFromObject({from:s,appIndex:xt(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(At=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",At.FixedOutput="fixed-output";var Tt,yt,wt;!function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(wt||(wt={}));var vt,_t,St=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),Et=lt("swap"),bt=(f(yt={},exports.SwapType.FixedInput,lt("fixed-input")),f(yt,exports.SwapType.FixedOutput,lt("fixed-output")),yt),Dt={ASSET_OPT_IN:lt("asset_opt_in")},Ot={testnet:159521633,mainnet:0},Nt=(f(vt={},exports.SwapType.FixedInput,7),f(vt,exports.SwapType.FixedOutput,8),vt),Pt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function kt(t){var e=Bt(t),n=e.assetIn,r=e.assetOut;return et(r.asset.decimals,Number(r.amount))/et(n.asset.decimals,Number(n.amount))}function Lt(t){if(!Ot[t])throw new Error("Unknown network or network not supported: ".concat(t));return Ot[t]}function Rt(t){return t[t.length-1].quote.amount_out}function Ft(t){return t[0].quote.amount_in}function Bt(t){return{assetIn:Ft(t),assetOut:Rt(t)}}function Mt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function e(n){var r,o,s,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.routerAppID,s=n.assetIDs,i=n.accountAddress,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o,appArgs:[Dt.ASSET_OPT_IN],foreignAssets:s,suggestedParams:c}),l=s.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,v,_,S,E,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,o=n.client,s=n.network,i=n.swapType,c=n.route,e.next=3,o.getTransactionParams().do();case 3:return p=e.sent,l=[X(c[0].quote.amount_in.asset),X(c[0].quote.amount_out.asset),X(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Ft(c).amount),Number(Rt(c).amount)],x=g[0],I=g[1],A=[c[0].pool.address,c[1].pool.address],h=A[0],T=A[1],y=j(d),w=Lt(s),v=y?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(w),amount:x,suggestedParams:p}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(w),amount:x,assetIndex:d,suggestedParams:p}),(_=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:w,appArgs:[Et,bt[i],a.default.encodeUint64(I)],foreignApps:[xt(s,N.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Nt[i]+1),S=[v,_],e.next=15,jt({client:o,routerAppID:w,assetIDs:[d,f,m]});case 15:if(!((E=e.sent).length>0)){e.next=21;break}return e.next=19,Mt({client:o,accountAddress:r,assetIDs:E,routerAppID:w});case 19:b=e.sent,S.unshift(b);case 21:return D=a.default.assignGroupID(S),e.abrupt("return",D.map((function(t){return{txn:t,signers:[r]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(u().mark((function e(n){var r,a,o,s,i,c,p;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.routerAppID,o=n.assetIDs,s=t.getApplicationAddress(a),e.next=4,r.accountInformation(s).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),p=o.filter((function(t){return 0!==t&&!c.includes(t)})),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qt(t){return Ut.apply(this,arguments)}function Ut(){return(Ut=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.swapType,s=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Pt[s].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(!dt(p)){t.next=13;break}throw new D(p.type,p.fallback_message);case 13:throw new D(exports.SwapQuoteErrorType.UnknownError,"Unknown error");case 14:return t.abrupt("return",p);case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapQuoteType=void 0,(_t=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",_t.Router="router";var Gt=4*t.ALGORAND_MIN_TX_FEE;function Wt(e){return(St[e]+2)*t.ALGORAND_MIN_TX_FEE}function zt(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:.003;return Math.ceil(1/t)}function Jt(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:.003;return tn&&(e=a,n=o)}return e}function re(t){return t instanceof D&&[exports.SwapQuoteErrorType.SwapRouterInsufficientReservesError,exports.SwapQuoteErrorType.SwapRouterLowSwapAmountError,exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,exports.SwapQuoteErrorType.LowSwapAmountError].includes(t.type)}var ae={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},oe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},se=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ue=new(function(n){m(a,se);var r=w(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=xt(n.network,N.V1_1),a=v(C(n.asset1ID,n.asset2ID),2),o=a[0],s=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:o,asset_id_2:s,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(s(s({},i),{},{minimum_required_balance:me(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),o(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function fe(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function me(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ge,xe,Ie=lt("e");function Ae(t){return he.apply(this,arguments)}function he(){return(he=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,v,_,S,E;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.pool,i=r.accountAddr,n.next=3,o.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=s.account.address(),g=b(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((I=x.value).id==s.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(A=I["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=q({stateArray:A}),T=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ie,a.default.encodeUint64(s.asset1ID)])),y=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ie,a.default.encodeUint64(s.asset2ID)])),w=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ie,a.default.encodeUint64(s.poolTokenID)])),v=h[T],_=h[y],S=h[w],"bigint"==typeof v&&(l=v),"bigint"==typeof _&&(d=_),"bigint"==typeof S&&(f=S);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((E={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Te(){return(Te=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,x,I,A,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.accountAddr,i=r.validatorAppID,n.next=3,o.accountInformation(s).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mve)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ve,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function be(){return be=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,de(n,r);case 6:return s=t.sent,i=fe(s,xt(a,N.V1_1)),c=null,i&&(l=s["created-assets"][0],p=l.index,c={asset1ID:i[ye[N.V1_1].asset1],asset2ID:i[ye[N.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),be.apply(this,arguments)}var De=Object.freeze({__proto__:null,getPoolInfo:_e,getPoolReserves:function(t,e){return Ee.apply(this,arguments)},getPoolAssets:function(t){return be.apply(this,arguments)}});function Oe(t){return Ne.apply(this,arguments)}function Ne(){return(Ne=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,o=e.asset2ID,s=le(N.V2),i=s.generateLogicSigAccountForPool(e),c=xt(r,N.V2),p=i.address(),l=C(a,o),t.next=8,de(n,p);case 8:return d=t.sent,f=fe(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:N.V2},f&&(m.asset1ProtocolFees=BigInt(f[ye.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ye.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ye.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ye.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ye.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ye.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ye.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ye.v2.totalFeeShare]),m.poolTokenID=Number(f[ye.v2.poolTokenID]),m.asset1ID=Number(f[ye.v2.asset1]),m.asset2ID=Number(f[ye.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pe(){return(Pe=c(u().mark((function t(e,n){var r,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,de(e,n.account.address());case 2:return r=t.sent,a=fe(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[ye.v2.asset1Reserves]),o.asset2=BigInt(a[ye.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[ye.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ke(){return(ke=c(u().mark((function t(e){var n,r,a,o,s,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,de(n,r);case 3:return o=t.sent,s=fe(o,xt(a,N.V2)),i=null,s&&(i={asset1ID:s[ye[N.V2].asset1],asset2ID:s[ye[N.V2].asset2],poolTokenID:s[ye[N.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Le=Object.freeze({__proto__:null,getPoolInfo:Oe,getPoolReserves:function(t,e){return Pe.apply(this,arguments)},getPoolAssets:function(t){return ke.apply(this,arguments)}});function Re(t){return Boolean(t&&!(t.asset1+t.asset2))}var Fe,Be,Me=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Re(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=et(t.asset1,e.asset1)/et(t.asset2,e.asset2)),r},isPoolEmpty:Re,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([_e(t),Oe(t)])}}),Qe=s((f(Fe={},N.V1_1,s(s({},De),Me)),f(Fe,N.V2,s(s({},Le),Me)),Fe),Me);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Be||(Be={}));var Ve=96e4,Ce=859e3;function je(){return(je=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,S,E,b,D,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.asset_1,s=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=V(o,s),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,I=g.unit_name,A=j(x),h=xt(r,N.V1_1),T=ue.generateLogicSigAccountForPool({network:r,asset1ID:f,asset2ID:x}),y=T.address(),w=a.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:mt.getAppCallTxnNoteWithClientName(N.V1_1),appArgs:[lt("bootstrap"),a.default.encodeUint64(f),a.default.encodeUint64(x)],foreignAssets:A?[f]:[x],suggestedParams:c}),_=a.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:k.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),S=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Xe(A),suggestedParams:c}),(b=[])[Be.FUNDING_TXN]=E,b[Be.VALIDATOR_APP_CALL]=w,b[Be.POOL_TOKEN_CREATE]=_,b[Be.ASSET1_OPT_IN]=S,A||(b[Be.ASSET2_OPT_IN]=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),D=a.default.assignGroupID(b),O=[{txn:D[Be.FUNDING_TXN],signers:[i]},{txn:D[Be.VALIDATOR_APP_CALL],signers:[y]},{txn:D[Be.POOL_TOKEN_CREATE],signers:[y]},{txn:D[Be.ASSET1_OPT_IN],signers:[y]}],D[Be.ASSET2_OPT_IN]&&O.push({txn:D[Be.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(t){return t?Ve:Ce}function qe(){return(qe=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=C(s,i),f=v(d,2),m=f[0],g=f[1],x=ue.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===Be.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,x),r=n.txID,o=n.blob;return I.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ue(t){return Ge.apply(this,arguments)}function Ge(){return(Ge=c(u().mark((function t(e){var n,r,a,o,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Z(n,a[Be.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(s=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(s));case 10:return t.abrupt("return",{poolTokenID:s});case 13:throw t.prev=13,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function We(){return(We=c(u().mark((function t(e){var n,r,a,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Ue({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",Qe.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ze,Je={generateTxns:function(t){return je.apply(this,arguments)},signTxns:function(t){return qe.apply(this,arguments)},execute:function(t){return We.apply(this,arguments)},getBootstrapFundingTxnAmount:Xe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(ze||(ze={}));var Ye=5,Ke=6;function He(t,e){return Ze.apply(this,arguments)}function Ze(){return(Ze=c(u().mark((function e(n,r){var a,o,s;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return s=e.t0,e.abrupt("return",s);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function $e(t,e){return tn.apply(this,arguments)}function tn(){return(tn=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,He(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(){return(en=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,_,S;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.network,s=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return p=e.sent,l=xt(o,N.V2),d=t.getApplicationAddress(l),f=V(s,i),m=v(f,2),g=m[0].id,x=m[1].id,e.next=9,Qe.v2.getPoolInfo({client:r,network:o,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(s.unit_name,"-").concat(i.unit_name," already exists"));case 12:return I=pe.generateLogicSigAccountForPool({network:o,asset1ID:g,asset2ID:x}),A=I.address(),h=j(x),(T=a.default.makeApplicationOptInTxnFromObject({from:A,appIndex:l,appArgs:[lt("bootstrap")],note:mt.getAppCallTxnNoteWithClientName(N.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=rn(h),y=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:A,amount:nn(h),suggestedParams:p}),(w=[])[ze.FUNDING_TXN]=y,w[ze.VALIDATOR_APP_CALL]=T,_=a.default.assignGroupID(w),(S=[])[ze.FUNDING_TXN]={txn:_[ze.FUNDING_TXN],signers:[c]},S[ze.VALIDATOR_APP_CALL]={txn:_[ze.VALIDATOR_APP_CALL],signers:[A]},e.abrupt("return",S);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function nn(t){return function(t,e){var n=le(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(N.V2,t)+rn(t)+1e5}function rn(e){return((e?Ye:Ke)+1)*t.ALGORAND_MIN_TX_FEE}function an(){return(an=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=C(s,i),f=v(d,2),m=f[0],g=f[1],x=pe.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),I=[],A=n.map((function(t,e){if(e===ze.FUNDING_TXN)return I.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,x),r=n.txID,o=n.blob;return I.push(r),o})),t.abrupt("return",{signedTxns:A,txnIDs:I});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function on(){return(on=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,He(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ye.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Qe.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 21:throw t.prev=21,t.t3=t.catch(1),new M(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var sn,un={generateTxns:function(t){return en.apply(this,arguments)},signTxns:function(t){return an.apply(this,arguments)},execute:function(t){return on.apply(this,arguments)},getBootstrapFundingTxnAmount:nn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+nn(e)}};var cn,pn=(f(sn={},N.V1_1,Je),f(sn,N.V2,un),f(sn,"generateTxns",(function(t){return t.contractVersion===N.V1_1?Je.generateTxns(t):un.generateTxns(t)})),f(sn,"signTxns",(function(t){return t.contractVersion===N.V1_1?Je.signTxns(t):un.signTxns(t)})),f(sn,"execute",(function(t){return t.contractVersion===N.V1_1?Je.execute(t):un.execute(t)})),f(sn,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===N.V1_1?Je.getBootstrapFundingTxnAmount(n):un.getBootstrapFundingTxnAmount(n)})),sn);exports.V1_1AddLiquidityTxnIndices=void 0,(cn=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[cn.FEE_TXN=0]="FEE_TXN",cn[cn.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",cn[cn.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",cn[cn.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",cn[cn.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var ln,dn,fn,mn,gn=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(mn=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",mn.FLEXIBLE="flexible",mn.INITIAL="initial";var xn,In=(f(ln={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(ln,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(ln,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),ln),An=(f(dn={},exports.V2AddLiquidityType.INITIAL,1),f(dn,exports.V2AddLiquidityType.SINGLE,2),f(dn,exports.V2AddLiquidityType.FLEXIBLE,2),dn),hn=(f(fn={},exports.V2AddLiquidityType.INITIAL,3),f(fn,exports.V2AddLiquidityType.FLEXIBLE,3),f(fn,exports.V2AddLiquidityType.SINGLE,2),fn);function Tn(t){var e,n,r,a=t.reserves,o=t.totalFeeShare,s=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(s.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(s.amount)-m,I=BigInt(u.amount)-g;if(x>I){var A=x;r=yn(A,o),e={id:s.id,amount:A+r,decimals:s.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=yn(h,o),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:s.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:s.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Ht({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function yn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function wn(e){return(An[e]+1)*t.ALGORAND_MIN_TX_FEE}var vn=lt("add_liquidity"),_n=(f(xn={},N.V1_1,[lt("mint")]),f(xn,N.V2,{INITIAL_LIQUIDITY:[lt("add_initial_liquidity")],SINGLE_ASSET_MODE:[vn,lt("single")],FLEXIBLE_MODE:[vn,lt("flexible")]}),xn);function Sn(){return(Sn=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.poolAddress,s=e.asset1In,i=e.asset2In,c=e.poolTokenOut,p=e.slippage,l=e.initiatorAddr,d=tt("negative",p,c.amount),t.next=4,n.getTransactionParams().do();case 4:return f=t.sent,m=a.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:xt(r,N.V1_1),appArgs:_n.v1_1,accounts:[l],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==i.id?[s.id,c.id]:[s.id,i.id,c.id],suggestedParams:f}),g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:s.id,amount:s.amount,suggestedParams:f}),x=0===i.id?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:i.amount,suggestedParams:f}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),I=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:O,suggestedParams:f}),h=a.default.assignGroupID([A,m,g,x,I]),t.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,s=n.account,t.next=4,o([r]);case 4:return i=t.sent,c=v(i,3),p=c[0],l=c[1],d=c[2],f=r.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:a.default.signLogicSigTransactionObject(t.txn,s).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function bn(){return(bn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,I,A,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.signedTxns,s=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Ae({client:n,pool:r,accountAddr:s});case 5:return c=t.sent,t.next=8,st(n,[o]);case 8:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=it(a),x=ct(a),t.next=17,Ae({client:n,pool:r,accountAddr:s});case 17:return I=t.sent,(A=I.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new M(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Dn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var o=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(o<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(o,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:o-BigInt(1e3),share:1}}var s=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=su)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Jt(Number(e)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");return{assetInID:n,assetInAmount:i,assetOutID:r,assetOutAmount:l,swapFee:Number(d),rate:et(a.assetOut,Number(l))/et(a.assetIn,Number(i)),priceImpact:f}}function $n(t){var e=t.amount,n=t.assetInID,r=t.assetOutID,a=t.decimals,o=t.pool;if(!o||!Qe.isPoolReady(o))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");var s,u,i=BigInt(e),c=o.totalFeeShare;if(r===o.asset1ID)s=o.asset2Reserves,u=o.asset1Reserves;else{if(r!==o.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r,") doesn't belong to the pool ").concat(o.account.address(),"."));s=o.asset1Reserves,u=o.asset2Reserves}var p=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,o=t.decimals,s=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:s,totalFeeShare:a}),i=s+u,c=Ht({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:o.assetIn},assetOut:{amount:r,decimals:o.assetOut}});return{swapInputAmount:i,totalFeeAmount:u,priceImpact:c}}({inputSupply:s,outputSupply:u,swapOutputAmount:i,totalFeeShare:c,decimals:a}),l=p.swapInputAmount,d=p.totalFeeAmount,f=p.priceImpact;if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Jt(Number(l)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");return{type:exports.SwapQuoteType.Direct,data:{pool:o,quote:{assetInID:n,assetInAmount:l,assetOutID:r,assetOutAmount:i,swapFee:Number(d),rate:et(a.assetOut,Number(i))/et(a.assetIn,Number(l)),priceImpact:f}}}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[],p?l.push(new Promise((function(t,e){try{var s=Zn({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p});t({type:exports.SwapQuoteType.Direct,data:{pool:p,quote:s}})}catch(t){e(t)}}))):l.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools."))),s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedInput,network:i});case 2:if(!((e=t.sent).route.length<2)){t.next=5;break}throw new D(exports.SwapQuoteErrorType.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 5:return t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 6:case"end":return t.stop()}}),t)})))()),t.next=6,Hn(l);case 6:return d=t.sent,t.abrupt("return",ne(d));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(t){return rr.apply(this,arguments)}function rr(){return(rr=c(u().mark((function t(e){var n,r,a,o,s,i,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.decimals,s=e.isSwapRouterEnabled,i=e.network,p=e.pool,l=[new Promise((function(t,e){try{t($n({amount:n,assetInID:r,assetOutID:a,decimals:o,pool:p}))}catch(t){e(t)}}))],s&&l.push(c(u().mark((function t(){var e;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,qt({amount:n,assetInID:r,assetOutID:a,swapType:exports.SwapType.FixedOutput,network:i});case 2:return e=t.sent,t.abrupt("return",{type:exports.SwapQuoteType.Router,data:e});case 4:case"end":return t.stop()}}),t)})))()),t.next=5,Hn(l);case 5:return d=t.sent,t.abrupt("return",ne(d));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,o=t.decimals,s=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-s});return{swapOutputAmount:u,totalFeeAmount:s,priceImpact:Ht({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:o.assetIn},assetOut:{amount:u,decimals:o.assetOut}})}}!function(t){t[t.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(Xn||(Xn={}));var or={getQuote:function(t){return Kn.apply(this,arguments)},getFixedInputSwapQuote:tr,getFixedInputDirectSwapQuote:Zn,getFixedOutputDirectSwapQuote:$n,getFixedOutputSwapQuote:nr,generateTxns:function(t){return Jn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Yn.apply(this,arguments)},calculateFixedInputSwap:ar};function sr(t,e){var n,r,a=BigInt(t),o=e.issuedLiquidity;return o>a+BigInt(1e3)?(n=a*e.asset1/o,r=a*e.asset2/o):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function ur(){return(ur=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.account.address(),m=o.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:s,suggestedParams:d}),(x=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Un,a.default.encodeUint64(tt("negative",l,c)),a.default.encodeUint64(tt("negative",l,p))],accounts:[f],foreignAssets:[o.asset1ID,o.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(I=[])[Xn.ASSET_TRANSFER_TXN]=g,I[Xn.APP_CALL_TXN]=x,A=a.default.assignGroupID(I),e.abrupt("return",[{txn:A[Xn.ASSET_TRANSFER_TXN],signers:[i]},{txn:A[Xn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ir(){return(ir=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,v;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.asset1ID,m=o.asset2ID,g=o.account.address(),x=o.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(I=0,A=0,h=tt("negative",l,p),c!==f){e.next=17;break}I=h,A=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}I=0,A=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=a.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Un,a.default.encodeUint64(I),a.default.encodeUint64(A)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(w=[])[Xn.ASSET_TRANSFER_TXN]=T,w[Xn.APP_CALL_TXN]=y,v=a.default.assignGroupID(w),e.abrupt("return",[{txn:v[Xn.ASSET_TRANSFER_TXN],signers:[s]},{txn:v[Xn.APP_CALL_TXN],signers:[s]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function cr(){return(cr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,o=e.signedTxns,t.next=3,st(r,[o]);case 3:return s=t.sent,i=v(s,1),c=i[0].txnID,t.next=8,$e(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pr,lr,dr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=sr(a,n),s=o.asset1OutputAmount,u=o.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:s},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,o=t.assetOutID,s=t.decimals,u=BigInt(a),i=sr(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(o===n.asset1ID){var d=or.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:s}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(o!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=or.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:s}),I=x.swapOutputAmount,A=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:h}}}return e},generateTxns:function(t){return ur.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return ir.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return cr.apply(this,arguments)}},fr=(f(pr={},N.V1_1,{generateTxns:function(t){return Gn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,s=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:e.poolTokenID,poolTokenIn:a,asset1ID:e.asset1ID,asset1Out:o,asset2ID:e.asset2ID,asset2Out:s}},signTxns:function(t){return Wn.apply(this,arguments)},execute:function(t){return zn.apply(this,arguments)}}),f(pr,N.V2,dr),pr),mr=3n,gr=1000n;function xr(){return(xr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,t.next=3,o([r]);case 3:return s=t.sent,i=v(s,2),c=i[0],p=i[1],l=r.map((function(t,e){return e===lr.FEE_TXN_INDEX?c:e===lr.ASSET_IN_TXN_INDEX?p:a.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ir(){return(Ir=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,I,A,h,T,y,w,v;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.quote,o=e.swapType,s=e.slippage,i=e.initiatorAddr,c=r.pool,p=r.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new M({pool:c,quote:r},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return g=t.sent,x=[lt("swap"),o===exports.SwapType.FixedInput?lt("fi"):lt("fo")],I=a.default.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:x,accounts:[i],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==c.asset2ID?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),A=o===exports.SwapType.FixedOutput?tt("positive",s,p.assetInAmount):p.assetInAmount,h=0===l?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:f,amount:A,suggestedParams:g}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:l,amount:A,suggestedParams:g}),T=o===exports.SwapType.FixedInput?tt("negative",s,p.assetOutAmount):p.assetOutAmount,y=0===d?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:i,amount:T,suggestedParams:g}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:i,assetIndex:d,amount:T,suggestedParams:g}),w=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:f,amount:I.fee+y.fee,note:O,suggestedParams:g}),v=a.default.assignGroupID([w,I,h,y]),t.abrupt("return",[{txn:v[0],signers:[i]},{txn:v[1],signers:[f]},{txn:v[2],signers:[i]},{txn:v[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap on a non-existent pool");var o,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*mr/gr,l=i-u*i/(u+(c-p));if(l>i)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Kt(d),priceImpact:Ht(s({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function hr(t){return Tr.apply(this,arguments)}function Tr(){return(Tr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,Ae({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ae({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:s.id,assetOutAmount:BigInt(s.amount)+A,excessAmount:{assetID:s.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap on a non-existent pool");var o,s,u,i=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,s=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,s=n.asset1,u=n.asset2}if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=s*u/(u-i)-s,p=c*gr/(gr-mr),l=p-c,d=et(a.assetOut,Number(i))/et(a.assetIn,Number(p)),f=et(a.assetOut,Number(u))/et(a.assetIn,Number(s)),m=rt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:o,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function wr(t){return vr.apply(this,arguments)}function vr(){return(vr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,I,A;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,Ae({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ae({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,I=g.excessAsset1):(x=c.excessAsset2,I=g.excessAsset2),(A=I-x)<0n&&(A=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:s.id,assetOutAmount:BigInt(s.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _r(){return(_r=c(u().mark((function t(e){var n,r,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,o=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new M({pool:r,swapType:a,txGroup:o},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:o[lr.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:o[lr.ASSET_IN_TXN_INDEX].txn.amount},l={id:o[lr.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:o[lr.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,hr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,wr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",s(s({},d),{},{groupID:ct(o),fees:it(o)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new M(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(lr||(lr={}));var Sr,Er={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Ar({pool:e,reserves:n,assetIn:r,decimals:a}):yr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Ar,getFixedOutputSwapQuote:yr,generateTxns:function(t){return Ir.apply(this,arguments)},signTxns:function(t){return xr.apply(this,arguments)},execute:function(t){return _r.apply(this,arguments)},executeFixedOutputSwap:wr};function br(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=v(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(re(n)&&!re(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value})).map((function(t){return t.value}))}))}function Dr(){return(Dr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,a=e.assetIn,o=e.assetOut,s=e.isSwapRouterEnabled,i=e.pools,c=[],(p=i.find((function(t){return t.info.contractVersion===N.V1_1})))?(l={pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves},c.push(new Promise((function(t,e){try{t(Er.getFixedInputSwapQuote(l))}catch(t){e(t)}})))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap from non-existent pool"))),d=i.find((function(t){return t.info.contractVersion===N.V2})),c.push(or.getFixedInputSwapQuote({amount:r,assetInID:X(a),assetOutID:X(o),pool:null!==(n=null==d?void 0:d.info)&&void 0!==n?n:null,decimals:{assetIn:a.decimals,assetOut:o.decimals},isSwapRouterEnabled:s,network:e.network})),t.next=8,br(c);case 8:return f=t.sent,t.abrupt("return",ne(f));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,a=e.assetIn,o=e.assetOut,s=e.pools,i=e.isSwapRouterEnabled,c=[],(p=s.find((function(t){return t.info.contractVersion===N.V1_1})))?(l={pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves},c.push(new Promise((function(t,e){try{t(Er.getFixedOutputSwapQuote(l))}catch(t){e(t)}})))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap from non-existent pool"))),d=s.find((function(t){return t.info.contractVersion===N.V2})),c.push(or.getFixedOutputSwapQuote({amount:r,assetInID:X(a),assetOutID:X(o),pool:null!==(n=null==d?void 0:d.info)&&void 0!==n?n:null,decimals:{assetIn:a.decimals,assetOut:o.decimals},isSwapRouterEnabled:i,network:e.network})),t.next=8,br(c);case 8:return f=t.sent,t.abrupt("return",ne(f));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Nr=(f(Sr={},N.V1_1,Er),f(Sr,N.V2,or),f(Sr,"getQuote",(function(t){var e=t.type,n=t.pools;if(!t.isSwapRouterEnabled&&n.every((function(t){return Re(t.reserves)})))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools.");if(e===exports.SwapType.FixedInput)return function(t){return Dr.apply(this,arguments)}(t);if(e===exports.SwapType.FixedOutput)return function(t){return Or.apply(this,arguments)}(t);throw new D(exports.SwapQuoteErrorType.InvalidSwapTypeError,"Invalid swap type")})),f(Sr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&te(t.quote)===N.V1_1?Er.generateTxns(s(s({},t),{},{quote:t.quote.data})):or.generateTxns(t)})),f(Sr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&te(t.quote)===N.V1_1){var e=t.quote.data.pool;return Er.signTxns(s({pool:e},t))}return or.signTxns(t)})),f(Sr,"execute",(function(t){return t.contractVersion===N.V1_1?Er.execute(t):or.execute(t)})),Sr);function Pr(){return(Pr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,kr({txGroup:a,pool:r,initiatorSigner:o});case 4:return s=t.sent,t.next=7,st(n,[s]);case 7:return i=t.sent,c=v(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:it(a),confirmedRound:d,txnID:l,groupID:ct(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function kr(t){return Lr.apply(this,arguments)}function Lr(){return(Lr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.pool,o=e.initiatorSigner,t.next=3,o([n]);case 3:return s=t.sent,i=v(s,1),c=i[0],p=r.account.lsig,l=n.map((function(t,e){return 0===e?c:a.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rr(){return Rr=c(u().mark((function t(e){var n,r,o,s,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.data,o=e.initiatorSigner,t.prev=1,s=r.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:it(e),groupID:ct(e),lsig:n.account.lsig}})),t.next=5,o(s.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(s.map((function(t,e){return new Promise(function(){var r=c(u().mark((function r(o,s){var c,p,l,d,f,m;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,c=t.txns.map((function(n,r){return 0===r?i[e]:a.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),r.next=4,st(n,[c]);case 4:p=r.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),r.next=15;break;case 12:r.prev=12,r.t0=r.catch(0),s(r.t0);case 15:case"end":return r.stop()}}),r,null,[[0,12]])})));return function(t,e){return r.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Rr.apply(this,arguments)}function Fr(){return(Fr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,o=e.assetID,s=e.assetOut,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=r.account.address(),l=a.default.makeApplicationNoOpTxnFromObject({from:p,appIndex:r.validatorAppID,appArgs:[lt("redeem")],note:mt.getAppCallTxnNoteWithClientName(r.contractVersion),accounts:[i],foreignAssets:0==r.asset2ID?[r.asset1ID,r.poolTokenID]:[r.asset1ID,r.asset2ID,r.poolTokenID],suggestedParams:c}),d=0===o?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:i,amount:BigInt(s),suggestedParams:c}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:i,assetIndex:o,amount:BigInt(s),suggestedParams:c}),f=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:p,amount:l.fee+d.fee,note:O,suggestedParams:c}),m=a.default.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[i]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,o=e.liquidityAssetID,s=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(s.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[lt("commit"),i],note:U([lt("tinymanStaking/v1:b"),c,t.encodeUint64(o),i])})}function Mr(){return(Mr=c(u().mark((function e(n){var r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,s=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Br({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof s){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[lt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=P,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=jn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=pn,exports.CONTRACT_VERSION=N,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=k,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=fr,exports.Swap=Nr,exports.SwapQuoteError=D,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=qn,exports.V2AddLiquidityTxnIndices=In,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=tt,exports.calculateAccountMinimumRequiredBalance=me,exports.calculatePriceImpact=Ht,exports.calculateSwapRate=Kt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=Jt,exports.isSwapQuoteErrorCausedByAmount=re,exports.poolUtils=Qe,exports.prepareCommitTransactions=function(t){return Mr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Rr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Pr.apply(this,arguments)},exports.sendAndWaitRawTransaction=st,exports.sumUpTxnFees=it,exports.tinymanContract_v1_1=ue,exports.tinymanContract_v2=pe,exports.tinymanJSSDKConfig=mt; +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",o=r.asyncIterator||"@@asyncIterator",s=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,o=Object.create(a.prototype),s=new _(r||[]);return o._invoke=function(t,e,n){var r="suspendedStart";return function(a,o){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw o;return E()}for(n.method=a,n.arg=o;;){var s=n.delegate;if(s){var u=y(s,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,s),o}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var x=Object.getPrototypeOf,A=x&&x(x(S([])));A&&A!==e&&n.call(A,a)&&(g=A);var I=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,o,s,u){var i=p(t[a],t,o);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,s,u)}),(function(t){r("throw",t,s,u)})):e.resolve(l).then((function(t){c.value=t,s(c)}),(function(t){return r("throw",t,s,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function o(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(o,o):o()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function w(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function _(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function S(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,o=function e(){for(;++r=0;--a){var o=this.tryEntries[a],s=o.completion;if("root"===o.tryLoc)return r("end");if(o.tryLoc<=this.prev){var u=n.call(o,"catchLoc"),i=n.call(o,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),w(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;w(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:S(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,o,s){try{var u=t[o](s),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function s(t){i(o,r,a,s,u,"next",t)}function u(t){i(o,r,a,s,u,"throw",t)}s(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,s=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){u=!0,o=t},f:function(){try{s||null==n.return||n.return()}finally{if(u)throw o}}}}exports.SwapQuoteErrorType=void 0,(r=exports.SwapQuoteErrorType||(exports.SwapQuoteErrorType={})).SwapRouterStaleDataError="SwapRouterStaleDataError",r.SwapRouterNoRouteError="SwapRouterNoRouteError",r.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",r.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",r.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",r.NoAvailablePoolError="NoAvailablePoolError",r.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",r.UnknownError="UnknownError",r.LowSwapAmountError="LowSwapAmountError",r.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",r.InvalidSwapTypeError="InvalidSwapTypeError";var D=function(t){m(n,h(Error));var e=v(n);function n(t,r){var a;return p(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(T(a),n),a}return d(n)}(),O=Uint8Array.from([1]),N={V1_1:"v1_1",V2:"v2"},P={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},k={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},L="- would result negative",R="logic eval error:",F="exceeds schema integer count",B=/transaction \w+:/,M=function(t){m(n,h(Error));var e=v(n);function n(t,r){var a;p(this,n);for(var o=arguments.length,s=new Array(o>2?o-2:0),u=2;ur?[s(s({},t),{},{id:n}),s(s({},e),{},{id:r})]:[s(s({},e),{},{id:r}),s(s({},t),{},{id:n})]}function C(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function j(t){return 0===Number(t)}function X(t){return Number(t.id)}function q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,o=void 0!==a&&a,s={},u=b(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}s[o?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return s}function U(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,o=b(t);try{for(o.s();!(e=o.n()).done;){var s=e.value;r.set(s,a),a+=s.length}}catch(t){o.e(t)}finally{o.f()}return r}var G=100000n,W=100000n,z=100000n,J=25000n+25000n,Y=25000n+3500n;function K(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],o=t["created-apps"]||[],s=t.assets||[];return G+W*BigInt(s.length)+z*BigInt(o.length+a.length)+Y*r+J*n}function H(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,H(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function tt(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function et(t,e){var n=Number(t);return rt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function nt(t,e){return rt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function rt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=w(ot(e),2),o=a[0],s=a[1],u=w(ot(Math.round(Number(at(o,s+r)))),2),i=u[0],c=u[1];return Number(at(i,c-r))}return Math.round(e)}function at(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ot(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function st(t,e){return ut.apply(this,arguments)}function ut(){return(ut=c(u().mark((function t(e,n){var r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=b(n),t.prev=3,a.s();case 5:if((o=a.n()).done){t.next=18;break}return s=o.value,t.next=9,e.sendRawTransaction(s).do();case 9:return i=t.sent,c=i.txId,t.next=13,Z(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new M(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function it(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ct(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function pt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function lt(t){return(new TextEncoder).encode(t)}function dt(t){return Boolean(t)&&void 0!==t.fallback_message}var ft,mt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return lt("tinyman/".concat(t===N.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),gt=(f(ft={},N.V1_1,{testnet:62368684,mainnet:552635992}),f(ft,N.V2,{testnet:148607e3,mainnet:1002541853}),ft);function xt(t,e){var n=gt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function At(){return(At=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationOptInTxnFromObject({from:s,appIndex:xt(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var It;function ht(){return(ht=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationClearStateTxnFromObject({from:s,appIndex:xt(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(It=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",It.FixedOutput="fixed-output";var Tt,yt,vt;!function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(vt||(vt={}));var wt,_t,St=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),Et=lt("swap"),bt=(f(yt={},exports.SwapType.FixedInput,lt("fixed-input")),f(yt,exports.SwapType.FixedOutput,lt("fixed-output")),yt),Dt={ASSET_OPT_IN:lt("asset_opt_in")},Ot={testnet:159521633,mainnet:0},Nt=(f(wt={},exports.SwapType.FixedInput,7),f(wt,exports.SwapType.FixedOutput,8),wt),Pt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function kt(t){var e=Bt(t),n=e.assetIn,r=e.assetOut;return et(r.asset.decimals,Number(r.amount))/et(n.asset.decimals,Number(n.amount))}function Lt(t){var e=Ot[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Rt(t){return t[t.length-1].quote.amount_out}function Ft(t){return t[0].quote.amount_in}function Bt(t){return{assetIn:Ft(t),assetOut:Rt(t)}}function Mt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function e(n){var r,o,s,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.routerAppID,s=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o,appArgs:[Dt.ASSET_OPT_IN],foreignAssets:s,suggestedParams:c}),l=s.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,w,_,S,E,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,o=n.client,s=n.network,i=n.swapType,c=n.route,e.next=3,o.getTransactionParams().do();case 3:return p=e.sent,l=[X(c[0].quote.amount_in.asset),X(c[0].quote.amount_out.asset),X(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Ft(c).amount),Number(Rt(c).amount)],x=g[0],A=g[1],I=[c[0].pool.address,c[1].pool.address],h=I[0],T=I[1],y=j(d),v=Lt(s),w=y?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(_=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:v,appArgs:[Et,bt[i],a.default.encodeUint64(A)],foreignApps:[xt(s,N.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Nt[i]+1),S=[w,_],e.next=15,jt({client:o,network:s,assetIDs:[d,f,m]});case 15:if(!((E=e.sent).length>0)){e.next=21;break}return e.next=19,Mt({client:o,initiatorAddr:r,assetIDs:E,routerAppID:v});case 19:b=e.sent,S.unshift(b);case 21:return D=a.default.assignGroupID(S),e.abrupt("return",D.map((function(t){return{txn:t,signers:[r]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(u().mark((function e(n){var r,a,o,s,i,c;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,s=t.getApplicationAddress(Lt(a)),e.next=4,r.accountInformation(s).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return 0!==t&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qt(t){return Ut.apply(this,arguments)}function Ut(){return(Ut=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.swapType,s=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Pt[s].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(!dt(p)){t.next=13;break}throw new D(p.type,p.fallback_message);case 13:throw new D(exports.SwapQuoteErrorType.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new D(exports.SwapQuoteErrorType.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapQuoteType=void 0,(_t=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",_t.Router="router";var Gt=4*t.ALGORAND_MIN_TX_FEE;function Wt(e){return(St[e]+2)*t.ALGORAND_MIN_TX_FEE}function zt(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:.003;return Math.ceil(1/t)}function Jt(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:.003;return tn&&(e=a,n=o)}return e}function re(t){return t instanceof D&&[exports.SwapQuoteErrorType.SwapRouterInsufficientReservesError,exports.SwapQuoteErrorType.SwapRouterLowSwapAmountError,exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,exports.SwapQuoteErrorType.LowSwapAmountError].includes(t.type)}var ae={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},oe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},se=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ue=new(function(n){m(a,se);var r=v(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=xt(n.network,N.V1_1),a=w(C(n.asset1ID,n.asset2ID),2),o=a[0],s=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:o,asset_id_2:s,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(s(s({},i),{},{minimum_required_balance:me(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),o(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function fe(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function me(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ge,xe,Ae=lt("e");function Ie(t){return he.apply(this,arguments)}function he(){return(he=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,w,_,S,E;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.pool,i=r.accountAddr,n.next=3,o.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=s.account.address(),g=b(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((A=x.value).id==s.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=q({stateArray:I}),T=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ae,a.default.encodeUint64(s.asset1ID)])),y=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ae,a.default.encodeUint64(s.asset2ID)])),v=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ae,a.default.encodeUint64(s.poolTokenID)])),w=h[T],_=h[y],S=h[v],"bigint"==typeof w&&(l=w),"bigint"==typeof _&&(d=_),"bigint"==typeof S&&(f=S);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((E={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Te(){return(Te=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,x,A,I,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.accountAddr,i=r.validatorAppID,n.next=3,o.accountInformation(s).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mwe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(we,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function be(){return be=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,de(n,r);case 6:return s=t.sent,i=fe(s,xt(a,N.V1_1)),c=null,i&&(l=s["created-assets"][0],p=l.index,c={asset1ID:i[ye[N.V1_1].asset1],asset2ID:i[ye[N.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),be.apply(this,arguments)}var De=Object.freeze({__proto__:null,getPoolInfo:_e,getPoolReserves:function(t,e){return Ee.apply(this,arguments)},getPoolAssets:function(t){return be.apply(this,arguments)}});function Oe(t){return Ne.apply(this,arguments)}function Ne(){return(Ne=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,o=e.asset2ID,s=le(N.V2),i=s.generateLogicSigAccountForPool(e),c=xt(r,N.V2),p=i.address(),l=C(a,o),t.next=8,de(n,p);case 8:return d=t.sent,f=fe(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:N.V2},f&&(m.asset1ProtocolFees=BigInt(f[ye.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ye.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ye.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ye.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ye.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ye.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ye.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ye.v2.totalFeeShare]),m.poolTokenID=Number(f[ye.v2.poolTokenID]),m.asset1ID=Number(f[ye.v2.asset1]),m.asset2ID=Number(f[ye.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pe(){return(Pe=c(u().mark((function t(e,n){var r,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,de(e,n.account.address());case 2:return r=t.sent,a=fe(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[ye.v2.asset1Reserves]),o.asset2=BigInt(a[ye.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[ye.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ke(){return(ke=c(u().mark((function t(e){var n,r,a,o,s,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,de(n,r);case 3:return o=t.sent,s=fe(o,xt(a,N.V2)),i=null,s&&(i={asset1ID:s[ye[N.V2].asset1],asset2ID:s[ye[N.V2].asset2],poolTokenID:s[ye[N.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Le=Object.freeze({__proto__:null,getPoolInfo:Oe,getPoolReserves:function(t,e){return Pe.apply(this,arguments)},getPoolAssets:function(t){return ke.apply(this,arguments)}});function Re(t){return Boolean(t&&!(t.asset1+t.asset2))}var Fe,Be,Me=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Re(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=et(t.asset1,e.asset1)/et(t.asset2,e.asset2)),r},isPoolEmpty:Re,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([_e(t),Oe(t)])}}),Qe=s((f(Fe={},N.V1_1,s(s({},De),Me)),f(Fe,N.V2,s(s({},Le),Me)),Fe),Me);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Be||(Be={}));var Ve=96e4,Ce=859e3;function je(){return(je=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,_,S,E,b,D,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.asset_1,s=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=V(o,s),l=w(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,A=g.unit_name,I=j(x),h=xt(r,N.V1_1),T=ue.generateLogicSigAccountForPool({network:r,asset1ID:f,asset2ID:x}),y=T.address(),v=a.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:mt.getAppCallTxnNoteWithClientName(N.V1_1),appArgs:[lt("bootstrap"),a.default.encodeUint64(f),a.default.encodeUint64(x)],foreignAssets:I?[f]:[x],suggestedParams:c}),_=a.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:k.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(A),assetURL:"https://tinyman.org",suggestedParams:c}),S=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Xe(I),suggestedParams:c}),(b=[])[Be.FUNDING_TXN]=E,b[Be.VALIDATOR_APP_CALL]=v,b[Be.POOL_TOKEN_CREATE]=_,b[Be.ASSET1_OPT_IN]=S,I||(b[Be.ASSET2_OPT_IN]=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),D=a.default.assignGroupID(b),O=[{txn:D[Be.FUNDING_TXN],signers:[i]},{txn:D[Be.VALIDATOR_APP_CALL],signers:[y]},{txn:D[Be.POOL_TOKEN_CREATE],signers:[y]},{txn:D[Be.ASSET1_OPT_IN],signers:[y]}],D[Be.ASSET2_OPT_IN]&&O.push({txn:D[Be.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(t){return t?Ve:Ce}function qe(){return(qe=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=C(s,i),f=w(d,2),m=f[0],g=f[1],x=ue.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),A=[],I=n.map((function(t,e){if(e===Be.FUNDING_TXN)return A.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,x),r=n.txID,o=n.blob;return A.push(r),o})),t.abrupt("return",{signedTxns:I,txnIDs:A});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ue(t){return Ge.apply(this,arguments)}function Ge(){return(Ge=c(u().mark((function t(e){var n,r,a,o,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Z(n,a[Be.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(s=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(s));case 10:return t.abrupt("return",{poolTokenID:s});case 13:throw t.prev=13,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function We(){return(We=c(u().mark((function t(e){var n,r,a,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Ue({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",Qe.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ze,Je={generateTxns:function(t){return je.apply(this,arguments)},signTxns:function(t){return qe.apply(this,arguments)},execute:function(t){return We.apply(this,arguments)},getBootstrapFundingTxnAmount:Xe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(ze||(ze={}));var Ye=5,Ke=6;function He(t,e){return Ze.apply(this,arguments)}function Ze(){return(Ze=c(u().mark((function e(n,r){var a,o,s;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return s=e.t0,e.abrupt("return",s);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function $e(t,e){return tn.apply(this,arguments)}function tn(){return(tn=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,He(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(){return(en=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,_,S;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.network,s=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return p=e.sent,l=xt(o,N.V2),d=t.getApplicationAddress(l),f=V(s,i),m=w(f,2),g=m[0].id,x=m[1].id,e.next=9,Qe.v2.getPoolInfo({client:r,network:o,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(s.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=pe.generateLogicSigAccountForPool({network:o,asset1ID:g,asset2ID:x}),I=A.address(),h=j(x),(T=a.default.makeApplicationOptInTxnFromObject({from:I,appIndex:l,appArgs:[lt("bootstrap")],note:mt.getAppCallTxnNoteWithClientName(N.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=rn(h),y=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:I,amount:nn(h),suggestedParams:p}),(v=[])[ze.FUNDING_TXN]=y,v[ze.VALIDATOR_APP_CALL]=T,_=a.default.assignGroupID(v),(S=[])[ze.FUNDING_TXN]={txn:_[ze.FUNDING_TXN],signers:[c]},S[ze.VALIDATOR_APP_CALL]={txn:_[ze.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",S);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function nn(t){return function(t,e){var n=le(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(N.V2,t)+rn(t)+1e5}function rn(e){return((e?Ye:Ke)+1)*t.ALGORAND_MIN_TX_FEE}function an(){return(an=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=C(s,i),f=w(d,2),m=f[0],g=f[1],x=pe.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),A=[],I=n.map((function(t,e){if(e===ze.FUNDING_TXN)return A.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,x),r=n.txID,o=n.blob;return A.push(r),o})),t.abrupt("return",{signedTxns:I,txnIDs:A});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function on(){return(on=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,He(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ye.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Qe.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 21:throw t.prev=21,t.t3=t.catch(1),new M(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var sn,un={generateTxns:function(t){return en.apply(this,arguments)},signTxns:function(t){return an.apply(this,arguments)},execute:function(t){return on.apply(this,arguments)},getBootstrapFundingTxnAmount:nn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+nn(e)}};var cn,pn=(f(sn={},N.V1_1,Je),f(sn,N.V2,un),f(sn,"generateTxns",(function(t){return t.contractVersion===N.V1_1?Je.generateTxns(t):un.generateTxns(t)})),f(sn,"signTxns",(function(t){return t.contractVersion===N.V1_1?Je.signTxns(t):un.signTxns(t)})),f(sn,"execute",(function(t){return t.contractVersion===N.V1_1?Je.execute(t):un.execute(t)})),f(sn,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===N.V1_1?Je.getBootstrapFundingTxnAmount(n):un.getBootstrapFundingTxnAmount(n)})),sn);exports.V1_1AddLiquidityTxnIndices=void 0,(cn=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[cn.FEE_TXN=0]="FEE_TXN",cn[cn.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",cn[cn.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",cn[cn.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",cn[cn.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var ln,dn,fn,mn,gn=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(mn=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",mn.FLEXIBLE="flexible",mn.INITIAL="initial";var xn,An=(f(ln={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(ln,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(ln,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),ln),In=(f(dn={},exports.V2AddLiquidityType.INITIAL,1),f(dn,exports.V2AddLiquidityType.SINGLE,2),f(dn,exports.V2AddLiquidityType.FLEXIBLE,2),dn),hn=(f(fn={},exports.V2AddLiquidityType.INITIAL,3),f(fn,exports.V2AddLiquidityType.FLEXIBLE,3),f(fn,exports.V2AddLiquidityType.SINGLE,2),fn);function Tn(t){var e,n,r,a=t.reserves,o=t.totalFeeShare,s=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(s.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(s.amount)-m,A=BigInt(u.amount)-g;if(x>A){var I=x;r=yn(I,o),e={id:s.id,amount:I+r,decimals:s.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=A;r=yn(h,o),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:s.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:s.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Ht({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function yn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function vn(e){return(In[e]+1)*t.ALGORAND_MIN_TX_FEE}var wn=lt("add_liquidity"),_n=(f(xn={},N.V1_1,[lt("mint")]),f(xn,N.V2,{INITIAL_LIQUIDITY:[lt("add_initial_liquidity")],SINGLE_ASSET_MODE:[wn,lt("single")],FLEXIBLE_MODE:[wn,lt("flexible")]}),xn);function Sn(){return(Sn=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.poolAddress,s=e.asset1In,i=e.asset2In,c=e.poolTokenOut,p=e.slippage,l=e.initiatorAddr,d=tt("negative",p,c.amount),t.next=4,n.getTransactionParams().do();case 4:return f=t.sent,m=a.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:xt(r,N.V1_1),appArgs:_n.v1_1,accounts:[l],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==i.id?[s.id,c.id]:[s.id,i.id,c.id],suggestedParams:f}),g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:s.id,amount:s.amount,suggestedParams:f}),x=0===i.id?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:i.amount,suggestedParams:f}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),I=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+A.fee,note:O,suggestedParams:f}),h=a.default.assignGroupID([I,m,g,x,A]),t.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,s=n.account,t.next=4,o([r]);case 4:return i=t.sent,c=w(i,3),p=c[0],l=c[1],d=c[2],f=r.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:a.default.signLogicSigTransactionObject(t.txn,s).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function bn(){return(bn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,A,I,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.signedTxns,s=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Ie({client:n,pool:r,accountAddr:s});case 5:return c=t.sent,t.next=8,st(n,[o]);case 8:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=it(a),x=ct(a),t.next=17,Ie({client:n,pool:r,accountAddr:s});case 17:return A=t.sent,(I=A.excessPoolTokens-c.excessPoolTokens)<0n&&(I=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+I,excessAmount:{excessAmountForAddingLiquidity:I,totalExcessAmount:A.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new M(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Dn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var o=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(o<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(o,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:o-BigInt(1e3),share:1}}var s=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=ss)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Jt(Number(e)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");return{assetInID:u,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:et(p,Number(m))/et(i,Number(l)),priceImpact:x}}function $n(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!Qe.isPoolReady(a))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,s,u=n.id,i=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)o=a.asset2Reserves,s=a.asset1Reserves;else{if(c!==a.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));o=a.asset1Reserves,s=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,o=t.decimals,s=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:s,totalFeeShare:a}),i=s+u,c=Ht({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:o.assetIn},assetOut:{amount:r,decimals:o.assetOut}});return{swapInputAmount:i,totalFeeAmount:u,priceImpact:c}}({inputSupply:o,outputSupply:s,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:i,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact;if(l>s)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Jt(Number(m)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");return{type:exports.SwapQuoteType.Direct,data:{pool:a,quote:{assetInID:u,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:et(p,Number(l))/et(i,Number(m)),priceImpact:x}}}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,o=e.isSwapRouterEnabled,s=e.network,i=e.pool,c=[],i?c.push(new Promise((function(t,e){try{var o=Zn({amount:n,assetIn:r,assetOut:a,pool:i});t({type:exports.SwapQuoteType.Direct,data:{pool:i,quote:o}})}catch(t){e(t)}}))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(qt({amount:n,assetInID:r.id,assetOutID:a.id,swapType:exports.SwapType.FixedInput,network:s}).then((function(t){return{type:exports.SwapQuoteType.Router,data:t}}))),t.next=6,Hn(c);case 6:return p=t.sent,t.abrupt("return",ne(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(t){return rr.apply(this,arguments)}function rr(){return(rr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,o=e.isSwapRouterEnabled,s=e.network,i=e.pool,c=[new Promise((function(t,e){try{t($n({amount:n,assetIn:r,assetOut:a,pool:i}))}catch(t){e(t)}}))],o&&c.push(qt({amount:n,assetInID:r.id,assetOutID:a.id,swapType:exports.SwapType.FixedOutput,network:s}).then((function(t){return{type:exports.SwapQuoteType.Router,data:t}}))),t.next=5,Hn(c);case 5:return p=t.sent,t.abrupt("return",ne(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,o=t.decimals,s=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-s});return{swapOutputAmount:u,totalFeeAmount:s,priceImpact:Ht({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:o.assetIn},assetOut:{amount:u,decimals:o.assetOut}})}}!function(t){t[t.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(Xn||(Xn={}));var or={getQuote:function(t){return Kn.apply(this,arguments)},getFixedInputSwapQuote:tr,getFixedInputDirectSwapQuote:Zn,getFixedOutputDirectSwapQuote:$n,getFixedOutputSwapQuote:nr,generateTxns:function(t){return Jn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Yn.apply(this,arguments)},calculateFixedInputSwap:ar};function sr(t,e){var n,r,a=BigInt(t),o=e.issuedLiquidity;return o>a+BigInt(1e3)?(n=a*e.asset1/o,r=a*e.asset2/o):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function ur(){return(ur=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.account.address(),m=o.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:s,suggestedParams:d}),(x=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Un,a.default.encodeUint64(tt("negative",l,c)),a.default.encodeUint64(tt("negative",l,p))],accounts:[f],foreignAssets:[o.asset1ID,o.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(A=[])[Xn.ASSET_TRANSFER_TXN]=g,A[Xn.APP_CALL_TXN]=x,I=a.default.assignGroupID(A),e.abrupt("return",[{txn:I[Xn.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[Xn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ir(){return(ir=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,w;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.asset1ID,m=o.asset2ID,g=o.account.address(),x=o.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,h=tt("negative",l,p),c!==f){e.next=17;break}A=h,I=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}A=0,I=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=a.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Un,a.default.encodeUint64(A),a.default.encodeUint64(I)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[Xn.ASSET_TRANSFER_TXN]=T,v[Xn.APP_CALL_TXN]=y,w=a.default.assignGroupID(v),e.abrupt("return",[{txn:w[Xn.ASSET_TRANSFER_TXN],signers:[s]},{txn:w[Xn.APP_CALL_TXN],signers:[s]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function cr(){return(cr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,o=e.signedTxns,t.next=3,st(r,[o]);case 3:return s=t.sent,i=w(s,1),c=i[0].txnID,t.next=8,$e(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pr,lr,dr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=sr(a,n),s=o.asset1OutputAmount,u=o.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:s},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,o=t.assetOutID,s=t.decimals,u=BigInt(a),i=sr(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(o===n.asset1ID){var d=or.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:s}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(o!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=or.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:s}),A=x.swapOutputAmount,I=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:p+A},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:A},swapFees:{assetId:n.asset2ID,amount:I},priceImpact:h}}}return e},generateTxns:function(t){return ur.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return ir.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return cr.apply(this,arguments)}},fr=(f(pr={},N.V1_1,{generateTxns:function(t){return Gn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,s=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:e.poolTokenID,poolTokenIn:a,asset1ID:e.asset1ID,asset1Out:o,asset2ID:e.asset2ID,asset2Out:s}},signTxns:function(t){return Wn.apply(this,arguments)},execute:function(t){return zn.apply(this,arguments)}}),f(pr,N.V2,dr),pr),mr=3n,gr=1000n;function xr(){return(xr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,t.next=3,o([r]);case 3:return s=t.sent,i=w(s,2),c=i[0],p=i[1],l=r.map((function(t,e){return e===lr.FEE_TXN_INDEX?c:e===lr.ASSET_IN_TXN_INDEX?p:a.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(){return(Ar=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.quoteAndPool,o=e.swapType,s=e.slippage,i=e.initiatorAddr,c=r.pool,p=r.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new M({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return g=t.sent,x=[lt("swap"),o===exports.SwapType.FixedInput?lt("fi"):lt("fo")],A=a.default.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:x,accounts:[i],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==c.asset2ID?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),I=o===exports.SwapType.FixedOutput?tt("positive",s,p.assetInAmount):p.assetInAmount,h=0===l?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:f,amount:I,suggestedParams:g}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:l,amount:I,suggestedParams:g}),T=o===exports.SwapType.FixedInput?tt("negative",s,p.assetOutAmount):p.assetOutAmount,y=0===d?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:i,amount:T,suggestedParams:g}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:i,assetIndex:d,amount:T,suggestedParams:g}),v=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:f,amount:A.fee+y.fee,note:O,suggestedParams:g}),w=a.default.assignGroupID([v,A,h,y]),t.abrupt("return",[{txn:w[0],signers:[i]},{txn:w[1],signers:[f]},{txn:w[2],signers:[i]},{txn:w[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ir(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*mr/gr,l=i-u*i/(u+(c-p));if(l>i)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Kt(d),priceImpact:Ht(s({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function hr(t){return Tr.apply(this,arguments)}function Tr(){return(Tr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,Ie({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ie({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,A=g.excessAsset1):(x=c.excessAsset2,A=g.excessAsset2),(I=A-x)<0n&&(I=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:s.id,assetOutAmount:BigInt(s.amount)+I,excessAmount:{assetID:s.id,excessAmountForSwap:I,totalExcessAmount:A},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,s,u,i=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,s=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,s=n.asset1,u=n.asset2}if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=s*u/(u-i)-s,p=c*gr/(gr-mr),l=p-c,d=et(a.assetOut,Number(i))/et(a.assetIn,Number(p)),f=et(a.assetOut,Number(u))/et(a.assetIn,Number(s)),m=rt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:o,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function vr(t){return wr.apply(this,arguments)}function wr(){return(wr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,Ie({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ie({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,A=g.excessAsset1):(x=c.excessAsset2,A=g.excessAsset2),(I=A-x)<0n&&(I=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-I,assetOutID:s.id,assetOutAmount:BigInt(s.amount),excessAmount:{assetID:o.id,excessAmountForSwap:I,totalExcessAmount:A},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _r(){return(_r=c(u().mark((function t(e){var n,r,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,o=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new M({pool:r,swapType:a,txGroup:o},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:o[lr.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:o[lr.ASSET_IN_TXN_INDEX].txn.amount},l={id:o[lr.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:o[lr.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,hr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,vr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",s(s({},d),{},{groupID:ct(o),fees:it(o)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new M(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(lr||(lr={}));var Sr,Er={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Ir({pool:e,reserves:n,assetIn:r,decimals:a}):yr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Ir,getFixedOutputSwapQuote:yr,generateTxns:function(t){return Ar.apply(this,arguments)},signTxns:function(t){return xr.apply(this,arguments)},execute:function(t){return _r.apply(this,arguments)},executeFixedOutputSwap:vr};function br(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=w(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(re(n)&&!re(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Dr(){return(Dr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,a=e.assetIn,o=e.assetOut,s=e.isSwapRouterEnabled,i=e.pools,c=[],(p=i.find((function(t){return t.info.contractVersion===N.V1_1})))?c.push(new Promise((function(t,e){try{t(Er.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===N.V2})),c.push(or.getFixedInputSwapQuote({amount:r,assetIn:{id:X(a),decimals:a.decimals},assetOut:{id:X(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:s,network:e.network})),t.next=8,br(c);case 8:return d=t.sent,t.abrupt("return",ne(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,a=e.assetIn,o=e.assetOut,s=e.pools,i=e.isSwapRouterEnabled,c=[],(p=s.find((function(t){return t.info.contractVersion===N.V1_1})))?c.push(new Promise((function(t,e){try{t(Er.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=s.find((function(t){return t.info.contractVersion===N.V2})),c.push(or.getFixedOutputSwapQuote({amount:r,assetIn:{id:X(a),decimals:a.decimals},assetOut:{id:X(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,br(c);case 8:return d=t.sent,t.abrupt("return",ne(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Nr=(f(Sr={},N.V1_1,Er),f(Sr,N.V2,or),f(Sr,"getQuote",(function(t){var e=t.type;if(e===exports.SwapType.FixedInput)return function(t){return Dr.apply(this,arguments)}(t);if(e===exports.SwapType.FixedOutput)return function(t){return Or.apply(this,arguments)}(t);throw new D(exports.SwapQuoteErrorType.InvalidSwapTypeError,"Invalid swap type")})),f(Sr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&te(t.quote)===N.V1_1?Er.generateTxns(s(s({},t),{},{quoteAndPool:t.quote.data})):or.generateTxns(t)})),f(Sr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&te(t.quote)===N.V1_1){var e=t.quote.data.pool;return Er.signTxns(s(s({},t),{},{pool:e}))}return or.signTxns(t)})),f(Sr,"execute",(function(t){return t.contractVersion===N.V1_1?Er.execute(t):or.execute(t)})),Sr);function Pr(){return(Pr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,kr({txGroup:a,pool:r,initiatorSigner:o});case 4:return s=t.sent,t.next=7,st(n,[s]);case 7:return i=t.sent,c=w(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:it(a),confirmedRound:d,txnID:l,groupID:ct(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function kr(t){return Lr.apply(this,arguments)}function Lr(){return(Lr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.pool,o=e.initiatorSigner,t.next=3,o([n]);case 3:return s=t.sent,i=w(s,1),c=i[0],p=r.account.lsig,l=n.map((function(t,e){return 0===e?c:a.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rr(){return Rr=c(u().mark((function t(e){var n,r,o,s,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.data,o=e.initiatorSigner,t.prev=1,s=r.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:it(e),groupID:ct(e),lsig:n.account.lsig}})),t.next=5,o(s.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(s.map((function(t,e){return new Promise(function(){var r=c(u().mark((function r(o,s){var c,p,l,d,f,m;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,c=t.txns.map((function(n,r){return 0===r?i[e]:a.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),r.next=4,st(n,[c]);case 4:p=r.sent,l=w(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),r.next=15;break;case 12:r.prev=12,r.t0=r.catch(0),s(r.t0);case 15:case"end":return r.stop()}}),r,null,[[0,12]])})));return function(t,e){return r.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Rr.apply(this,arguments)}function Fr(){return(Fr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,o=e.assetID,s=e.assetOut,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=r.account.address(),l=a.default.makeApplicationNoOpTxnFromObject({from:p,appIndex:r.validatorAppID,appArgs:[lt("redeem")],note:mt.getAppCallTxnNoteWithClientName(r.contractVersion),accounts:[i],foreignAssets:0==r.asset2ID?[r.asset1ID,r.poolTokenID]:[r.asset1ID,r.asset2ID,r.poolTokenID],suggestedParams:c}),d=0===o?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:i,amount:BigInt(s),suggestedParams:c}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:i,assetIndex:o,amount:BigInt(s),suggestedParams:c}),f=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:p,amount:l.fee+d.fee,note:O,suggestedParams:c}),m=a.default.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[i]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,o=e.liquidityAssetID,s=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(s.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[lt("commit"),i],note:U([lt("tinymanStaking/v1:b"),c,t.encodeUint64(o),i])})}function Mr(){return(Mr=c(u().mark((function e(n){var r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,s=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Br({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof s){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[lt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=P,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=jn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=pn,exports.CONTRACT_VERSION=N,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=k,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=fr,exports.Swap=Nr,exports.SwapQuoteError=D,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=qn,exports.V2AddLiquidityTxnIndices=An,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=tt,exports.calculateAccountMinimumRequiredBalance=me,exports.calculatePriceImpact=Ht,exports.calculateSwapRate=Kt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=Jt,exports.isSwapQuoteErrorCausedByAmount=re,exports.poolUtils=Qe,exports.prepareCommitTransactions=function(t){return Mr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Rr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Pr.apply(this,arguments)},exports.sendAndWaitRawTransaction=st,exports.sumUpTxnFees=it,exports.tinymanContract_v1_1=ue,exports.tinymanContract_v2=pe,exports.tinymanJSSDKConfig=mt; diff --git a/dist/swap/index.d.ts b/dist/swap/index.d.ts index 3cad225c..677b5e52 100644 --- a/dist/swap/index.d.ts +++ b/dist/swap/index.d.ts @@ -23,7 +23,7 @@ export declare const Swap: { assetOut: number; }; }) => import("./types").SwapQuote; - generateTxns: ({ client, quote, swapType, slippage, initiatorAddr }: import("./types").GenerateV1_1SwapTxnsParams) => Promise; + generateTxns: ({ client, quoteAndPool, swapType, slippage, initiatorAddr }: import("./types").GenerateV1_1SwapTxnsParams) => Promise; signTxns: ({ pool, txGroup, initiatorSigner }: { pool: import("..").V1PoolInfo; txGroup: import("..").SignerTransaction[]; @@ -47,60 +47,40 @@ export declare const Swap: { }) => Promise>; }; v2: { - getQuote: ({ type, amount, assetInID, assetOutID, decimals, network, isSwapRouterEnabled, pool }: { + getQuote: ({ type, amount, assetIn, assetOut, network, isSwapRouterEnabled, pool }: { type: import("./constants").SwapType; amount: number | bigint; - assetInID: number; - assetOutID: number; + assetIn: import("../util/asset/assetModels").AssetWithIdAndDecimals; + assetOut: import("../util/asset/assetModels").AssetWithIdAndDecimals; pool: import("..").V2PoolInfo | null; - decimals: { - assetIn: number; - assetOut: number; - }; network: import("..").SupportedNetwork; isSwapRouterEnabled?: boolean | undefined; }) => Promise; - getFixedInputSwapQuote: ({ amount, assetInID, assetOutID, decimals, isSwapRouterEnabled, network, pool }: { + getFixedInputSwapQuote: ({ amount, assetIn, assetOut, isSwapRouterEnabled, network, pool }: { amount: number | bigint; - assetInID: number; - assetOutID: number; - decimals: { - assetIn: number; - assetOut: number; - }; + assetIn: import("../util/asset/assetModels").AssetWithIdAndDecimals; + assetOut: import("../util/asset/assetModels").AssetWithIdAndDecimals; network: import("..").SupportedNetwork; pool: import("..").V2PoolInfo | null; isSwapRouterEnabled?: boolean | undefined; }) => Promise; - getFixedInputDirectSwapQuote: ({ amount, assetInID, assetOutID, decimals, pool }: { + getFixedInputDirectSwapQuote: ({ amount, assetIn, assetOut, pool }: { pool: import("..").V2PoolInfo; amount: number | bigint; - assetInID: number; - assetOutID: number; - decimals: { - assetIn: number; - assetOut: number; - }; + assetIn: import("../util/asset/assetModels").AssetWithIdAndDecimals; + assetOut: import("../util/asset/assetModels").AssetWithIdAndDecimals; }) => import("./types").DirectSwapQuote; - getFixedOutputDirectSwapQuote: ({ amount, assetInID, assetOutID, decimals, pool }: { + getFixedOutputDirectSwapQuote: ({ amount, assetIn, assetOut, pool }: { pool: import("..").V2PoolInfo | null; amount: number | bigint; - assetInID: number; - assetOutID: number; - decimals: { - assetIn: number; - assetOut: number; - }; + assetIn: import("../util/asset/assetModels").AssetWithIdAndDecimals; + assetOut: import("../util/asset/assetModels").AssetWithIdAndDecimals; }) => import("./types").SwapQuote; - getFixedOutputSwapQuote: ({ amount, assetInID, assetOutID, decimals, isSwapRouterEnabled, network, pool }: { + getFixedOutputSwapQuote: ({ amount, assetIn, assetOut, isSwapRouterEnabled, network, pool }: { amount: number | bigint; - assetInID: number; - assetOutID: number; + assetIn: import("../util/asset/assetModels").AssetWithIdAndDecimals; + assetOut: import("../util/asset/assetModels").AssetWithIdAndDecimals; pool: import("..").V2PoolInfo | null; - decimals: { - assetIn: number; - assetOut: number; - }; network: import("..").SupportedNetwork; isSwapRouterEnabled?: boolean | undefined; }) => Promise; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index 8be133fb..d196137b 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -106,15 +106,8 @@ export interface GenerateSwapTxnsParams { initiatorAddr: string; } export declare type GenerateV1_1SwapTxnsParams = Omit & { - quote: DirectSwapQuoteAndPool; + quoteAndPool: DirectSwapQuoteAndPool; }; -export interface GenerateSwapRouterTxnsParams { - client: Algodv2; - initiatorAddr: string; - swapType: SwapType; - route: SwapRoute; - network: SupportedNetwork; -} /** An object containing information about a successfully executed swap. */ export interface V1SwapExecution { /** The round that the swap occurred in. */ diff --git a/dist/swap/v1_1/index.d.ts b/dist/swap/v1_1/index.d.ts index 1066fda8..4117d8bc 100644 --- a/dist/swap/v1_1/index.d.ts +++ b/dist/swap/v1_1/index.d.ts @@ -9,7 +9,7 @@ declare function signTxns({ pool, txGroup, initiatorSigner }: { txGroup: SignerTransaction[]; initiatorSigner: InitiatorSigner; }): Promise; -declare function generateTxns({ client, quote, swapType, slippage, initiatorAddr }: GenerateV1_1SwapTxnsParams): Promise; +declare function generateTxns({ client, quoteAndPool, swapType, slippage, initiatorAddr }: GenerateV1_1SwapTxnsParams): Promise; /** * * @param type - Type of the swap diff --git a/dist/swap/v2/index.d.ts b/dist/swap/v2/index.d.ts index fb4e19f8..5032dd0c 100644 --- a/dist/swap/v2/index.d.ts +++ b/dist/swap/v2/index.d.ts @@ -3,6 +3,7 @@ import { InitiatorSigner, SignerTransaction, SupportedNetwork } from "../../util import { V2PoolInfo } from "../../util/pool/poolTypes"; import { DirectSwapQuote, GenerateSwapTxnsParams, SwapQuote, V2SwapExecution } from "../types"; import { SwapType } from "../constants"; +import { AssetWithIdAndDecimals } from "../../util/asset/assetModels"; declare function generateTxns(params: GenerateSwapTxnsParams): Promise; declare function signTxns({ txGroup, initiatorSigner }: { txGroup: SignerTransaction[]; @@ -20,56 +21,41 @@ declare function execute({ client, quote, txGroup, signedTxns }: { /** * @param type - Type of the swap * @param pool - Information for the pool. - * @param asset.assetID - ID of the asset to be swapped - * @param asset.amount - Amount of the asset to be swapped - * @param decimals.assetIn - Decimals quantity for the input asset - * @param decimals.assetOut - Decimals quantity for the output asset + * @param assetIn - Asset to be swapped + * @param assetOut - Asset to be received + * @param amount - Amount of asset to be swapped + * @param network - Network to be used + * @param isSwapRouterEnabled - Whether the swap router is enabled * @returns A promise for the Swap quote */ -declare function getQuote({ type, amount, assetInID, assetOutID, decimals, network, isSwapRouterEnabled, pool }: { +declare function getQuote({ type, amount, assetIn, assetOut, network, isSwapRouterEnabled, pool }: { type: SwapType; amount: number | bigint; - assetInID: number; - assetOutID: number; + assetIn: AssetWithIdAndDecimals; + assetOut: AssetWithIdAndDecimals; pool: V2PoolInfo | null; - decimals: { - assetIn: number; - assetOut: number; - }; network: SupportedNetwork; isSwapRouterEnabled?: boolean; }): Promise; -declare function getFixedInputDirectSwapQuote({ amount, assetInID, assetOutID, decimals, pool }: { +declare function getFixedInputDirectSwapQuote({ amount, assetIn, assetOut, pool }: { pool: V2PoolInfo; amount: number | bigint; - assetInID: number; - assetOutID: number; - decimals: { - assetIn: number; - assetOut: number; - }; + assetIn: AssetWithIdAndDecimals; + assetOut: AssetWithIdAndDecimals; }): DirectSwapQuote; -declare function getFixedOutputDirectSwapQuote({ amount, assetInID, assetOutID, decimals, pool }: { +declare function getFixedOutputDirectSwapQuote({ amount, assetIn, assetOut, pool }: { pool: V2PoolInfo | null; amount: number | bigint; - assetInID: number; - assetOutID: number; - decimals: { - assetIn: number; - assetOut: number; - }; + assetIn: AssetWithIdAndDecimals; + assetOut: AssetWithIdAndDecimals; }): SwapQuote; /** * @returns A quote for a fixed input swap. Does NOT execute any transactions. */ -declare function getFixedInputSwapQuote({ amount, assetInID, assetOutID, decimals, isSwapRouterEnabled, network, pool }: { +declare function getFixedInputSwapQuote({ amount, assetIn, assetOut, isSwapRouterEnabled, network, pool }: { amount: number | bigint; - assetInID: number; - assetOutID: number; - decimals: { - assetIn: number; - assetOut: number; - }; + assetIn: AssetWithIdAndDecimals; + assetOut: AssetWithIdAndDecimals; network: SupportedNetwork; pool: V2PoolInfo | null; isSwapRouterEnabled?: boolean; @@ -77,15 +63,11 @@ declare function getFixedInputSwapQuote({ amount, assetInID, assetOutID, decimal /** * @returns A quote for a fixed output swap. Does NOT execute any transactions. */ -declare function getFixedOutputSwapQuote({ amount, assetInID, assetOutID, decimals, isSwapRouterEnabled, network, pool }: { +declare function getFixedOutputSwapQuote({ amount, assetIn, assetOut, isSwapRouterEnabled, network, pool }: { amount: number | bigint; - assetInID: number; - assetOutID: number; + assetIn: AssetWithIdAndDecimals; + assetOut: AssetWithIdAndDecimals; pool: V2PoolInfo | null; - decimals: { - assetIn: number; - assetOut: number; - }; network: SupportedNetwork; isSwapRouterEnabled?: boolean; }): Promise; diff --git a/dist/swap/v2/router/swap-router.d.ts b/dist/swap/v2/router/swap-router.d.ts index abb8f8e7..96fddc84 100644 --- a/dist/swap/v2/router/swap-router.d.ts +++ b/dist/swap/v2/router/swap-router.d.ts @@ -1,21 +1,30 @@ -import algosdk, { Transaction } from "algosdk"; +import algosdk, { Algodv2, Transaction } from "algosdk"; import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; import { SupportedNetwork } from "../../../util/commonTypes"; import { SwapType } from "../../constants"; -import { SwapRouterResponse, GenerateSwapRouterTxnsParams } from "../../types"; -export declare function generateSwapRouterAssetOptInTransaction({ client, routerAppID, assetIDs, accountAddress }: { +import { SwapRouterResponse, SwapRoute } from "../../types"; +/** + * Generates txns that would opt in the Swap Router Application to the assets used in the swap router + */ +export declare function generateSwapRouterAssetOptInTransaction({ client, routerAppID, assetIDs, initiatorAddr }: { client: AlgodClient; routerAppID: number; assetIDs: number[]; - accountAddress: string; + initiatorAddr: string; }): Promise; -export declare function generateSwapRouterTxns({ initiatorAddr, client, network, swapType, route }: GenerateSwapRouterTxnsParams): Promise<{ +export declare function generateSwapRouterTxns({ initiatorAddr, client, network, swapType, route }: { + client: Algodv2; + initiatorAddr: string; + swapType: SwapType; + route: SwapRoute; + network: SupportedNetwork; +}): Promise<{ txn: algosdk.Transaction; signers: string[]; }[]>; -export declare function getSwapRouterAppOptInRequiredAssetIDs({ client, routerAppID, assetIDs }: { +export declare function getSwapRouterAppOptInRequiredAssetIDs({ client, network, assetIDs }: { client: AlgodClient; - routerAppID: number; + network: SupportedNetwork; assetIDs: number[]; }): Promise; export declare function getSwapRoute({ amount, assetInID, assetOutID, swapType, network }: { diff --git a/dist/util/asset/assetModels.d.ts b/dist/util/asset/assetModels.d.ts index 05703bd2..22d99fe6 100644 --- a/dist/util/asset/assetModels.d.ts +++ b/dist/util/asset/assetModels.d.ts @@ -49,6 +49,10 @@ export interface AssetWithIdAndAmount { id: number; amount: number | bigint; } +export interface AssetWithIdAndDecimals { + id: number; + decimals: number; +} export interface AssetWithAmountAndDecimals { amount: number | bigint; decimals: number; diff --git a/src/swap/types.ts b/src/swap/types.ts index 8c6ebfb9..15a493ba 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -123,17 +123,9 @@ export type GenerateV1_1SwapTxnsParams = Omit< GenerateSwapTxnsParams, "quote" | "network" > & { - quote: DirectSwapQuoteAndPool; + quoteAndPool: DirectSwapQuoteAndPool; }; -export interface GenerateSwapRouterTxnsParams { - client: Algodv2; - initiatorAddr: string; - swapType: SwapType; - route: SwapRoute; - network: SupportedNetwork; -} - /** An object containing information about a successfully executed swap. */ export interface V1SwapExecution { /** The round that the swap occurred in. */ diff --git a/src/swap/utils.ts b/src/swap/utils.ts index 7eac35ad..bc63632d 100644 --- a/src/swap/utils.ts +++ b/src/swap/utils.ts @@ -18,21 +18,13 @@ import { isSwapQuoteErrorCausedByAmount } from "./common/utils"; import {getAssetId} from "../util/asset/assetUtils"; -import {isPoolEmpty} from "../util/pool/common"; import SwapQuoteError, {SwapQuoteErrorType} from "../util/error/SwapQuoteError"; /** * Gets the best quote for swap from the pools and swap router and returns the best option. */ export function getQuote(params: GetSwapQuoteParams): Promise { - const {type, pools, isSwapRouterEnabled} = params; - - if (!isSwapRouterEnabled && pools.every((pool) => isPoolEmpty(pool.reserves))) { - throw new SwapQuoteError( - SwapQuoteErrorType.NoAvailablePoolError, - "There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools." - ); - } + const {type} = params; if (type === SwapType.FixedInput) { return getFixedInputSwapQuote(params); @@ -67,7 +59,7 @@ function validateQuotes(promises: Promise[]): Promise { const filteredResults = ( results.filter( - (result) => result.status === "fulfilled" && result.value !== undefined + (result) => result.status === "fulfilled" && result.value ) as PromiseFulfilledResult[] ).map((result) => result.value); @@ -91,17 +83,17 @@ export async function getFixedInputSwapQuote( ); if (v1_1Pool) { - const quoteGetterArgs = { - pool: v1_1Pool.info, - assetIn: {amount, id: Number(assetIn.id)}, - decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, - reserves: v1_1Pool.reserves - }; - quotePromises.push( new Promise((resolve, reject) => { try { - resolve(SwapV1_1.getFixedInputSwapQuote(quoteGetterArgs)); + resolve( + SwapV1_1.getFixedInputSwapQuote({ + pool: v1_1Pool.info, + assetIn: {amount, id: Number(assetIn.id)}, + decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, + reserves: v1_1Pool.reserves + }) + ); } catch (error) { reject(error); } @@ -123,10 +115,15 @@ export async function getFixedInputSwapQuote( quotePromises.push( SwapV2.getFixedInputSwapQuote({ amount, - assetInID: getAssetId(assetIn), - assetOutID: getAssetId(assetOut), + assetIn: { + id: getAssetId(assetIn), + decimals: assetIn.decimals + }, + assetOut: { + id: getAssetId(assetOut), + decimals: assetOut.decimals + }, pool: v2Pool?.info ?? null, - decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, isSwapRouterEnabled, network: params.network }) @@ -153,17 +150,17 @@ export async function getFixedOutputSwapQuote( ); if (v1_1Pool) { - const quoteGetterArgs = { - pool: v1_1Pool.info, - assetOut: {amount, id: Number(assetOut.id)}, - decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, - reserves: v1_1Pool.reserves - }; - quotePromises.push( new Promise((resolve, reject) => { try { - resolve(SwapV1_1.getFixedOutputSwapQuote(quoteGetterArgs)); + resolve( + SwapV1_1.getFixedOutputSwapQuote({ + pool: v1_1Pool.info, + assetOut: {amount, id: Number(assetOut.id)}, + decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, + reserves: v1_1Pool.reserves + }) + ); } catch (error) { reject(error); } @@ -185,10 +182,15 @@ export async function getFixedOutputSwapQuote( quotePromises.push( SwapV2.getFixedOutputSwapQuote({ amount, - assetInID: getAssetId(assetIn), - assetOutID: getAssetId(assetOut), + assetIn: { + id: getAssetId(assetIn), + decimals: assetIn.decimals + }, + assetOut: { + id: getAssetId(assetOut), + decimals: assetOut.decimals + }, pool: v2Pool?.info ?? null, - decimals: {assetIn: assetIn.decimals, assetOut: assetOut.decimals}, isSwapRouterEnabled, network: params.network }) @@ -206,7 +208,7 @@ export function generateTxns( params.quote.type === SwapQuoteType.Direct && getSwapQuoteContractVersion(params.quote) === CONTRACT_VERSION.V1_1 ) { - return SwapV1_1.generateTxns({...params, quote: params.quote.data}); + return SwapV1_1.generateTxns({...params, quoteAndPool: params.quote.data}); } return SwapV2.generateTxns(params); @@ -225,7 +227,7 @@ export function signTxns(params: { data: {pool} } = params.quote; - return SwapV1_1.signTxns({pool, ...params}); + return SwapV1_1.signTxns({...params, pool}); } return SwapV2.signTxns(params); diff --git a/src/swap/v1_1/index.ts b/src/swap/v1_1/index.ts index 563a78ac..d9083422 100644 --- a/src/swap/v1_1/index.ts +++ b/src/swap/v1_1/index.ts @@ -68,13 +68,13 @@ async function signTxns({ async function generateTxns({ client, - quote, + quoteAndPool, swapType, slippage, initiatorAddr }: GenerateV1_1SwapTxnsParams): Promise { - const {pool, quote: swapQuote} = quote; - const {assetInID, assetOutID} = swapQuote; + const {pool, quote} = quoteAndPool; + const {assetInID, assetOutID} = quote; const poolAddress = pool.account.address(); const poolAssets = [pool.asset1ID, pool.asset2ID]; @@ -111,8 +111,8 @@ async function generateTxns({ const assetInAmount = swapType === SwapType.FixedOutput - ? applySlippageToAmount("positive", slippage, swapQuote.assetInAmount) - : swapQuote.assetInAmount; + ? applySlippageToAmount("positive", slippage, quote.assetInAmount) + : quote.assetInAmount; let assetInTxn: algosdk.Transaction; if (assetInID === ALGO_ASSET_ID) { @@ -134,8 +134,8 @@ async function generateTxns({ const assetOutAmount = swapType === SwapType.FixedInput - ? applySlippageToAmount("negative", slippage, swapQuote.assetOutAmount) - : swapQuote.assetOutAmount; + ? applySlippageToAmount("negative", slippage, quote.assetOutAmount) + : quote.assetOutAmount; let assetOutTxn: algosdk.Transaction; if (assetOutID === ALGO_ASSET_ID) { @@ -230,7 +230,7 @@ function getFixedInputSwapQuote({ if (pool.status !== PoolStatus.READY) { throw new SwapQuoteError( SwapQuoteErrorType.NoAvailablePoolError, - "Trying to swap on a non-existent pool" + "There is not an available pool for this asset pair" ); } @@ -395,7 +395,7 @@ function getFixedOutputSwapQuote({ if (pool.status !== PoolStatus.READY) { throw new SwapQuoteError( SwapQuoteErrorType.NoAvailablePoolError, - "Trying to swap on a non-existent pool" + "There is not an available pool for this asset pair" ); } @@ -577,7 +577,7 @@ async function execute({ if (pool.status !== PoolStatus.READY) { throw new TinymanError( {pool, swapType, txGroup}, - "Trying to swap on a non-existent pool" + "There is not an available pool for this asset pair" ); } diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index e34e8659..13242d65 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -40,6 +40,7 @@ import {generateSwapRouterTxns, getSwapRoute} from "./router/swap-router"; import {poolUtils} from "../../util/pool"; import SwapQuoteError, {SwapQuoteErrorType} from "../../util/error/SwapQuoteError"; import {getSwapAppCallFeeAmount, isSwapAssetInAmountLow} from "./util"; +import {AssetWithIdAndDecimals} from "../../util/asset/assetModels"; async function generateTxns( params: GenerateSwapTxnsParams @@ -193,28 +194,27 @@ async function execute({ /** * @param type - Type of the swap * @param pool - Information for the pool. - * @param asset.assetID - ID of the asset to be swapped - * @param asset.amount - Amount of the asset to be swapped - * @param decimals.assetIn - Decimals quantity for the input asset - * @param decimals.assetOut - Decimals quantity for the output asset + * @param assetIn - Asset to be swapped + * @param assetOut - Asset to be received + * @param amount - Amount of asset to be swapped + * @param network - Network to be used + * @param isSwapRouterEnabled - Whether the swap router is enabled * @returns A promise for the Swap quote */ async function getQuote({ type, amount, - assetInID, - assetOutID, - decimals, + assetIn, + assetOut, network, isSwapRouterEnabled, pool }: { type: SwapType; amount: number | bigint; - assetInID: number; - assetOutID: number; + assetIn: AssetWithIdAndDecimals; + assetOut: AssetWithIdAndDecimals; pool: V2PoolInfo | null; - decimals: {assetIn: number; assetOut: number}; network: SupportedNetwork; isSwapRouterEnabled?: boolean; }): Promise { @@ -222,10 +222,9 @@ async function getQuote({ if (type === SwapType.FixedInput) { quote = await getFixedInputSwapQuote({ - assetInID, - assetOutID, + assetIn, + assetOut, amount, - decimals, isSwapRouterEnabled, network, pool @@ -233,9 +232,8 @@ async function getQuote({ } else { quote = await getFixedOutputSwapQuote({ amount, - assetInID, - assetOutID, - decimals, + assetIn, + assetOut, isSwapRouterEnabled, network, pool @@ -274,7 +272,7 @@ function validateQuotes(quotePromises: Promise[]): Promise result.status === "fulfilled" && result.value !== undefined + (result) => result.status === "fulfilled" && result.value ) as PromiseFulfilledResult[] ).map((result) => result.value); }); @@ -282,24 +280,27 @@ function validateQuotes(quotePromises: Promise[]): Promise outputSupply) { @@ -348,32 +352,35 @@ function getFixedInputDirectSwapQuote({ assetOutAmount: swapOutputAmount, swapFee: Number(totalFeeAmount), rate: - convertFromBaseUnits(decimals.assetOut, Number(swapOutputAmount)) / - convertFromBaseUnits(decimals.assetIn, Number(assetInAmount)), + convertFromBaseUnits(assetOutDecimals, Number(swapOutputAmount)) / + convertFromBaseUnits(assetInDecimals, Number(assetInAmount)), priceImpact }; } function getFixedOutputDirectSwapQuote({ amount, - assetInID, - assetOutID, - decimals, + assetIn, + assetOut, pool }: { pool: V2PoolInfo | null; amount: number | bigint; - assetInID: number; - assetOutID: number; - decimals: {assetIn: number; assetOut: number}; + assetIn: AssetWithIdAndDecimals; + assetOut: AssetWithIdAndDecimals; }): SwapQuote { if (!pool || !poolUtils.isPoolReady(pool)) { throw new SwapQuoteError( SwapQuoteErrorType.NoAvailablePoolError, - "There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools." + "There is not an available pool for this asset pair" ); } + const [ + {id: assetInID, decimals: assetInDecimals}, + {id: assetOutID, decimals: assetOutDecimals} + ] = [assetIn, assetOut]; + const assetOutAmount = BigInt(amount); const totalFeeShare = pool.totalFeeShare!; let inputSupply: bigint; @@ -397,7 +404,10 @@ function getFixedOutputDirectSwapQuote({ outputSupply, swapOutputAmount: assetOutAmount, totalFeeShare, - decimals + decimals: { + assetIn: assetInDecimals, + assetOut: assetOutDecimals + } }); if (assetOutAmount > outputSupply) { @@ -425,8 +435,8 @@ function getFixedOutputDirectSwapQuote({ assetOutAmount, swapFee: Number(totalFeeAmount), rate: - convertFromBaseUnits(decimals.assetOut, Number(assetOutAmount)) / - convertFromBaseUnits(decimals.assetIn, Number(swapInputAmount)), + convertFromBaseUnits(assetOutDecimals, Number(assetOutAmount)) / + convertFromBaseUnits(assetInDecimals, Number(swapInputAmount)), priceImpact } } @@ -438,17 +448,15 @@ function getFixedOutputDirectSwapQuote({ */ async function getFixedInputSwapQuote({ amount, - assetInID, - assetOutID, - decimals, + assetIn, + assetOut, isSwapRouterEnabled, network, pool }: { amount: number | bigint; - assetInID: number; - assetOutID: number; - decimals: {assetIn: number; assetOut: number}; + assetIn: AssetWithIdAndDecimals; + assetOut: AssetWithIdAndDecimals; network: SupportedNetwork; pool: V2PoolInfo | null; isSwapRouterEnabled?: boolean; @@ -461,9 +469,8 @@ async function getFixedInputSwapQuote({ try { const quote = getFixedInputDirectSwapQuote({ amount, - assetInID, - assetOutID, - decimals, + assetIn, + assetOut, pool }); @@ -484,7 +491,7 @@ async function getFixedInputSwapQuote({ Promise.reject( new SwapQuoteError( SwapQuoteErrorType.NoAvailablePoolError, - "There is not an available pool for this swap. However, you can enable swap router and try to perform this swap with multiple pools." + "There is not an available pool for this asset pair" ) ) ); @@ -492,27 +499,13 @@ async function getFixedInputSwapQuote({ if (isSwapRouterEnabled) { quotePromises.push( - (async () => { - const data = await getSwapRoute({ - amount, - assetInID, - assetOutID, - swapType: SwapType.FixedInput, - network - }); - - if (data.route.length < 2) { - throw new SwapQuoteError( - SwapQuoteErrorType.SwapRouterNoRouteError, - "Swap router couldn't find a route for this swap." - ); - } - - return { - type: SwapQuoteType.Router, - data - }; - })() + getSwapRoute({ + amount, + assetInID: assetIn.id, + assetOutID: assetOut.id, + swapType: SwapType.FixedInput, + network + }).then((data) => ({type: SwapQuoteType.Router, data})) ); } @@ -526,18 +519,16 @@ async function getFixedInputSwapQuote({ */ async function getFixedOutputSwapQuote({ amount, - assetInID, - assetOutID, - decimals, + assetIn, + assetOut, isSwapRouterEnabled, network, pool }: { amount: number | bigint; - assetInID: number; - assetOutID: number; + assetIn: AssetWithIdAndDecimals; + assetOut: AssetWithIdAndDecimals; pool: V2PoolInfo | null; - decimals: {assetIn: number; assetOut: number}; network: SupportedNetwork; isSwapRouterEnabled?: boolean; }): Promise { @@ -547,9 +538,8 @@ async function getFixedOutputSwapQuote({ resolve( getFixedOutputDirectSwapQuote({ amount, - assetInID, - assetOutID, - decimals, + assetIn, + assetOut, pool }) ); @@ -561,20 +551,13 @@ async function getFixedOutputSwapQuote({ if (isSwapRouterEnabled) { quotePromises.push( - (async () => { - const data = await getSwapRoute({ - amount, - assetInID, - assetOutID, - swapType: SwapType.FixedOutput, - network - }); - - return { - type: SwapQuoteType.Router, - data - }; - })() + getSwapRoute({ + amount, + assetInID: assetIn.id, + assetOutID: assetOut.id, + swapType: SwapType.FixedOutput, + network + }).then((data) => ({type: SwapQuoteType.Router, data})) ); } diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index 826b9c05..3e95bfe0 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -1,7 +1,13 @@ -import algosdk, {ALGORAND_MIN_TX_FEE, getApplicationAddress, Transaction} from "algosdk"; +import algosdk, { + Algodv2, + ALGORAND_MIN_TX_FEE, + getApplicationAddress, + Transaction +} from "algosdk"; import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; import {CONTRACT_VERSION} from "../../../contract/constants"; +import {AccountInformation} from "../../../util/account/accountTypes"; import {ALGO_ASSET_ID} from "../../../util/asset/assetConstants"; import {getAssetId, isAlgo} from "../../../util/asset/assetUtils"; import {SupportedNetwork} from "../../../util/commonTypes"; @@ -9,11 +15,7 @@ import SwapQuoteError, {SwapQuoteErrorType} from "../../../util/error/SwapQuoteE import {hasTinymanApiErrorShape} from "../../../util/util"; import {getValidatorAppID} from "../../../validator"; import {SwapType} from "../../constants"; -import { - FetchSwapRouteQuotesPayload, - SwapRouterResponse, - GenerateSwapRouterTxnsParams -} from "../../types"; +import {FetchSwapRouteQuotesPayload, SwapRouterResponse, SwapRoute} from "../../types"; import { V2_SWAP_APP_CALL_ARG_ENCODED, V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED, @@ -26,21 +28,24 @@ import { getSwapRouterAppID } from "./util"; +/** + * Generates txns that would opt in the Swap Router Application to the assets used in the swap router + */ export async function generateSwapRouterAssetOptInTransaction({ client, routerAppID, assetIDs, - accountAddress + initiatorAddr }: { client: AlgodClient; routerAppID: number; assetIDs: number[]; - accountAddress: string; + initiatorAddr: string; }): Promise { const suggestedParams = await client.getTransactionParams().do(); // We need to create a NoOp transaction to opt-in to the assets const assetOptInTxn = algosdk.makeApplicationNoOpTxnFromObject({ - from: accountAddress, + from: initiatorAddr, appIndex: routerAppID, appArgs: [V2_SWAP_ROUTER_APP_ARGS_ENCODED.ASSET_OPT_IN], foreignAssets: assetIDs, @@ -64,7 +69,13 @@ export async function generateSwapRouterTxns({ network, swapType, route -}: GenerateSwapRouterTxnsParams) { +}: { + client: Algodv2; + initiatorAddr: string; + swapType: SwapType; + route: SwapRoute; + network: SupportedNetwork; +}) { const suggestedParams = await client.getTransactionParams().do(); const [assetInID, intermediaryAssetID, assetOutID] = [ @@ -118,14 +129,14 @@ export async function generateSwapRouterTxns({ const optInRequiredAssetIDs = await getSwapRouterAppOptInRequiredAssetIDs({ client, - routerAppID, + network, assetIDs: [assetInID, intermediaryAssetID, assetOutID] }); if (optInRequiredAssetIDs.length > 0) { const routerAppAssetOptInTxn = await generateSwapRouterAssetOptInTransaction({ client, - accountAddress: initiatorAddr, + initiatorAddr, assetIDs: optInRequiredAssetIDs, routerAppID }); @@ -143,24 +154,23 @@ export async function generateSwapRouterTxns({ export async function getSwapRouterAppOptInRequiredAssetIDs({ client, - routerAppID, + network, assetIDs }: { client: AlgodClient; - routerAppID: number; + network: SupportedNetwork; assetIDs: number[]; }) { - const swapRouterAppAddress = getApplicationAddress(routerAppID); - const accountInfo = await client.accountInformation(swapRouterAppAddress).do(); - const appOptedInAssetIDs = accountInfo.assets.map( - (asset: {"asset-id": number}) => asset["asset-id"] - ); - const requiredAssetIDs = assetIDs.filter( + const swapRouterAppAddress = getApplicationAddress(getSwapRouterAppID(network)); + const accountInfo = (await client + .accountInformation(swapRouterAppAddress) + .do()) as AccountInformation; + const appOptedInAssetIDs = accountInfo.assets.map((asset) => asset["asset-id"]); + + return assetIDs.filter( (assetID: number) => assetID !== ALGO_ASSET_ID && !appOptedInAssetIDs.includes(assetID) ); - - return requiredAssetIDs; } export async function getSwapRoute({ @@ -205,9 +215,19 @@ export async function getSwapRoute({ serializedResponse.fallback_message ); } else { - throw new SwapQuoteError(SwapQuoteErrorType.UnknownError, "Unknown error"); + throw new SwapQuoteError( + SwapQuoteErrorType.UnknownError, + "There was an error while getting a quote from Swap Router" + ); } } + if ((serializedResponse as SwapRouterResponse).route.length < 2) { + throw new SwapQuoteError( + SwapQuoteErrorType.SwapRouterNoRouteError, + "Swap router couldn't find a route for this swap." + ); + } + return serializedResponse; } diff --git a/src/swap/v2/router/util.ts b/src/swap/v2/router/util.ts index c4dcb8f4..a8c10b10 100644 --- a/src/swap/v2/router/util.ts +++ b/src/swap/v2/router/util.ts @@ -19,7 +19,7 @@ function getSwapRouterAppID(network: SupportedNetwork) { throw new Error(`Unknown network or network not supported: ${network}`); } - return SWAP_ROUTER_APP_ID[network]; + return id; } function getAssetOutFromSwapRoute(route: SwapRoute) { diff --git a/src/util/asset/assetModels.ts b/src/util/asset/assetModels.ts index 385836a5..27831f1c 100644 --- a/src/util/asset/assetModels.ts +++ b/src/util/asset/assetModels.ts @@ -54,6 +54,11 @@ export interface AssetWithIdAndAmount { amount: number | bigint; } +export interface AssetWithIdAndDecimals { + id: number; + decimals: number; +} + export interface AssetWithAmountAndDecimals { amount: number | bigint; decimals: number; From 99ea62ecaf6943e6b75282d33de0f6ec94427564 Mon Sep 17 00:00:00 2001 From: edizcelik Date: Thu, 23 Mar 2023 16:45:22 -0700 Subject: [PATCH 28/39] Build lib --- dist/index.js | 6 +----- dist/swap/v2/router/swap-router.d.ts | 5 ++--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/dist/index.js b/dist/index.js index fd589b39..cb225d4a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,7 +1,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("algosdk"),e=require("base64-js");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r,a=n(t);function o(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function s(t){for(var e=1;e=0;--a){var o=this.tryEntries[a],s=o.completion;if("root"===o.tryLoc)return r("end");if(o.tryLoc<=this.prev){var u=n.call(o,"catchLoc"),i=n.call(o,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),w(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;w(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:S(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,o,s){try{var u=t[o](s),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function s(t){i(o,r,a,s,u,"next",t)}function u(t){i(o,r,a,s,u,"throw",t)}s(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,s=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){u=!0,o=t},f:function(){try{s||null==n.return||n.return()}finally{if(u)throw o}}}}exports.SwapQuoteErrorType=void 0,(r=exports.SwapQuoteErrorType||(exports.SwapQuoteErrorType={})).SwapRouterStaleDataError="SwapRouterStaleDataError",r.SwapRouterNoRouteError="SwapRouterNoRouteError",r.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",r.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",r.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",r.NoAvailablePoolError="NoAvailablePoolError",r.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",r.UnknownError="UnknownError",r.LowSwapAmountError="LowSwapAmountError",r.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",r.InvalidSwapTypeError="InvalidSwapTypeError";var D=function(t){m(n,h(Error));var e=v(n);function n(t,r){var a;return p(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(T(a),n),a}return d(n)}(),O=Uint8Array.from([1]),N={V1_1:"v1_1",V2:"v2"},P={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151"},k={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},L="- would result negative",R="logic eval error:",F="exceeds schema integer count",B=/transaction \w+:/,M=function(t){m(n,h(Error));var e=v(n);function n(t,r){var a;p(this,n);for(var o=arguments.length,s=new Array(o>2?o-2:0),u=2;ur?[s(s({},t),{},{id:n}),s(s({},e),{},{id:r})]:[s(s({},e),{},{id:r}),s(s({},t),{},{id:n})]}function C(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function j(t){return 0===Number(t)}function X(t){return Number(t.id)}function q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,o=void 0!==a&&a,s={},u=b(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}s[o?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return s}function U(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,o=b(t);try{for(o.s();!(e=o.n()).done;){var s=e.value;r.set(s,a),a+=s.length}}catch(t){o.e(t)}finally{o.f()}return r}var G=100000n,W=100000n,z=100000n,J=25000n+25000n,Y=25000n+3500n;function K(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],o=t["created-apps"]||[],s=t.assets||[];return G+W*BigInt(s.length)+z*BigInt(o.length+a.length)+Y*r+J*n}function H(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,H(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function tt(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function et(t,e){var n=Number(t);return rt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function nt(t,e){return rt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function rt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=w(ot(e),2),o=a[0],s=a[1],u=w(ot(Math.round(Number(at(o,s+r)))),2),i=u[0],c=u[1];return Number(at(i,c-r))}return Math.round(e)}function at(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ot(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function st(t,e){return ut.apply(this,arguments)}function ut(){return(ut=c(u().mark((function t(e,n){var r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=b(n),t.prev=3,a.s();case 5:if((o=a.n()).done){t.next=18;break}return s=o.value,t.next=9,e.sendRawTransaction(s).do();case 9:return i=t.sent,c=i.txId,t.next=13,Z(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new M(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function it(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ct(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function pt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function lt(t){return(new TextEncoder).encode(t)}function dt(t){return Boolean(t)&&void 0!==t.fallback_message}var ft,mt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return lt("tinyman/".concat(t===N.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),gt=(f(ft={},N.V1_1,{testnet:62368684,mainnet:552635992}),f(ft,N.V2,{testnet:148607e3,mainnet:1002541853}),ft);function xt(t,e){var n=gt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function At(){return(At=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationOptInTxnFromObject({from:s,appIndex:xt(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var It;function ht(){return(ht=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationClearStateTxnFromObject({from:s,appIndex:xt(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(It=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",It.FixedOutput="fixed-output";var Tt,yt,vt;!function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(vt||(vt={}));var wt,_t,St=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),Et=lt("swap"),bt=(f(yt={},exports.SwapType.FixedInput,lt("fixed-input")),f(yt,exports.SwapType.FixedOutput,lt("fixed-output")),yt),Dt={ASSET_OPT_IN:lt("asset_opt_in")},Ot={testnet:159521633,mainnet:0},Nt=(f(wt={},exports.SwapType.FixedInput,7),f(wt,exports.SwapType.FixedOutput,8),wt),Pt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function kt(t){var e=Bt(t),n=e.assetIn,r=e.assetOut;return et(r.asset.decimals,Number(r.amount))/et(n.asset.decimals,Number(n.amount))}function Lt(t){var e=Ot[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Rt(t){return t[t.length-1].quote.amount_out}function Ft(t){return t[0].quote.amount_in}function Bt(t){return{assetIn:Ft(t),assetOut:Rt(t)}}function Mt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function e(n){var r,o,s,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.routerAppID,s=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o,appArgs:[Dt.ASSET_OPT_IN],foreignAssets:s,suggestedParams:c}),l=s.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,w,_,S,E,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,o=n.client,s=n.network,i=n.swapType,c=n.route,e.next=3,o.getTransactionParams().do();case 3:return p=e.sent,l=[X(c[0].quote.amount_in.asset),X(c[0].quote.amount_out.asset),X(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Ft(c).amount),Number(Rt(c).amount)],x=g[0],A=g[1],I=[c[0].pool.address,c[1].pool.address],h=I[0],T=I[1],y=j(d),v=Lt(s),w=y?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(_=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:v,appArgs:[Et,bt[i],a.default.encodeUint64(A)],foreignApps:[xt(s,N.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Nt[i]+1),S=[w,_],e.next=15,jt({client:o,network:s,assetIDs:[d,f,m]});case 15:if(!((E=e.sent).length>0)){e.next=21;break}return e.next=19,Mt({client:o,initiatorAddr:r,assetIDs:E,routerAppID:v});case 19:b=e.sent,S.unshift(b);case 21:return D=a.default.assignGroupID(S),e.abrupt("return",D.map((function(t){return{txn:t,signers:[r]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(u().mark((function e(n){var r,a,o,s,i,c;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,s=t.getApplicationAddress(Lt(a)),e.next=4,r.accountInformation(s).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return 0!==t&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qt(t){return Ut.apply(this,arguments)}function Ut(){return(Ut=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.swapType,s=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Pt[s].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(!dt(p)){t.next=13;break}throw new D(p.type,p.fallback_message);case 13:throw new D(exports.SwapQuoteErrorType.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new D(exports.SwapQuoteErrorType.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapQuoteType=void 0,(_t=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",_t.Router="router";var Gt=4*t.ALGORAND_MIN_TX_FEE;function Wt(e){return(St[e]+2)*t.ALGORAND_MIN_TX_FEE}function zt(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:.003;return Math.ceil(1/t)}function Jt(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:.003;return tn&&(e=a,n=o)}return e}function re(t){return t instanceof D&&[exports.SwapQuoteErrorType.SwapRouterInsufficientReservesError,exports.SwapQuoteErrorType.SwapRouterLowSwapAmountError,exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,exports.SwapQuoteErrorType.LowSwapAmountError].includes(t.type)}var ae={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},oe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},se=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ue=new(function(n){m(a,se);var r=v(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=xt(n.network,N.V1_1),a=w(C(n.asset1ID,n.asset2ID),2),o=a[0],s=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:o,asset_id_2:s,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(s(s({},i),{},{minimum_required_balance:me(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),o(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function fe(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function me(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ge,xe,Ae=lt("e");function Ie(t){return he.apply(this,arguments)}function he(){return(he=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,w,_,S,E;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.pool,i=r.accountAddr,n.next=3,o.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=s.account.address(),g=b(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((A=x.value).id==s.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=q({stateArray:I}),T=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ae,a.default.encodeUint64(s.asset1ID)])),y=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ae,a.default.encodeUint64(s.asset2ID)])),v=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ae,a.default.encodeUint64(s.poolTokenID)])),w=h[T],_=h[y],S=h[v],"bigint"==typeof w&&(l=w),"bigint"==typeof _&&(d=_),"bigint"==typeof S&&(f=S);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((E={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Te(){return(Te=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,x,A,I,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.accountAddr,i=r.validatorAppID,n.next=3,o.accountInformation(s).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mwe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(we,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function be(){return be=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,de(n,r);case 6:return s=t.sent,i=fe(s,xt(a,N.V1_1)),c=null,i&&(l=s["created-assets"][0],p=l.index,c={asset1ID:i[ye[N.V1_1].asset1],asset2ID:i[ye[N.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),be.apply(this,arguments)}var De=Object.freeze({__proto__:null,getPoolInfo:_e,getPoolReserves:function(t,e){return Ee.apply(this,arguments)},getPoolAssets:function(t){return be.apply(this,arguments)}});function Oe(t){return Ne.apply(this,arguments)}function Ne(){return(Ne=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,o=e.asset2ID,s=le(N.V2),i=s.generateLogicSigAccountForPool(e),c=xt(r,N.V2),p=i.address(),l=C(a,o),t.next=8,de(n,p);case 8:return d=t.sent,f=fe(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:N.V2},f&&(m.asset1ProtocolFees=BigInt(f[ye.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ye.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ye.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ye.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ye.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ye.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ye.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ye.v2.totalFeeShare]),m.poolTokenID=Number(f[ye.v2.poolTokenID]),m.asset1ID=Number(f[ye.v2.asset1]),m.asset2ID=Number(f[ye.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pe(){return(Pe=c(u().mark((function t(e,n){var r,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,de(e,n.account.address());case 2:return r=t.sent,a=fe(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[ye.v2.asset1Reserves]),o.asset2=BigInt(a[ye.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[ye.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ke(){return(ke=c(u().mark((function t(e){var n,r,a,o,s,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,de(n,r);case 3:return o=t.sent,s=fe(o,xt(a,N.V2)),i=null,s&&(i={asset1ID:s[ye[N.V2].asset1],asset2ID:s[ye[N.V2].asset2],poolTokenID:s[ye[N.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Le=Object.freeze({__proto__:null,getPoolInfo:Oe,getPoolReserves:function(t,e){return Pe.apply(this,arguments)},getPoolAssets:function(t){return ke.apply(this,arguments)}});function Re(t){return Boolean(t&&!(t.asset1+t.asset2))}var Fe,Be,Me=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Re(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=et(t.asset1,e.asset1)/et(t.asset2,e.asset2)),r},isPoolEmpty:Re,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([_e(t),Oe(t)])}}),Qe=s((f(Fe={},N.V1_1,s(s({},De),Me)),f(Fe,N.V2,s(s({},Le),Me)),Fe),Me);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Be||(Be={}));var Ve=96e4,Ce=859e3;function je(){return(je=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,_,S,E,b,D,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.asset_1,s=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=V(o,s),l=w(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,A=g.unit_name,I=j(x),h=xt(r,N.V1_1),T=ue.generateLogicSigAccountForPool({network:r,asset1ID:f,asset2ID:x}),y=T.address(),v=a.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:mt.getAppCallTxnNoteWithClientName(N.V1_1),appArgs:[lt("bootstrap"),a.default.encodeUint64(f),a.default.encodeUint64(x)],foreignAssets:I?[f]:[x],suggestedParams:c}),_=a.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:k.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(A),assetURL:"https://tinyman.org",suggestedParams:c}),S=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Xe(I),suggestedParams:c}),(b=[])[Be.FUNDING_TXN]=E,b[Be.VALIDATOR_APP_CALL]=v,b[Be.POOL_TOKEN_CREATE]=_,b[Be.ASSET1_OPT_IN]=S,I||(b[Be.ASSET2_OPT_IN]=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),D=a.default.assignGroupID(b),O=[{txn:D[Be.FUNDING_TXN],signers:[i]},{txn:D[Be.VALIDATOR_APP_CALL],signers:[y]},{txn:D[Be.POOL_TOKEN_CREATE],signers:[y]},{txn:D[Be.ASSET1_OPT_IN],signers:[y]}],D[Be.ASSET2_OPT_IN]&&O.push({txn:D[Be.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(t){return t?Ve:Ce}function qe(){return(qe=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=C(s,i),f=w(d,2),m=f[0],g=f[1],x=ue.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),A=[],I=n.map((function(t,e){if(e===Be.FUNDING_TXN)return A.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,x),r=n.txID,o=n.blob;return A.push(r),o})),t.abrupt("return",{signedTxns:I,txnIDs:A});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ue(t){return Ge.apply(this,arguments)}function Ge(){return(Ge=c(u().mark((function t(e){var n,r,a,o,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Z(n,a[Be.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(s=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(s));case 10:return t.abrupt("return",{poolTokenID:s});case 13:throw t.prev=13,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function We(){return(We=c(u().mark((function t(e){var n,r,a,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Ue({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",Qe.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ze,Je={generateTxns:function(t){return je.apply(this,arguments)},signTxns:function(t){return qe.apply(this,arguments)},execute:function(t){return We.apply(this,arguments)},getBootstrapFundingTxnAmount:Xe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(ze||(ze={}));var Ye=5,Ke=6;function He(t,e){return Ze.apply(this,arguments)}function Ze(){return(Ze=c(u().mark((function e(n,r){var a,o,s;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return s=e.t0,e.abrupt("return",s);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function $e(t,e){return tn.apply(this,arguments)}function tn(){return(tn=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,He(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(){return(en=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,_,S;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.network,s=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return p=e.sent,l=xt(o,N.V2),d=t.getApplicationAddress(l),f=V(s,i),m=w(f,2),g=m[0].id,x=m[1].id,e.next=9,Qe.v2.getPoolInfo({client:r,network:o,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(s.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=pe.generateLogicSigAccountForPool({network:o,asset1ID:g,asset2ID:x}),I=A.address(),h=j(x),(T=a.default.makeApplicationOptInTxnFromObject({from:I,appIndex:l,appArgs:[lt("bootstrap")],note:mt.getAppCallTxnNoteWithClientName(N.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=rn(h),y=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:I,amount:nn(h),suggestedParams:p}),(v=[])[ze.FUNDING_TXN]=y,v[ze.VALIDATOR_APP_CALL]=T,_=a.default.assignGroupID(v),(S=[])[ze.FUNDING_TXN]={txn:_[ze.FUNDING_TXN],signers:[c]},S[ze.VALIDATOR_APP_CALL]={txn:_[ze.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",S);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function nn(t){return function(t,e){var n=le(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(N.V2,t)+rn(t)+1e5}function rn(e){return((e?Ye:Ke)+1)*t.ALGORAND_MIN_TX_FEE}function an(){return(an=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=C(s,i),f=w(d,2),m=f[0],g=f[1],x=pe.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),A=[],I=n.map((function(t,e){if(e===ze.FUNDING_TXN)return A.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,x),r=n.txID,o=n.blob;return A.push(r),o})),t.abrupt("return",{signedTxns:I,txnIDs:A});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function on(){return(on=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,He(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ye.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Qe.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 21:throw t.prev=21,t.t3=t.catch(1),new M(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var sn,un={generateTxns:function(t){return en.apply(this,arguments)},signTxns:function(t){return an.apply(this,arguments)},execute:function(t){return on.apply(this,arguments)},getBootstrapFundingTxnAmount:nn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+nn(e)}};var cn,pn=(f(sn={},N.V1_1,Je),f(sn,N.V2,un),f(sn,"generateTxns",(function(t){return t.contractVersion===N.V1_1?Je.generateTxns(t):un.generateTxns(t)})),f(sn,"signTxns",(function(t){return t.contractVersion===N.V1_1?Je.signTxns(t):un.signTxns(t)})),f(sn,"execute",(function(t){return t.contractVersion===N.V1_1?Je.execute(t):un.execute(t)})),f(sn,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===N.V1_1?Je.getBootstrapFundingTxnAmount(n):un.getBootstrapFundingTxnAmount(n)})),sn);exports.V1_1AddLiquidityTxnIndices=void 0,(cn=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[cn.FEE_TXN=0]="FEE_TXN",cn[cn.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",cn[cn.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",cn[cn.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",cn[cn.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var ln,dn,fn,mn,gn=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(mn=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",mn.FLEXIBLE="flexible",mn.INITIAL="initial";var xn,An=(f(ln={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(ln,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(ln,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),ln),In=(f(dn={},exports.V2AddLiquidityType.INITIAL,1),f(dn,exports.V2AddLiquidityType.SINGLE,2),f(dn,exports.V2AddLiquidityType.FLEXIBLE,2),dn),hn=(f(fn={},exports.V2AddLiquidityType.INITIAL,3),f(fn,exports.V2AddLiquidityType.FLEXIBLE,3),f(fn,exports.V2AddLiquidityType.SINGLE,2),fn);function Tn(t){var e,n,r,a=t.reserves,o=t.totalFeeShare,s=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(s.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(s.amount)-m,A=BigInt(u.amount)-g;if(x>A){var I=x;r=yn(I,o),e={id:s.id,amount:I+r,decimals:s.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=A;r=yn(h,o),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:s.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:s.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Ht({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function yn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function vn(e){return(In[e]+1)*t.ALGORAND_MIN_TX_FEE}var wn=lt("add_liquidity"),_n=(f(xn={},N.V1_1,[lt("mint")]),f(xn,N.V2,{INITIAL_LIQUIDITY:[lt("add_initial_liquidity")],SINGLE_ASSET_MODE:[wn,lt("single")],FLEXIBLE_MODE:[wn,lt("flexible")]}),xn);function Sn(){return(Sn=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.poolAddress,s=e.asset1In,i=e.asset2In,c=e.poolTokenOut,p=e.slippage,l=e.initiatorAddr,d=tt("negative",p,c.amount),t.next=4,n.getTransactionParams().do();case 4:return f=t.sent,m=a.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:xt(r,N.V1_1),appArgs:_n.v1_1,accounts:[l],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==i.id?[s.id,c.id]:[s.id,i.id,c.id],suggestedParams:f}),g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:s.id,amount:s.amount,suggestedParams:f}),x=0===i.id?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:i.amount,suggestedParams:f}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),I=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+A.fee,note:O,suggestedParams:f}),h=a.default.assignGroupID([I,m,g,x,A]),t.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,s=n.account,t.next=4,o([r]);case 4:return i=t.sent,c=w(i,3),p=c[0],l=c[1],d=c[2],f=r.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:a.default.signLogicSigTransactionObject(t.txn,s).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function bn(){return(bn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,A,I,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.signedTxns,s=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Ie({client:n,pool:r,accountAddr:s});case 5:return c=t.sent,t.next=8,st(n,[o]);case 8:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=it(a),x=ct(a),t.next=17,Ie({client:n,pool:r,accountAddr:s});case 17:return A=t.sent,(I=A.excessPoolTokens-c.excessPoolTokens)<0n&&(I=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+I,excessAmount:{excessAmountForAddingLiquidity:I,totalExcessAmount:A.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new M(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Dn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var o=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(o<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(o,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:o-BigInt(1e3),share:1}}var s=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=ss)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Jt(Number(e)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");return{assetInID:u,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:et(p,Number(m))/et(i,Number(l)),priceImpact:x}}function $n(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!Qe.isPoolReady(a))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,s,u=n.id,i=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)o=a.asset2Reserves,s=a.asset1Reserves;else{if(c!==a.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));o=a.asset1Reserves,s=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,o=t.decimals,s=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:s,totalFeeShare:a}),i=s+u,c=Ht({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:o.assetIn},assetOut:{amount:r,decimals:o.assetOut}});return{swapInputAmount:i,totalFeeAmount:u,priceImpact:c}}({inputSupply:o,outputSupply:s,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:i,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact;if(l>s)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Jt(Number(m)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");return{type:exports.SwapQuoteType.Direct,data:{pool:a,quote:{assetInID:u,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:et(p,Number(l))/et(i,Number(m)),priceImpact:x}}}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,o=e.isSwapRouterEnabled,s=e.network,i=e.pool,c=[],i?c.push(new Promise((function(t,e){try{var o=Zn({amount:n,assetIn:r,assetOut:a,pool:i});t({type:exports.SwapQuoteType.Direct,data:{pool:i,quote:o}})}catch(t){e(t)}}))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(qt({amount:n,assetInID:r.id,assetOutID:a.id,swapType:exports.SwapType.FixedInput,network:s}).then((function(t){return{type:exports.SwapQuoteType.Router,data:t}}))),t.next=6,Hn(c);case 6:return p=t.sent,t.abrupt("return",ne(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(t){return rr.apply(this,arguments)}function rr(){return(rr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,o=e.isSwapRouterEnabled,s=e.network,i=e.pool,c=[new Promise((function(t,e){try{t($n({amount:n,assetIn:r,assetOut:a,pool:i}))}catch(t){e(t)}}))],o&&c.push(qt({amount:n,assetInID:r.id,assetOutID:a.id,swapType:exports.SwapType.FixedOutput,network:s}).then((function(t){return{type:exports.SwapQuoteType.Router,data:t}}))),t.next=5,Hn(c);case 5:return p=t.sent,t.abrupt("return",ne(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,o=t.decimals,s=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-s});return{swapOutputAmount:u,totalFeeAmount:s,priceImpact:Ht({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:o.assetIn},assetOut:{amount:u,decimals:o.assetOut}})}}!function(t){t[t.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(Xn||(Xn={}));var or={getQuote:function(t){return Kn.apply(this,arguments)},getFixedInputSwapQuote:tr,getFixedInputDirectSwapQuote:Zn,getFixedOutputDirectSwapQuote:$n,getFixedOutputSwapQuote:nr,generateTxns:function(t){return Jn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Yn.apply(this,arguments)},calculateFixedInputSwap:ar};function sr(t,e){var n,r,a=BigInt(t),o=e.issuedLiquidity;return o>a+BigInt(1e3)?(n=a*e.asset1/o,r=a*e.asset2/o):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function ur(){return(ur=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.account.address(),m=o.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:s,suggestedParams:d}),(x=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Un,a.default.encodeUint64(tt("negative",l,c)),a.default.encodeUint64(tt("negative",l,p))],accounts:[f],foreignAssets:[o.asset1ID,o.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(A=[])[Xn.ASSET_TRANSFER_TXN]=g,A[Xn.APP_CALL_TXN]=x,I=a.default.assignGroupID(A),e.abrupt("return",[{txn:I[Xn.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[Xn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ir(){return(ir=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,w;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.asset1ID,m=o.asset2ID,g=o.account.address(),x=o.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,h=tt("negative",l,p),c!==f){e.next=17;break}A=h,I=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}A=0,I=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=a.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Un,a.default.encodeUint64(A),a.default.encodeUint64(I)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[Xn.ASSET_TRANSFER_TXN]=T,v[Xn.APP_CALL_TXN]=y,w=a.default.assignGroupID(v),e.abrupt("return",[{txn:w[Xn.ASSET_TRANSFER_TXN],signers:[s]},{txn:w[Xn.APP_CALL_TXN],signers:[s]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function cr(){return(cr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,o=e.signedTxns,t.next=3,st(r,[o]);case 3:return s=t.sent,i=w(s,1),c=i[0].txnID,t.next=8,$e(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pr,lr,dr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=sr(a,n),s=o.asset1OutputAmount,u=o.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:s},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,o=t.assetOutID,s=t.decimals,u=BigInt(a),i=sr(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(o===n.asset1ID){var d=or.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:s}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(o!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=or.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:s}),A=x.swapOutputAmount,I=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:p+A},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:A},swapFees:{assetId:n.asset2ID,amount:I},priceImpact:h}}}return e},generateTxns:function(t){return ur.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return ir.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return cr.apply(this,arguments)}},fr=(f(pr={},N.V1_1,{generateTxns:function(t){return Gn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,s=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:e.poolTokenID,poolTokenIn:a,asset1ID:e.asset1ID,asset1Out:o,asset2ID:e.asset2ID,asset2Out:s}},signTxns:function(t){return Wn.apply(this,arguments)},execute:function(t){return zn.apply(this,arguments)}}),f(pr,N.V2,dr),pr),mr=3n,gr=1000n;function xr(){return(xr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,t.next=3,o([r]);case 3:return s=t.sent,i=w(s,2),c=i[0],p=i[1],l=r.map((function(t,e){return e===lr.FEE_TXN_INDEX?c:e===lr.ASSET_IN_TXN_INDEX?p:a.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(){return(Ar=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.quoteAndPool,o=e.swapType,s=e.slippage,i=e.initiatorAddr,c=r.pool,p=r.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new M({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return g=t.sent,x=[lt("swap"),o===exports.SwapType.FixedInput?lt("fi"):lt("fo")],A=a.default.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:x,accounts:[i],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==c.asset2ID?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),I=o===exports.SwapType.FixedOutput?tt("positive",s,p.assetInAmount):p.assetInAmount,h=0===l?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:f,amount:I,suggestedParams:g}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:l,amount:I,suggestedParams:g}),T=o===exports.SwapType.FixedInput?tt("negative",s,p.assetOutAmount):p.assetOutAmount,y=0===d?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:i,amount:T,suggestedParams:g}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:i,assetIndex:d,amount:T,suggestedParams:g}),v=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:f,amount:A.fee+y.fee,note:O,suggestedParams:g}),w=a.default.assignGroupID([v,A,h,y]),t.abrupt("return",[{txn:w[0],signers:[i]},{txn:w[1],signers:[f]},{txn:w[2],signers:[i]},{txn:w[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ir(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*mr/gr,l=i-u*i/(u+(c-p));if(l>i)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Kt(d),priceImpact:Ht(s({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function hr(t){return Tr.apply(this,arguments)}function Tr(){return(Tr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,Ie({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ie({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,A=g.excessAsset1):(x=c.excessAsset2,A=g.excessAsset2),(I=A-x)<0n&&(I=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:s.id,assetOutAmount:BigInt(s.amount)+I,excessAmount:{assetID:s.id,excessAmountForSwap:I,totalExcessAmount:A},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,s,u,i=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,s=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,s=n.asset1,u=n.asset2}if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=s*u/(u-i)-s,p=c*gr/(gr-mr),l=p-c,d=et(a.assetOut,Number(i))/et(a.assetIn,Number(p)),f=et(a.assetOut,Number(u))/et(a.assetIn,Number(s)),m=rt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:o,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function vr(t){return wr.apply(this,arguments)}function wr(){return(wr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,Ie({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ie({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,A=g.excessAsset1):(x=c.excessAsset2,A=g.excessAsset2),(I=A-x)<0n&&(I=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-I,assetOutID:s.id,assetOutAmount:BigInt(s.amount),excessAmount:{assetID:o.id,excessAmountForSwap:I,totalExcessAmount:A},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _r(){return(_r=c(u().mark((function t(e){var n,r,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,o=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new M({pool:r,swapType:a,txGroup:o},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:o[lr.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:o[lr.ASSET_IN_TXN_INDEX].txn.amount},l={id:o[lr.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:o[lr.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,hr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,vr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",s(s({},d),{},{groupID:ct(o),fees:it(o)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new M(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(lr||(lr={}));var Sr,Er={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Ir({pool:e,reserves:n,assetIn:r,decimals:a}):yr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Ir,getFixedOutputSwapQuote:yr,generateTxns:function(t){return Ar.apply(this,arguments)},signTxns:function(t){return xr.apply(this,arguments)},execute:function(t){return _r.apply(this,arguments)},executeFixedOutputSwap:vr};function br(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=w(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(re(n)&&!re(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Dr(){return(Dr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,a=e.assetIn,o=e.assetOut,s=e.isSwapRouterEnabled,i=e.pools,c=[],(p=i.find((function(t){return t.info.contractVersion===N.V1_1})))?c.push(new Promise((function(t,e){try{t(Er.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===N.V2})),c.push(or.getFixedInputSwapQuote({amount:r,assetIn:{id:X(a),decimals:a.decimals},assetOut:{id:X(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:s,network:e.network})),t.next=8,br(c);case 8:return d=t.sent,t.abrupt("return",ne(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,a=e.assetIn,o=e.assetOut,s=e.pools,i=e.isSwapRouterEnabled,c=[],(p=s.find((function(t){return t.info.contractVersion===N.V1_1})))?c.push(new Promise((function(t,e){try{t(Er.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=s.find((function(t){return t.info.contractVersion===N.V2})),c.push(or.getFixedOutputSwapQuote({amount:r,assetIn:{id:X(a),decimals:a.decimals},assetOut:{id:X(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,br(c);case 8:return d=t.sent,t.abrupt("return",ne(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Nr=(f(Sr={},N.V1_1,Er),f(Sr,N.V2,or),f(Sr,"getQuote",(function(t){var e=t.type;if(e===exports.SwapType.FixedInput)return function(t){return Dr.apply(this,arguments)}(t);if(e===exports.SwapType.FixedOutput)return function(t){return Or.apply(this,arguments)}(t);throw new D(exports.SwapQuoteErrorType.InvalidSwapTypeError,"Invalid swap type")})),f(Sr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&te(t.quote)===N.V1_1?Er.generateTxns(s(s({},t),{},{quoteAndPool:t.quote.data})):or.generateTxns(t)})),f(Sr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&te(t.quote)===N.V1_1){var e=t.quote.data.pool;return Er.signTxns(s(s({},t),{},{pool:e}))}return or.signTxns(t)})),f(Sr,"execute",(function(t){return t.contractVersion===N.V1_1?Er.execute(t):or.execute(t)})),Sr);function Pr(){return(Pr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,kr({txGroup:a,pool:r,initiatorSigner:o});case 4:return s=t.sent,t.next=7,st(n,[s]);case 7:return i=t.sent,c=w(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:it(a),confirmedRound:d,txnID:l,groupID:ct(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function kr(t){return Lr.apply(this,arguments)}function Lr(){return(Lr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.pool,o=e.initiatorSigner,t.next=3,o([n]);case 3:return s=t.sent,i=w(s,1),c=i[0],p=r.account.lsig,l=n.map((function(t,e){return 0===e?c:a.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rr(){return Rr=c(u().mark((function t(e){var n,r,o,s,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.data,o=e.initiatorSigner,t.prev=1,s=r.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:it(e),groupID:ct(e),lsig:n.account.lsig}})),t.next=5,o(s.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(s.map((function(t,e){return new Promise(function(){var r=c(u().mark((function r(o,s){var c,p,l,d,f,m;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,c=t.txns.map((function(n,r){return 0===r?i[e]:a.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),r.next=4,st(n,[c]);case 4:p=r.sent,l=w(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),r.next=15;break;case 12:r.prev=12,r.t0=r.catch(0),s(r.t0);case 15:case"end":return r.stop()}}),r,null,[[0,12]])})));return function(t,e){return r.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Rr.apply(this,arguments)}function Fr(){return(Fr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,o=e.assetID,s=e.assetOut,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=r.account.address(),l=a.default.makeApplicationNoOpTxnFromObject({from:p,appIndex:r.validatorAppID,appArgs:[lt("redeem")],note:mt.getAppCallTxnNoteWithClientName(r.contractVersion),accounts:[i],foreignAssets:0==r.asset2ID?[r.asset1ID,r.poolTokenID]:[r.asset1ID,r.asset2ID,r.poolTokenID],suggestedParams:c}),d=0===o?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:i,amount:BigInt(s),suggestedParams:c}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:i,assetIndex:o,amount:BigInt(s),suggestedParams:c}),f=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:p,amount:l.fee+d.fee,note:O,suggestedParams:c}),m=a.default.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[i]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,o=e.liquidityAssetID,s=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(s.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[lt("commit"),i],note:U([lt("tinymanStaking/v1:b"),c,t.encodeUint64(o),i])})}function Mr(){return(Mr=c(u().mark((function e(n){var r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,s=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Br({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof s){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[lt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=P,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=jn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=pn,exports.CONTRACT_VERSION=N,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=k,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=fr,exports.Swap=Nr,exports.SwapQuoteError=D,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=qn,exports.V2AddLiquidityTxnIndices=An,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=tt,exports.calculateAccountMinimumRequiredBalance=me,exports.calculatePriceImpact=Ht,exports.calculateSwapRate=Kt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=Jt,exports.isSwapQuoteErrorCausedByAmount=re,exports.poolUtils=Qe,exports.prepareCommitTransactions=function(t){return Mr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Rr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Pr.apply(this,arguments)},exports.sendAndWaitRawTransaction=st,exports.sumUpTxnFees=it,exports.tinymanContract_v1_1=ue,exports.tinymanContract_v2=pe,exports.tinymanJSSDKConfig=mt; -======= -i=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},s=r.iterator||"@@iterator",a=r.asyncIterator||"@@asyncIterator",o=r.toStringTag||"@@toStringTag";function u(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var s=e&&e.prototype instanceof d?e:d,a=Object.create(s.prototype),o=new w(r||[]);return a._invoke=function(t,e,n){var r="suspendedStart";return function(s,a){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===s)throw a;return b()}for(n.method=s,n.arg=a;;){var o=n.delegate;if(o){var i=v(o,n);if(i){if(i===l)continue;return i}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var u=p(t,e,n);if("normal"===u.type){if(r=n.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:n.done}}"throw"===u.type&&(r="completed",n.method="throw",n.arg=u.arg)}}}(t,n,o),a}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};u(g,s,(function(){return this}));var I=Object.getPrototypeOf,A=I&&I(I(D([])));A&&A!==e&&n.call(A,s)&&(g=A);var x=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(s,a,o,i){var u=p(t[s],t,a);if("throw"!==u.type){var c=u.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,i)}),(function(t){r("throw",t,o,i)})):e.resolve(l).then((function(t){c.value=t,o(c)}),(function(t){return r("throw",t,o,i)}))}i(u.arg)}var s;this._invoke=function(t,n){function a(){return new e((function(e,s){r(t,n,e,s)}))}return s=s?s.then(a,a):a()}}function v(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,v(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var s=r.arg;return s?s.done?(e[t.resultName]=s.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):s:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function y(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function w(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(y,this),this.reset(!0)}function D(t){if(t){var e=t[s];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,a=function e(){for(;++r=0;--s){var a=this.tryEntries[s],o=a.completion;if("root"===a.tryLoc)return r("end");if(a.tryLoc<=this.prev){var i=n.call(a,"catchLoc"),u=n.call(a,"finallyLoc");if(i&&u){if(this.prev=0;--r){var s=this.tryEntries[r];if(s.tryLoc<=this.prev&&n.call(s,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var s=r.arg;_(n)}return s}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function u(t,e,n,r,s,a,o){try{var i=t[a](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,s)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,s){var a=t.apply(e,n);function o(t){u(a,r,s,o,i,"next",t)}function i(t){u(a,r,s,o,i,"throw",t)}o(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:s}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,a=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw a}}}}var E="- would result negative",S="logic eval error:",N="exceeds schema integer count",O=/transaction \w+:/,P=function(t){m(n,h(Error));var e=v(n);function n(t,r){var s;p(this,n);for(var a=arguments.length,o=new Array(a>2?a-2:0),i=2;i1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var s="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*s))}catch(t){throw new Error(t.message)}return r}function G(t,e){var n=Number(t);return W({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function q(t,e){return W({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function W(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var s=y(J(e),2),a=s[0],o=s[1],i=y(J(Math.round(Number(z(a,o+r)))),2),u=i[0],c=i[1];return Number(z(u,c-r))}return Math.round(e)}function z(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function J(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function Y(t,e){return K.apply(this,arguments)}function K(){return(K=c(i().mark((function t(e,n){var r,s,a,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=b(n),t.prev=3,s.s();case 5:if((a=s.n()).done){t.next=18;break}return o=a.value,t.next=9,e.sendRawTransaction(o).do();case 9:return u=t.sent,c=u.txId,t.next=13,j(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new P(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function H(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function Z(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function $(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function tt(t){return(new TextEncoder).encode(t)}var et={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},nt={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"};function rt(){return(rt=c(i().mark((function t(e){var n,s,a,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.assetID,a=e.initiatorAddr,t.prev=1,t.next=4,n.getTransactionParams().do();case 4:return o=t.sent,u=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:a,assetIndex:s,amount:0,suggestedParams:o}),t.abrupt("return",[{txn:u,signers:[a]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new P(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function st(t,e){var n=Number(t.id),r=Number(e.id);return n>r?[o(o({},t),{},{id:n}),o(o({},e),{},{id:r})]:[o(o({},e),{},{id:r}),o(o({},t),{},{id:n})]}function at(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function ot(t){return 0===Number(t)}var it,ut={V1_1:"v1_1",V2:"v2"},ct={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},pt={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},lt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return tt("tinyman/".concat(t===ut.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),dt=(f(it={},ut.V1_1,{testnet:62368684,mainnet:552635992}),f(it,ut.V2,{testnet:148607e3,mainnet:1002541853}),it);function ft(t,e){var n=dt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function mt(){return(mt=c(i().mark((function t(e){var n,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationOptInTxnFromObject({from:o,appIndex:ft(s,a),note:lt.getAppCallTxnNoteWithClientName(a),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function gt(){return(gt=c(i().mark((function t(e){var n,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.contractVersion,o=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return u=t.sent,c=r.default.makeApplicationClearStateTxnFromObject({from:o,appIndex:ft(s,a),note:lt.getAppCallTxnNoteWithClientName(a),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[o]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var It=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),At=new(function(n){m(s,It);var r=v(s);function s(t,e){var n;return p(this,s),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(s,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=ft(n.network,ut.V1_1),s=y(at(n.asset1ID,n.asset2ID),2),a=s[0],o=s[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:a,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(i().mark((function t(s,a){var u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:u=t.sent,s(o(o({},u),{},{minimum_required_balance:wt(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),a(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function _t(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?k({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function wt(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var Dt,bt,Et=tt("e");function St(t){return Nt.apply(this,arguments)}function Nt(){return(Nt=c(i().mark((function n(s){var a,o,u,c,p,l,d,f,m,g,I,A,x,h,T,v,y,_,w,D,E;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.pool,u=s.accountAddr,n.next=3,a.accountInformation(u).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=o.account.address(),g=b(p),n.prev=10,g.s();case 12:if((I=g.n()).done){n.next=31;break}if((A=I.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=k({stateArray:x}),T=e.fromByteArray(L([r.default.decodeAddress(m).publicKey,Et,r.default.encodeUint64(o.asset1ID)])),v=e.fromByteArray(L([r.default.decodeAddress(m).publicKey,Et,r.default.encodeUint64(o.asset2ID)])),y=e.fromByteArray(L([r.default.decodeAddress(m).publicKey,Et,r.default.encodeUint64(o.poolTokenID)])),_=h[T],w=h[v],D=h[y],"bigint"==typeof _&&(l=_),"bigint"==typeof w&&(d=w),"bigint"==typeof D&&(f=D);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((E={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ot(){return(Ot=c(i().mark((function n(s){var a,o,u,c,p,l,d,f,m,g,I,A,x,h,T;return i().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=s.client,o=s.accountAddr,u=s.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==u})),d=[],l&&l["key-value"])for(f=k({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mLt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Lt,"]): ").concat(B.issuedLiquidity));case 48:return n.abrupt("return",B);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d=arguments;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(a=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",a[r]);case 4:return t.next=6,yt(n,r);case 6:return o=t.sent,u=_t(o,ft(s,ut.V1_1)),c=null,u&&(l=o["created-assets"][0],p=l.index,c={asset1ID:u[Pt[ut.V1_1].asset1],asset2ID:u[Pt[ut.V1_1].asset2],poolTokenID:p},a[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Mt.apply(this,arguments)}var Ct=Object.freeze({__proto__:null,getPoolInfo:Ft,getPoolReserves:function(t,e){return Rt.apply(this,arguments)},getPoolAssets:function(t){return Mt.apply(this,arguments)}});function Vt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,a=e.asset2ID,o=vt(ut.V2),u=o.generateLogicSigAccountForPool(e),c=ft(r,ut.V2),p=u.address(),l=at(s,a),t.next=8,yt(n,p);case 8:return d=t.sent,f=_t(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:ut.V2},f&&(m.asset1ProtocolFees=BigInt(f[Pt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Pt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Pt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Pt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Pt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Pt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Pt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Pt.v2.totalFeeShare]),m.poolTokenID=Number(f[Pt.v2.poolTokenID]),m.asset1ID=Number(f[Pt.v2.asset1]),m.asset2ID=Number(f[Pt.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function jt(){return(jt=c(i().mark((function t(e,n){var r,s,a;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,yt(e,n.account.address());case 2:return r=t.sent,s=_t(r,n.validatorAppID),a={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(a.asset1=BigInt(s[Pt.v2.asset1Reserves]),a.asset2=BigInt(s[Pt.v2.asset2Reserves]),a.issuedLiquidity=BigInt(s[Pt.v2.issuedPoolTokens])),t.abrupt("return",a);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Qt(){return(Qt=c(i().mark((function t(e){var n,r,s,a,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,yt(n,r);case 3:return a=t.sent,o=_t(a,ft(s,ut.V2)),u=null,o&&(u={asset1ID:o[Pt[ut.V2].asset1],asset2ID:o[Pt[ut.V2].asset2],poolTokenID:o[Pt[ut.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolInfo:Vt,getPoolReserves:function(t,e){return jt.apply(this,arguments)},getPoolAssets:function(t){return Qt.apply(this,arguments)}});function Gt(t){return Boolean(t&&!(t.asset1+t.asset2))}var qt,Wt,zt=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Gt(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=G(t.asset1,e.asset1)/G(t.asset2,e.asset2)),r},isPoolEmpty:Gt,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([Ft(t),Vt(t)])}}),Jt=o((f(qt={},ut.V1_1,o(o({},Ct),zt)),f(qt,ut.V2,o(o({},Ut),zt)),qt),zt);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Wt||(Wt={}));var Yt=96e4,Kt=859e3;function Ht(){return(Ht=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f,m,g,I,A,x,h,T,v,_,w,D,b,E,S,N;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.network,a=e.asset_1,o=e.asset_2,u=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=st(a,o),l=y(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],I=g.id,A=g.unit_name,x=ot(I),h=ft(s,ut.V1_1),T=At.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:I}),v=T.address(),_=r.default.makeApplicationOptInTxnFromObject({from:v,appIndex:h,note:lt.getAppCallTxnNoteWithClientName(ut.V1_1),appArgs:[tt("bootstrap"),r.default.encodeUint64(f),r.default.encodeUint64(I)],foreignAssets:x?[f]:[I],suggestedParams:c}),w=r.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:v,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:nt.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(A),assetURL:"https://tinyman.org",suggestedParams:c}),D=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:v,to:v,assetIndex:f,amount:0,suggestedParams:c}),b=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:v,amount:Zt(x),suggestedParams:c}),(E=[])[Wt.FUNDING_TXN]=b,E[Wt.VALIDATOR_APP_CALL]=_,E[Wt.POOL_TOKEN_CREATE]=w,E[Wt.ASSET1_OPT_IN]=D,x||(E[Wt.ASSET2_OPT_IN]=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:v,to:v,assetIndex:I,amount:0,suggestedParams:c})),S=r.default.assignGroupID(E),N=[{txn:S[Wt.FUNDING_TXN],signers:[u]},{txn:S[Wt.VALIDATOR_APP_CALL],signers:[v]},{txn:S[Wt.POOL_TOKEN_CREATE],signers:[v]},{txn:S[Wt.ASSET1_OPT_IN],signers:[v]}],S[Wt.ASSET2_OPT_IN]&&N.push({txn:S[Wt.ASSET2_OPT_IN],signers:[v]}),t.abrupt("return",N);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zt(t){return t?Yt:Kt}function $t(){return($t=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f,m,g,I,A,x;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=y(c,1),l=p[0],d=at(o,u),f=y(d,2),m=f[0],g=f[1],I=At.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),A=[],x=n.map((function(t,e){if(e===Wt.FUNDING_TXN)return A.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,I),s=n.txID,a=n.blob;return A.push(s),a})),t.abrupt("return",{signedTxns:x,txnIDs:A});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function te(t){return ee.apply(this,arguments)}function ee(){return(ee=c(i().mark((function t(e){var n,r,s,a,o;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,j(n,s[Wt.POOL_TOKEN_CREATE]);case 6:if(a=t.sent,"number"==typeof(o=a["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(o));case 10:return t.abrupt("return",{poolTokenID:o});case 13:throw t.prev=13,t.t0=t.catch(1),new P(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function ne(){return(ne=c(i().mark((function t(e){var n,r,s,a,o,u,c;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,te({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var re,se={generateTxns:function(t){return Ht.apply(this,arguments)},signTxns:function(t){return $t.apply(this,arguments)},execute:function(t){return ne.apply(this,arguments)},getBootstrapFundingTxnAmount:Zt};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(re||(re={}));var ae=5,oe=6;function ie(t,e){return ue.apply(this,arguments)}function ue(){return(ue=c(i().mark((function e(n,r){var s,a,o;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(a=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,a,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return o=e.t0,e.abrupt("return",o);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ce(t,e){return pe.apply(this,arguments)}function pe(){return(pe=c(i().mark((function t(e,n){var r;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ie(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function le(){return(le=c(i().mark((function e(n){var s,a,o,u,c,p,l,d,f,m,g,I,A,x,h,T,v,_,w,D;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.network,o=n.asset_1,u=n.asset_2,c=n.initiatorAddr,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=ft(a,ut.V2),d=t.getApplicationAddress(l),f=st(o,u),m=y(f,2),g=m[0].id,I=m[1].id,e.next=9,Jt.v2.getPoolInfo({client:s,network:a,asset1ID:g,asset2ID:I});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(u.unit_name," already exists"));case 12:return A=Tt.generateLogicSigAccountForPool({network:a,asset1ID:g,asset2ID:I}),x=A.address(),h=ot(I),(T=r.default.makeApplicationOptInTxnFromObject({from:x,appIndex:l,appArgs:[tt("bootstrap")],note:lt.getAppCallTxnNoteWithClientName(ut.V2),foreignAssets:[g,I],rekeyTo:d,suggestedParams:p})).fee=fe(h),v=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:x,amount:de(h),suggestedParams:p}),(_=[])[re.FUNDING_TXN]=v,_[re.VALIDATOR_APP_CALL]=T,w=r.default.assignGroupID(_),(D=[])[re.FUNDING_TXN]={txn:w[re.FUNDING_TXN],signers:[c]},D[re.VALIDATOR_APP_CALL]={txn:w[re.VALIDATOR_APP_CALL],signers:[x]},e.abrupt("return",D);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function de(t){return function(t,e){var n=vt(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(ut.V2,t)+fe(t)+1e5}function fe(e){return((e?ae:oe)+1)*t.ALGORAND_MIN_TX_FEE}function me(){return(me=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f,m,g,I,A,x;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.network,a=e.initiatorSigner,o=e.asset1ID,u=e.asset2ID,t.next=3,a([n]);case 3:return c=t.sent,p=y(c,1),l=p[0],d=at(o,u),f=y(d,2),m=f[0],g=f[1],I=Tt.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),A=[],x=n.map((function(t,e){if(e===re.FUNDING_TXN)return A.push(t.txn.txID().toString()),l;var n=r.default.signLogicSigTransactionObject(t.txn,I),s=n.txID,a=n.blob;return A.push(s),a})),t.abrupt("return",{signedTxns:x,txnIDs:A});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ge(){return(ge=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,a=s.asset1ID,o=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,ie(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Pt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:a,asset2ID:o}));case 21:throw t.prev=21,t.t3=t.catch(1),new P(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var Ie,Ae={generateTxns:function(t){return le.apply(this,arguments)},signTxns:function(t){return me.apply(this,arguments)},execute:function(t){return ge.apply(this,arguments)},getBootstrapFundingTxnAmount:de,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+de(e)}};var xe,he=(f(Ie={},ut.V1_1,se),f(Ie,ut.V2,Ae),f(Ie,"generateTxns",(function(t){return t.contractVersion===ut.V1_1?se.generateTxns(t):Ae.generateTxns(t)})),f(Ie,"signTxns",(function(t){return t.contractVersion===ut.V1_1?se.signTxns(t):Ae.signTxns(t)})),f(Ie,"execute",(function(t){return t.contractVersion===ut.V1_1?se.execute(t):Ae.execute(t)})),f(Ie,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===ut.V1_1?se.getBootstrapFundingTxnAmount(n):Ae.getBootstrapFundingTxnAmount(n)})),Ie);exports.V1_1AddLiquidityTxnIndices=void 0,(xe=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[xe.FEE_TXN=0]="FEE_TXN",xe[xe.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",xe[xe.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",xe[xe.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",xe[xe.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var Te,ve,ye,_e,we=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(_e=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",_e.FLEXIBLE="flexible",_e.INITIAL="initial";var De,be=(f(Te={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(Te,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(Te,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Te),Ee=(f(ve={},exports.V2AddLiquidityType.INITIAL,1),f(ve,exports.V2AddLiquidityType.SINGLE,2),f(ve,exports.V2AddLiquidityType.FLEXIBLE,2),ve),Se=(f(ye={},exports.V2AddLiquidityType.INITIAL,3),f(ye,exports.V2AddLiquidityType.FLEXIBLE,3),f(ye,exports.V2AddLiquidityType.SINGLE,2),ye);function Ne(t){var e=t.assetIn,n=t.assetOut;return G(n.decimals,Number(n.amount))/G(e.decimals,Number(e.amount))}function Oe(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,s=t.assetOut,a=Ne({assetIn:r,assetOut:s}),o=G(s.decimals,Number(n))/G(r.decimals,Number(e));return W({decimalPlaces:5},Math.abs(a/o-1))}function Pe(t){var e,n,r,s=t.reserves,a=t.totalFeeShare,o=t.asset1,i=t.asset2,u=s.asset1*s.asset2,c=s.asset1+BigInt(o.amount),p=s.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*s.issuedLiquidity*s.issuedLiquidity/u))))),f=d-s.issuedLiquidity,m=f*c/d,g=f*p/d,I=BigInt(o.amount)-m,A=BigInt(i.amount)-g;if(I>A){var x=I;r=ke(x,a),e={id:o.id,amount:x+r,decimals:o.decimals,reserves:s.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:i.decimals,reserves:s.asset2},f-=r*d/(c*BigInt(2))}else{var h=A;r=ke(h,a),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:s.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:o.decimals,reserves:s.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Oe({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function ke(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Le(e){return(Ee[e]+1)*t.ALGORAND_MIN_TX_FEE}var Fe=tt("add_liquidity"),Be=(f(De={},ut.V1_1,[tt("mint")]),f(De,ut.V2,{INITIAL_LIQUIDITY:[tt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Fe,tt("single")],FLEXIBLE_MODE:[Fe,tt("flexible")]}),De);function Re(){return(Re=c(i().mark((function t(e){var n,a,o,u,c,p,l,d,f,m,g,I,A,x,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.network,o=e.poolAddress,u=e.asset1In,c=e.asset2In,p=e.poolTokenOut,l=e.slippage,d=e.initiatorAddr,f=U("negative",l,p.amount),t.next=4,n.getTransactionParams().do();case 4:return m=t.sent,g=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:ft(a,ut.V1_1),appArgs:Be.v1_1,accounts:[d],note:lt.getAppCallTxnNoteWithClientName(ut.V1_1),foreignAssets:0==c.id?[u.id,p.id]:[u.id,c.id,p.id],suggestedParams:m}),I=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:m}),A=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:c.amount,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:o,assetIndex:c.id,amount:c.amount,suggestedParams:m}),x=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:d,assetIndex:p.id,amount:f,suggestedParams:m}),h=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:o,amount:g.fee+x.fee,note:s,suggestedParams:m}),T=r.default.assignGroupID([h,g,I,A,x]),t.abrupt("return",[{txn:T[0],signers:[d]},{txn:T[1],signers:[o]},{txn:T[2],signers:[d]},{txn:T[3],signers:[d]},{txn:T[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Me(){return(Me=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,o=n.account,t.next=4,a([s]);case 4:return u=t.sent,c=y(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:r.default.signLogicSigTransactionObject(t.txn,o).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ce(){return(Ce=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,I,A,x,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.signedTxns,o=e.initiatorAddr,t.prev=1,u=BigInt(s[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,St({client:n,pool:r,accountAddr:o});case 5:return c=t.sent,t.next=8,Y(n,[a]);case 8:return p=t.sent,l=y(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=H(s),I=Z(s),t.next=17,St({client:n,pool:r,accountAddr:o});case 17:return A=t.sent,(x=A.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:A.excessPoolTokens},txnID:m,groupID:I});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new P(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Ve=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,s=t.asset2In;if(0n===n.issuedLiquidity){var a=BigInt(Math.floor(Math.sqrt(Number(r)*Number(s))));if(a<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(a,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(s),poolTokenID:e.poolTokenID,poolTokenOut:a-BigInt(1e3),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(s)*n.issuedLiquidity/n.asset2,u=o0&&void 0!==arguments[0]?arguments[0]:"Output amount exceeds available liquidity";return p(this,n),e.call(this,t)}return d(n)}();function gn(){return(gn=c(i().mark((function t(e){var n,s,a,o,u,c,p,l,d,f,m,g,I,A,x,h,T;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,a=e.swapType,o=e.assetIn,u=e.assetOut,c=e.initiatorAddr,p=e.slippage,l=s.account.address(),(d=[s.asset1ID,s.asset2ID]).includes(o.id)&&d.includes(u.id)&&o.id!==u.id){t.next=5;break}throw new P({pool:s,assetIn:o,assetOut:u},"Input asset (#".concat(o.id,") and output asset (#").concat(u.id,") provided to generate transactions do not belong to the pool ").concat(l,"."));case 5:return t.next=7,n.getTransactionParams().do();case 7:return f=t.sent,m=ot(o.id),g=a===exports.SwapType.FixedInput?o.amount:U("positive",p,o.amount),I=a===exports.SwapType.FixedOutput?u.amount:U("negative",p,u.amount),A=m?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:g,suggestedParams:f}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:l,amount:g,assetIndex:o.id,suggestedParams:f}),(x=r.default.makeApplicationNoOpTxnFromObject({from:c,appIndex:s.validatorAppID,appArgs:[dn,fn[a],r.default.encodeUint64(I)],note:lt.getAppCallTxnNoteWithClientName(ut.V2),accounts:[l],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:f})).fee=In(a),(h=[])[cn.INPUT_TXN]=A,h[cn.APP_CALL_TXN]=x,T=r.default.assignGroupID(h),t.abrupt("return",[{txn:T[cn.INPUT_TXN],signers:[c]},{txn:T[cn.APP_CALL_TXN],signers:[c]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function In(e){return(ln[e]+1)*t.ALGORAND_MIN_TX_FEE}function An(){return(An=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,I,A,x,h;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return s=e.client,a=e.pool,o=e.txGroup,u=e.signedTxns,c=e.network,p=e.assetIn,t.next=3,Y(s,[u]);case 3:return l=t.sent,d=y(l,1),f=d[0],m=f.confirmedRound,g=f.txnID,t.next=10,ce(s,o);case 10:return I=t.sent,A=[a.asset1ID,a.asset2ID].filter((function(t){return t!==p.id}))[0],x=null==I||null===(n=I.find((function(t){return t.txn.txn.xaid===p.id})))||void 0===n?void 0:n.txn.txn,h=null==I||null===(r=I.find((function(t){return t.txn.txn.xaid===A})))||void 0===r?void 0:r.txn.txn,t.t0=m,t.t1=x&&{amount:BigInt(p.amount)-BigInt(x.aamt||0),id:p.id},t.t2=h&&{amount:h.aamt,id:A},t.next=19,Jt.v2.getPoolInfo({client:s,network:c,asset1ID:a.asset1ID,asset2ID:a.asset2ID});case 19:return t.t3=t.sent,t.t4=g,t.abrupt("return",{round:t.t0,assetIn:t.t1,assetOut:t.t2,pool:t.t3,txnID:t.t4});case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(t){var e=t.pool,n=t.assetIn,r=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new P({pool:e,assetIn:n},"Trying to swap on a non-existent pool");var s,a,o,i=BigInt(n.amount),u=e.totalFeeShare;if(n.id===e.asset1ID)s=e.asset2ID,a=e.asset1Reserves,o=e.asset2Reserves;else{if(n.id!==e.asset2ID)throw new P({pool:e,assetIn:n},"Input asset (#".concat(n.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,a=e.asset2Reserves,o=e.asset1Reserves}var c=Tn({inputSupply:a,outputSupply:o,swapInputAmount:i,totalFeeShare:u,decimals:r}),p=c.swapOutputAmount,l=c.totalFeeAmount,d=c.priceImpact;if(p>o)throw new mn;return{assetInID:n.id,assetInAmount:i,assetOutID:s,assetOutAmount:p,swapFee:Number(l),rate:G(r.assetOut,Number(p))/G(r.assetIn,Number(i)),priceImpact:d}}function hn(t){var e=t.pool,n=t.assetOut,r=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new P({pool:e,assetOut:n},"Trying to swap on a non-existent pool");var s,a,o,i=BigInt(n.amount),u=e.totalFeeShare;if(n.id===e.asset1ID)s=e.asset2ID,a=e.asset2Reserves,o=e.asset1Reserves;else{if(n.id!==e.asset2ID)throw new P({pool:e,assetOut:n},"Output asset (#".concat(n.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,a=e.asset1Reserves,o=e.asset2Reserves}var c=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,s=t.totalFeeShare,a=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,s=BigInt(e*n/(n-r))-e;return s+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:o,totalFeeShare:s}),u=o+i,c=Oe({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:a.assetIn},assetOut:{amount:r,decimals:a.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:a,outputSupply:o,swapOutputAmount:i,totalFeeShare:u,decimals:r}),p=c.swapInputAmount,l=c.totalFeeAmount,d=c.priceImpact;if(i>o)throw new mn;return{assetInID:s,assetInAmount:p,assetOutID:n.id,assetOutAmount:i,swapFee:Number(l),rate:G(r.assetOut,Number(i))/G(r.assetIn,Number(p)),priceImpact:d}}function Tn(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,s=t.totalFeeShare,a=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:s})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,s=n-BigInt(e*n/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:Oe({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:a.assetIn},assetOut:{amount:i,decimals:a.assetOut}})}}var vn={getQuote:function(t,e,n,r){return t===exports.SwapType.FixedInput?xn({pool:e,assetIn:n,decimals:r}):hn({pool:e,assetOut:n,decimals:r})},getFixedInputSwapQuote:xn,getFixedOutputSwapQuote:hn,generateTxns:function(t){return gn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return An.apply(this,arguments)},calculateFixedInputSwap:Tn};function yn(t,e){var n,r,s=BigInt(t),a=e.issuedLiquidity;return a>s+BigInt(1e3)?(n=s*e.asset1/a,r=s*e.asset2/a):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function _n(){return(_n=c(i().mark((function e(n){var s,a,o,u,c,p,l,d,f,m,g,I,A,x;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.poolTokenIn,u=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.account.address(),m=a.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:m,amount:o,suggestedParams:d}),(I=r.default.makeApplicationNoOpTxnFromObject({from:u,appIndex:a.validatorAppID,note:lt.getAppCallTxnNoteWithClientName(ut.V2),appArgs:[en,r.default.encodeUint64(U("negative",l,c)),r.default.encodeUint64(U("negative",l,p))],accounts:[f],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(A=[])[$e.ASSET_TRANSFER_TXN]=g,A[$e.APP_CALL_TXN]=I,x=r.default.assignGroupID(A),e.abrupt("return",[{txn:x[$e.ASSET_TRANSFER_TXN],signers:[u]},{txn:x[$e.APP_CALL_TXN],signers:[u]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function wn(){return(wn=c(i().mark((function e(n){var s,a,o,u,c,p,l,d,f,m,g,I,A,x,h,T,v,y,_;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=n.client,a=n.pool,o=n.initiatorAddr,u=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,s.getTransactionParams().do();case 3:if(d=e.sent,f=a.asset1ID,m=a.asset2ID,g=a.account.address(),I=a.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,x=0,h=U("negative",l,p),c!==f){e.next=17;break}A=h,x=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}A=0,x=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:g,assetIndex:I,amount:u,suggestedParams:d}),(v=r.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:lt.getAppCallTxnNoteWithClientName(ut.V2),appArgs:[en,r.default.encodeUint64(A),r.default.encodeUint64(x)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(y=[])[$e.ASSET_TRANSFER_TXN]=T,y[$e.APP_CALL_TXN]=v,_=r.default.assignGroupID(y),e.abrupt("return",[{txn:_[$e.ASSET_TRANSFER_TXN],signers:[o]},{txn:_[$e.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Dn(){return(Dn=c(i().mark((function t(e){var n,r,s,a,o,u,c,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,s=e.txGroup,a=e.signedTxns,t.next=3,Y(r,[a]);case 3:return o=t.sent,u=y(o,1),c=u[0].txnID,t.next=8,ce(r,s);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var bn,En,Sn={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,s=BigInt(r),a=yn(s,n),o=a.asset1OutputAmount,i=a.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:s}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,s=t.poolTokenIn,a=t.assetOutID,o=t.decimals,i=BigInt(s),u=yn(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(a===n.asset1ID){var d=vn.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(a!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var I=vn.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),A=I.swapOutputAmount,x=I.totalFeeAmount,h=I.priceImpact;e={round:r.round,assetOut:{assetId:a,amount:p+A},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:A},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:h}}}return e},generateTxns:function(t){return _n.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return wn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Dn.apply(this,arguments)}},Nn=(f(bn={},ut.V1_1,pn),f(bn,ut.V2,Sn),bn),On=3n,Pn=1000n;function kn(){return(kn=c(i().mark((function t(e){var n,s,a,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,s=e.txGroup,a=e.initiatorSigner,t.next=3,a([s]);case 3:return o=t.sent,u=y(o,2),c=u[0],p=u[1],l=s.map((function(t,e){return e===En.FEE_TXN_INDEX?c:e===En.ASSET_IN_TXN_INDEX?p:r.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ln(){return(Ln=c(i().mark((function t(e){var n,a,o,u,c,p,l,d,f,m,g,I,A,x,h,T,v,y;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,a=e.pool,o=e.swapType,u=e.assetIn,c=e.assetOut,p=e.slippage,l=e.initiatorAddr,d=a.account.address(),(f=[a.asset1ID,a.asset2ID]).includes(u.id)&&f.includes(c.id)&&u.id!==c.id){t.next=5;break}throw new P({pool:a,assetIn:u,assetOut:c},"Input asset (#".concat(u.id,") and output asset (#").concat(c.id,") provided to generate transactions do not belong to the pool ").concat(d,"."));case 5:return t.next=7,n.getTransactionParams().do();case 7:return m=t.sent,g=[tt("swap"),o===exports.SwapType.FixedInput?tt("fi"):tt("fo")],I=r.default.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:g,accounts:[l],note:lt.getAppCallTxnNoteWithClientName(ut.V1_1),foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:m}),A=o===exports.SwapType.FixedOutput?U("positive",p,u.amount):u.amount,x=0===u.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:A,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:u.id,amount:A,suggestedParams:m}),h=o===exports.SwapType.FixedInput?U("negative",p,c.amount):c.amount,T=0===c.id?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:l,amount:h,suggestedParams:m}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:l,assetIndex:c.id,amount:h,suggestedParams:m}),v=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I.fee+T.fee,note:s,suggestedParams:m}),y=r.default.assignGroupID([v,I,x,T]),t.abrupt("return",[{txn:y[0],signers:[l]},{txn:y[1],signers:[d]},{txn:y[2],signers:[l]},{txn:y[3],signers:[d]}]);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Fn(t){var e=t.pool,n=t.reserves,r=t.assetIn,s=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new P({pool:e,assetIn:r},"Trying to swap on a non-existent pool");var a,i,u,c=BigInt(r.amount);if(r.id===e.asset1ID)a=e.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==e.asset2ID)throw new P({pool:e,assetIn:r},"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));a=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*On/Pn,l=u-i*u/(i+(c-p));if(l>u)throw new mn;var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}};return{round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:a,assetOutAmount:l,swapFee:Number(p),rate:Ne(d),priceImpact:Oe(o({inputSupply:i,outputSupply:u},d))}}function Bn(t){return Rn.apply(this,arguments)}function Rn(){return(Rn=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,I,A,x;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,St({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Y(n,[s]);case 6:return p=t.sent,l=y(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,St({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(I=c.excessAsset1,A=g.excessAsset1):(I=c.excessAsset2,A=g.excessAsset2),(x=A-I)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount),assetOutID:o.id,assetOutAmount:BigInt(o.amount)+x,excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:A},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Mn(t){var e=t.pool,n=t.reserves,r=t.assetOut,s=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new P({pool:e,assetOut:r},"Trying to swap on a non-existent pool");var a,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)a=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new P({pool:e,assetOut:r},"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));a=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new mn;var c=o*i/(i-u)-o,p=c*Pn/(Pn-On),l=p-c,d=G(s.assetOut,Number(u))/G(s.assetIn,Number(p)),f=G(s.assetOut,Number(i))/G(s.assetIn,Number(o)),m=W({decimalPlaces:5},Math.abs(d/f-1));return{round:n.round,assetInID:a,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m}}function Cn(t){return Vn.apply(this,arguments)}function Vn(){return(Vn=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d,f,m,g,I,A,x;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,a=e.assetIn,o=e.assetOut,u=e.initiatorAddr,t.next=3,St({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,Y(n,[s]);case 6:return p=t.sent,l=y(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,St({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,a.id===r.asset1ID?(I=c.excessAsset1,A=g.excessAsset1):(I=c.excessAsset2,A=g.excessAsset2),(x=A-I)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:a.id,assetInAmount:BigInt(a.amount)-x,assetOutID:o.id,assetOutAmount:BigInt(o.amount),excessAmount:{assetID:a.id,excessAmountForSwap:x,totalExcessAmount:A},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xn(){return(Xn=c(i().mark((function t(e){var n,r,s,a,u,c,p,l,d,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,s=e.swapType,a=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new P({pool:r,swapType:s,txGroup:a},"Trying to swap on a non-existent pool");case 3:if(t.prev=3,p={id:a[En.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:a[En.ASSET_IN_TXN_INDEX].txn.amount},l={id:a[En.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:a[En.ASSET_OUT_TXN_INDEX].txn.amount},s!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,Bn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Cn({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",o(o({},d),{},{groupID:Z(a),fees:H(a)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new P(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(En||(En={}));var jn,Qn={getQuote:function(t,e,n,r,s){return t===exports.SwapType.FixedInput?Fn({pool:e,reserves:n,assetIn:r,decimals:s}):Mn({pool:e,reserves:n,assetOut:r,decimals:s})},getFixedInputSwapQuote:Fn,getFixedOutputSwapQuote:Mn,generateTxns:function(t){return Ln.apply(this,arguments)},signTxns:function(t){return kn.apply(this,arguments)},execute:function(t){return Xn.apply(this,arguments)},executeFixedOutputSwap:Cn},Un=4*t.ALGORAND_MIN_TX_FEE;function Gn(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status&&t.reason instanceof mn})))throw new mn;return t.filter((function(t){return"fulfilled"===t.status&&void 0!==t.value.quote})).map((function(t){return t.value}))}))}function qn(){return(qn=c(i().mark((function t(e){var n,r,s,a,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pools,r=e.assetIn,s=e.assetOut,a=e.amount,o=n.map((function(t){return new Promise((function(e,n){var o,i={pool:t.info,assetIn:{amount:a,id:Number(r.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves};try{o=t.info.contractVersion===ut.V1_1?Qn.getFixedInputSwapQuote(i):vn.getFixedInputSwapQuote(i),e({pool:t,quote:o})}catch(t){n(t)}}))})),t.next=4,Gn(o);case 4:return u=t.sent,t.abrupt("return",zn(u));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Wn(){return(Wn=c(i().mark((function t(e){var n,r,s,a,o,u;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pools,r=e.assetIn,s=e.assetOut,a=e.amount,o=n.map((function(t){return new Promise((function(e,n){var o,i={pool:t.info,assetOut:{amount:a,id:Number(s.id)},decimals:{assetIn:r.decimals,assetOut:s.decimals},reserves:t.reserves};try{o=t.info.contractVersion===ut.V1_1?Qn.getFixedOutputSwapQuote(i):vn.getFixedOutputSwapQuote(i),e({pool:t,quote:o})}catch(t){n(t)}}))})),t.next=4,Gn(o);case 4:return u=t.sent,t.abrupt("return",zn(u));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function zn(t){return t.filter((function(t){return!Gt(t.pool.reserves)})).sort((function(t,e){return e.quote.rate-t.quote.rate}))[0]}var Jn=(f(jn={},ut.V1_1,Qn),f(jn,ut.V2,vn),f(jn,"getQuote",(function(t){if(t.pools.every((function(t){return Gt(t.reserves)})))throw new Error("No pools available for swap");return t.type===exports.SwapType.FixedInput?function(t){return qn.apply(this,arguments)}(t):function(t){return Wn.apply(this,arguments)}(t)})),f(jn,"generateTxns",(function(t){return t.pool.contractVersion===ut.V1_1?Qn.generateTxns(t):vn.generateTxns(t)})),f(jn,"signTxns",(function(t){return t.pool.contractVersion===ut.V1_1?Qn.signTxns(t):vn.signTxns(t)})),f(jn,"execute",(function(t){return t.contractVersion===ut.V1_1?Qn.execute(t):vn.execute(t)})),jn);function Yn(){return(Yn=c(i().mark((function t(e){var n,r,s,a,o,u,c,p,l,d;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,a=e.initiatorSigner,t.prev=1,t.next=4,Kn({txGroup:s,pool:r,initiatorSigner:a});case 4:return o=t.sent,t.next=7,Y(n,[o]);case 7:return u=t.sent,c=y(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:H(s),confirmedRound:d,txnID:l,groupID:Z(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new P(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function Kn(t){return Hn.apply(this,arguments)}function Hn(){return(Hn=c(i().mark((function t(e){var n,s,a,o,u,c,p,l;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,s=e.pool,a=e.initiatorSigner,t.next=3,a([n]);case 3:return o=t.sent,u=y(o,1),c=u[0],p=s.account.lsig,l=n.map((function(t,e){return 0===e?c:r.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zn(){return Zn=c(i().mark((function t(e){var n,s,a,o,u,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,s=e.data,a=e.initiatorSigner,t.prev=1,o=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:H(e),groupID:Z(e),lsig:n.account.lsig}})),t.next=5,a(o.map((function(t){return t.txns})));case 5:return u=t.sent,p=Promise.all(o.map((function(t,e){return new Promise(function(){var s=c(i().mark((function s(a,o){var c,p,l,d,f,m;return i().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:return s.prev=0,c=t.txns.map((function(n,s){return 0===s?u[e]:r.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),s.next=4,Y(n,[c]);case 4:p=s.sent,l=y(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,a({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),s.next=15;break;case 12:s.prev=12,s.t0=s.catch(0),o(s.t0);case 15:case"end":return s.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new P(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Zn.apply(this,arguments)}function $n(){return($n=c(i().mark((function t(e){var n,a,o,u,c,p,l,d,f,m,g;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,a=e.pool,o=e.assetID,u=e.assetOut,c=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return p=t.sent,l=a.account.address(),d=r.default.makeApplicationNoOpTxnFromObject({from:l,appIndex:a.validatorAppID,appArgs:[tt("redeem")],note:lt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[c],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:p}),f=0===o?r.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:c,amount:BigInt(u),suggestedParams:p}):r.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:c,assetIndex:o,amount:BigInt(u),suggestedParams:p}),m=r.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:l,amount:d.fee+f.fee,note:s,suggestedParams:p}),g=r.default.assignGroupID([m,d,f]),t.abrupt("return",[{txn:g[0],signers:[c]},{txn:g[1],signers:[l]},{txn:g[2],signers:[l]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function tr(e){var n=e.suggestedParams,r=e.stakingAppID,s=e.initiatorAddr,a=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:s,suggestedParams:n,foreignAssets:[a],accounts:[o.accountAddress],appArgs:[tt("commit"),u],note:L([tt("tinymanStaking/v1:b"),c,t.encodeUint64(a),u])})}function er(){return(er=c(i().mark((function e(n){var r,s,a,o,u,c,p,l,d,f,m;return i().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,a=n.program,o=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=tr({suggestedParams:l,stakingAppID:s,program:a,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof o){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[o],accounts:[a.accountAddress],appArgs:[tt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=et,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Ze,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=he,exports.CONTRACT_VERSION=ut,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=nt,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=Nn,exports.Swap=Jn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=tn,exports.V2AddLiquidityTxnIndices=be,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=U,exports.calculateAccountMinimumRequiredBalance=wt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.poolUtils=Jt,exports.prepareCommitTransactions=function(t){return er.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Zn.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Yn.apply(this,arguments)},exports.sendAndWaitRawTransaction=Y,exports.sumUpTxnFees=H,exports.tinymanContract_v1_1=At,exports.tinymanContract_v2=Tt,exports.tinymanJSSDKConfig=lt; ->>>>>>> main +u=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},a=r.iterator||"@@iterator",o=r.asyncIterator||"@@asyncIterator",s=r.toStringTag||"@@toStringTag";function i(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var a=e&&e.prototype instanceof d?e:d,o=Object.create(a.prototype),s=new _(r||[]);return o._invoke=function(t,e,n){var r="suspendedStart";return function(a,o){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===a)throw o;return E()}for(n.method=a,n.arg=o;;){var s=n.delegate;if(s){var u=y(s,n);if(u){if(u===l)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var i=p(t,e,n);if("normal"===i.type){if(r=n.done?"completed":"suspendedYield",i.arg===l)continue;return{value:i.arg,done:n.done}}"throw"===i.type&&(r="completed",n.method="throw",n.arg=i.arg)}}}(t,n,s),o}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function d(){}function f(){}function m(){}var g={};i(g,a,(function(){return this}));var x=Object.getPrototypeOf,A=x&&x(x(S([])));A&&A!==e&&n.call(A,a)&&(g=A);var I=m.prototype=d.prototype=Object.create(g);function h(t){["next","throw","return"].forEach((function(e){i(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(a,o,s,u){var i=p(t[a],t,o);if("throw"!==i.type){var c=i.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,s,u)}),(function(t){r("throw",t,s,u)})):e.resolve(l).then((function(t){c.value=t,s(c)}),(function(t){return r("throw",t,s,u)}))}u(i.arg)}var a;this._invoke=function(t,n){function o(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(o,o):o()}}function y(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,y(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=p(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var a=r.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function v(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function w(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function _(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(v,this),this.reset(!0)}function S(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,o=function e(){for(;++r=0;--a){var o=this.tryEntries[a],s=o.completion;if("root"===o.tryLoc)return r("end");if(o.tryLoc<=this.prev){var u=n.call(o,"catchLoc"),i=n.call(o,"finallyLoc");if(u&&i){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),w(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;w(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:S(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function i(t,e,n,r,a,o,s){try{var u=t[o](s),i=u.value}catch(t){return void n(t)}u.done?e(i):Promise.resolve(i).then(r,a)}function c(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function s(t){i(o,r,a,s,u,"next",t)}function u(t){i(o,r,a,s,u,"throw",t)}s(void 0)}))}}function p(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,s=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){u=!0,o=t},f:function(){try{s||null==n.return||n.return()}finally{if(u)throw o}}}}exports.SwapQuoteErrorType=void 0,(r=exports.SwapQuoteErrorType||(exports.SwapQuoteErrorType={})).SwapRouterStaleDataError="SwapRouterStaleDataError",r.SwapRouterNoRouteError="SwapRouterNoRouteError",r.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",r.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",r.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",r.NoAvailablePoolError="NoAvailablePoolError",r.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",r.UnknownError="UnknownError",r.LowSwapAmountError="LowSwapAmountError",r.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",r.InvalidSwapTypeError="InvalidSwapTypeError";var D=function(t){m(n,h(Error));var e=v(n);function n(t,r){var a;return p(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(T(a),n),a}return d(n)}(),O=Uint8Array.from([1]),N={V1_1:"v1_1",V2:"v2"},P={id:"".concat(0),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},k={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},L="- would result negative",R="logic eval error:",F="exceeds schema integer count",B=/transaction \w+:/,M=function(t){m(n,h(Error));var e=v(n);function n(t,r){var a;p(this,n);for(var o=arguments.length,s=new Array(o>2?o-2:0),u=2;ur?[s(s({},t),{},{id:n}),s(s({},e),{},{id:r})]:[s(s({},e),{},{id:r}),s(s({},t),{},{id:n})]}function C(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function j(t){return 0===Number(t)}function X(t){return Number(t.id)}function q(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,o=void 0!==a&&a,s={},u=b(r);try{for(u.s();!(e=u.n()).done;){var i=e.value,c=i.key,p=void 0;if(1==i.value.type)p=i.value.bytes;else{if(2!=i.value.type)throw new Error("Unexpected state type: ".concat(i.value.type));p=i.value.uint}s[o?atob(c):c]=p}}catch(t){u.e(t)}finally{u.f()}return s}function U(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,o=b(t);try{for(o.s();!(e=o.n()).done;){var s=e.value;r.set(s,a),a+=s.length}}catch(t){o.e(t)}finally{o.f()}return r}var G=100000n,W=100000n,z=100000n,J=25000n+25000n,Y=25000n+3500n;function K(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],o=t["created-apps"]||[],s=t.assets||[];return G+W*BigInt(s.length)+z*BigInt(o.length+a.length)+Y*r+J*n}function H(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function Z(t,e){return $.apply(this,arguments)}function $(){return($=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,H(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function tt(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function et(t,e){var n=Number(t);return rt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function nt(t,e){return rt({decimalPlaces:0},Math.pow(10,Number(t))*Number(e))}function rt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=w(ot(e),2),o=a[0],s=a[1],u=w(ot(Math.round(Number(at(o,s+r)))),2),i=u[0],c=u[1];return Number(at(i,c-r))}return Math.round(e)}function at(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ot(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function st(t,e){return ut.apply(this,arguments)}function ut(){return(ut=c(u().mark((function t(e,n){var r,a,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],a=b(n),t.prev=3,a.s();case 5:if((o=a.n()).done){t.next=18;break}return s=o.value,t.next=9,e.sendRawTransaction(s).do();case 9:return i=t.sent,c=i.txId,t.next=13,Z(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),a.e(t.t0);case 23:return t.prev=23,a.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new M(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function it(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function ct(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function pt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function lt(t){return(new TextEncoder).encode(t)}function dt(t){return Boolean(t)&&void 0!==t.fallback_message}var ft,mt=new(function(){function t(){p(this,t),this.clientName="tinyman-js-sdk"}return d(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){return lt("tinyman/".concat(t===N.V1_1?"v1":t,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),gt=(f(ft={},N.V1_1,{testnet:62368684,mainnet:552635992}),f(ft,N.V2,{testnet:148607e3,mainnet:1002541853}),ft);function xt(t,e){var n=gt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function At(){return(At=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationOptInTxnFromObject({from:s,appIndex:xt(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var It;function ht(){return(ht=c(u().mark((function t(e){var n,r,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.contractVersion,s=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return i=t.sent,c=a.default.makeApplicationClearStateTxnFromObject({from:s,appIndex:xt(r,o),note:mt.getAppCallTxnNoteWithClientName(o),suggestedParams:i}),t.abrupt("return",[{txn:c,signers:[s]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapType=void 0,(It=exports.SwapType||(exports.SwapType={})).FixedInput="fixed-input",It.FixedOutput="fixed-output";var Tt,yt,vt;!function(t){t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(vt||(vt={}));var wt,_t,St=(f(Tt={},exports.SwapType.FixedInput,1),f(Tt,exports.SwapType.FixedOutput,2),Tt),Et=lt("swap"),bt=(f(yt={},exports.SwapType.FixedInput,lt("fixed-input")),f(yt,exports.SwapType.FixedOutput,lt("fixed-output")),yt),Dt={ASSET_OPT_IN:lt("asset_opt_in")},Ot={testnet:159521633,mainnet:0},Nt=(f(wt={},exports.SwapType.FixedInput,7),f(wt,exports.SwapType.FixedOutput,8),wt),Pt={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function kt(t){var e=Bt(t),n=e.assetIn,r=e.assetOut;return et(r.asset.decimals,Number(r.amount))/et(n.asset.decimals,Number(n.amount))}function Lt(t){var e=Ot[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Rt(t){return t[t.length-1].quote.amount_out}function Ft(t){return t[0].quote.amount_in}function Bt(t){return{assetIn:Ft(t),assetOut:Rt(t)}}function Mt(t){return Qt.apply(this,arguments)}function Qt(){return(Qt=c(u().mark((function e(n){var r,o,s,i,c,p,l;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.routerAppID,s=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o,appArgs:[Dt.ASSET_OPT_IN],foreignAssets:s,suggestedParams:c}),l=s.length,p.fee=t.ALGORAND_MIN_TX_FEE*(l+1),e.abrupt("return",p);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(t){return Ct.apply(this,arguments)}function Ct(){return(Ct=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,w,_,S,E,b,D;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,o=n.client,s=n.network,i=n.swapType,c=n.route,e.next=3,o.getTransactionParams().do();case 3:return p=e.sent,l=[X(c[0].quote.amount_in.asset),X(c[0].quote.amount_out.asset),X(c[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Ft(c).amount),Number(Rt(c).amount)],x=g[0],A=g[1],I=[c[0].pool.address,c[1].pool.address],h=I[0],T=I[1],y=j(d),v=Lt(s),w=y?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(v),amount:x,suggestedParams:p}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(v),amount:x,assetIndex:d,suggestedParams:p}),(_=a.default.makeApplicationNoOpTxnFromObject({from:r,appIndex:v,appArgs:[Et,bt[i],a.default.encodeUint64(A)],foreignApps:[xt(s,N.V2)],foreignAssets:[d,f,m],accounts:[h,T],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Nt[i]+1),S=[w,_],e.next=15,jt({client:o,network:s,assetIDs:[d,f,m]});case 15:if(!((E=e.sent).length>0)){e.next=21;break}return e.next=19,Mt({client:o,initiatorAddr:r,assetIDs:E,routerAppID:v});case 19:b=e.sent,S.unshift(b);case 21:return D=a.default.assignGroupID(S),e.abrupt("return",D.map((function(t){return{txn:t,signers:[r]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(t){return Xt.apply(this,arguments)}function Xt(){return(Xt=c(u().mark((function e(n){var r,a,o,s,i,c;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,s=t.getApplicationAddress(Lt(a)),e.next=4,r.accountInformation(s).do();case 4:return i=e.sent,c=i.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return 0!==t&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qt(t){return Ut.apply(this,arguments)}function Ut(){return(Ut=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,a=e.assetOutID,o=e.swapType,s=e.network,i={asset_in_id:String(r),asset_out_id:String(a),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Pt[s].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(!dt(p)){t.next=13;break}throw new D(p.type,p.fallback_message);case 13:throw new D(exports.SwapQuoteErrorType.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new D(exports.SwapQuoteErrorType.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.SwapQuoteType=void 0,(_t=exports.SwapQuoteType||(exports.SwapQuoteType={})).Direct="direct",_t.Router="router";var Gt=4*t.ALGORAND_MIN_TX_FEE;function Wt(e){return(St[e]+2)*t.ALGORAND_MIN_TX_FEE}function zt(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:.003;return Math.ceil(1/t)}function Jt(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:.003;return tn&&(e=a,n=o)}return e}function re(t){return t instanceof D&&[exports.SwapQuoteErrorType.SwapRouterInsufficientReservesError,exports.SwapQuoteErrorType.SwapRouterLowSwapAmountError,exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,exports.SwapQuoteErrorType.LowSwapAmountError].includes(t.type)}var ae={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},oe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},se=d((function t(e){p(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ue=new(function(n){m(a,se);var r=v(a);function a(t,e){var n;return p(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return d(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=xt(n.network,N.V1_1),a=w(C(n.asset1ID,n.asset2ID),2),o=a[0],s=a[1],u=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),i={asset_id_1:o,asset_id_2:s,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=c(u().mark((function t(a,o){var i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(r).do();case 3:i=t.sent,a(s(s({},i),{},{minimum_required_balance:me(i)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),o(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function fe(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function me(t){var e=t["apps-total-schema"];return 1e5+1e5*(t.assets||[]).length+1e5*(t["created-apps"]||[]).length+1e5*(t["apps-local-state"]||[]).length+5e4*Number(e&&e["num-byte-slice"]||0)+28500*Number(e&&e["num-uint"]||0)+1e5*(t["apps-total-extra-pages"]||0)}var ge,xe,Ae=lt("e");function Ie(t){return he.apply(this,arguments)}function he(){return(he=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,w,_,S,E;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.pool,i=r.accountAddr,n.next=3,o.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:c=n.sent,p=c["apps-local-state"]||[],l=0n,d=0n,f=0n,m=s.account.address(),g=b(p),n.prev=10,g.s();case 12:if((x=g.n()).done){n.next=31;break}if((A=x.value).id==s.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=q({stateArray:I}),T=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ae,a.default.encodeUint64(s.asset1ID)])),y=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ae,a.default.encodeUint64(s.asset2ID)])),v=e.fromByteArray(U([a.default.decodeAddress(m).publicKey,Ae,a.default.encodeUint64(s.poolTokenID)])),w=h[T],_=h[y],S=h[v],"bigint"==typeof w&&(l=w),"bigint"==typeof _&&(d=_),"bigint"==typeof S&&(f=S);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),g.e(n.t0);case 36:return n.prev=36,g.f(),n.finish(36);case 39:if(!((E={excessAsset1:l,excessAsset2:d,excessPoolTokens:f}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Te(){return(Te=c(u().mark((function n(r){var o,s,i,c,p,l,d,f,m,g,x,A,I,h,T;return u().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return o=r.client,s=r.accountAddr,i=r.validatorAppID,n.next=3,o.accountInformation(s).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(c=n.sent,p=c["apps-local-state"]||[],l=p.find((function(t){return t.id==i})),d=[],l&&l["key-value"])for(f=q({stateArray:l["key-value"]}),m=0,g=Object.entries(f);mwe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(we,"]): ").concat(L.issuedLiquidity));case 48:return n.abrupt("return",L);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function be(){return be=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d=arguments;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,a=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,de(n,r);case 6:return s=t.sent,i=fe(s,xt(a,N.V1_1)),c=null,i&&(l=s["created-assets"][0],p=l.index,c={asset1ID:i[ye[N.V1_1].asset1],asset2ID:i[ye[N.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),be.apply(this,arguments)}var De=Object.freeze({__proto__:null,getPoolInfo:_e,getPoolReserves:function(t,e){return Ee.apply(this,arguments)},getPoolAssets:function(t){return be.apply(this,arguments)}});function Oe(t){return Ne.apply(this,arguments)}function Ne(){return(Ne=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.asset1ID,o=e.asset2ID,s=le(N.V2),i=s.generateLogicSigAccountForPool(e),c=xt(r,N.V2),p=i.address(),l=C(a,o),t.next=8,de(n,p);case 8:return d=t.sent,f=fe(d,c),m={account:i,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?exports.PoolStatus.READY:exports.PoolStatus.NOT_CREATED,contractVersion:N.V2},f&&(m.asset1ProtocolFees=BigInt(f[ye.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[ye.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[ye.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[ye.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[ye.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[ye.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[ye.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[ye.v2.totalFeeShare]),m.poolTokenID=Number(f[ye.v2.poolTokenID]),m.asset1ID=Number(f[ye.v2.asset1]),m.asset2ID=Number(f[ye.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pe(){return(Pe=c(u().mark((function t(e,n){var r,a,o;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,de(e,n.account.address());case 2:return r=t.sent,a=fe(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[ye.v2.asset1Reserves]),o.asset2=BigInt(a[ye.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[ye.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ke(){return(ke=c(u().mark((function t(e){var n,r,a,o,s,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,a=e.network,t.next=3,de(n,r);case 3:return o=t.sent,s=fe(o,xt(a,N.V2)),i=null,s&&(i={asset1ID:s[ye[N.V2].asset1],asset2ID:s[ye[N.V2].asset2],poolTokenID:s[ye[N.V2].poolTokenID]}),t.abrupt("return",i);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Le=Object.freeze({__proto__:null,getPoolInfo:Oe,getPoolReserves:function(t,e){return Pe.apply(this,arguments)},getPoolAssets:function(t){return ke.apply(this,arguments)}});function Re(t){return Boolean(t&&!(t.asset1+t.asset2))}var Fe,Be,Me=Object.freeze({__proto__:null,getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolPairRatio:function(t,e){var n=Re(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=et(t.asset1,e.asset1)/et(t.asset2,e.asset2)),r},isPoolEmpty:Re,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===exports.PoolStatus.READY},getPoolsForPair:function(t){return Promise.all([_e(t),Oe(t)])}}),Qe=s((f(Fe={},N.V1_1,s(s({},De),Me)),f(Fe,N.V2,s(s({},Le),Me)),Fe),Me);!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN"}(Be||(Be={}));var Ve=96e4,Ce=859e3;function je(){return(je=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,_,S,E,b,D,O;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.asset_1,s=e.asset_2,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=V(o,s),l=w(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],x=g.id,A=g.unit_name,I=j(x),h=xt(r,N.V1_1),T=ue.generateLogicSigAccountForPool({network:r,asset1ID:f,asset2ID:x}),y=T.address(),v=a.default.makeApplicationOptInTxnFromObject({from:y,appIndex:h,note:mt.getAppCallTxnNoteWithClientName(N.V1_1),appArgs:[lt("bootstrap"),a.default.encodeUint64(f),a.default.encodeUint64(x)],foreignAssets:I?[f]:[x],suggestedParams:c}),_=a.default.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:k.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(A),assetURL:"https://tinyman.org",suggestedParams:c}),S=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:y,amount:Xe(I),suggestedParams:c}),(b=[])[Be.FUNDING_TXN]=E,b[Be.VALIDATOR_APP_CALL]=v,b[Be.POOL_TOKEN_CREATE]=_,b[Be.ASSET1_OPT_IN]=S,I||(b[Be.ASSET2_OPT_IN]=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:x,amount:0,suggestedParams:c})),D=a.default.assignGroupID(b),O=[{txn:D[Be.FUNDING_TXN],signers:[i]},{txn:D[Be.VALIDATOR_APP_CALL],signers:[y]},{txn:D[Be.POOL_TOKEN_CREATE],signers:[y]},{txn:D[Be.ASSET1_OPT_IN],signers:[y]}],D[Be.ASSET2_OPT_IN]&&O.push({txn:D[Be.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",O);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(t){return t?Ve:Ce}function qe(){return(qe=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=C(s,i),f=w(d,2),m=f[0],g=f[1],x=ue.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),A=[],I=n.map((function(t,e){if(e===Be.FUNDING_TXN)return A.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,x),r=n.txID,o=n.blob;return A.push(r),o})),t.abrupt("return",{signedTxns:I,txnIDs:A});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ue(t){return Ge.apply(this,arguments)}function Ge(){return(Ge=c(u().mark((function t(e){var n,r,a,o,s;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,a=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,Z(n,a[Be.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(s=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(s));case 10:return t.abrupt("return",{poolTokenID:s});case 13:throw t.prev=13,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function We(){return(We=c(u().mark((function t(e){var n,r,a,o,s,i,c;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.signedTxns,c=e.txnIDs,t.next=3,Ue({client:n,signedTxns:i,txnIDs:c});case 3:return t.abrupt("return",Qe.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ze,Je={generateTxns:function(t){return je.apply(this,arguments)},signTxns:function(t){return qe.apply(this,arguments)},execute:function(t){return We.apply(this,arguments)},getBootstrapFundingTxnAmount:Xe};!function(t){t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL"}(ze||(ze={}));var Ye=5,Ke=6;function He(t,e){return Ze.apply(this,arguments)}function Ze(){return(Ze=c(u().mark((function e(n,r){var a,o,s;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(a=r.find((function(t){return"appl"===t.txn.type})))||void 0===a?void 0:a.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,1e3);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return s=e.t0,e.abrupt("return",s);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function $e(t,e){return tn.apply(this,arguments)}function tn(){return(tn=c(u().mark((function t(e,n){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,He(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(){return(en=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,_,S;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.network,s=n.asset_1,i=n.asset_2,c=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return p=e.sent,l=xt(o,N.V2),d=t.getApplicationAddress(l),f=V(s,i),m=w(f,2),g=m[0].id,x=m[1].id,e.next=9,Qe.v2.getPoolInfo({client:r,network:o,asset1ID:g,asset2ID:x});case 9:if(e.sent.status!==exports.PoolStatus.READY){e.next=12;break}throw new Error("Pool for ".concat(s.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=pe.generateLogicSigAccountForPool({network:o,asset1ID:g,asset2ID:x}),I=A.address(),h=j(x),(T=a.default.makeApplicationOptInTxnFromObject({from:I,appIndex:l,appArgs:[lt("bootstrap")],note:mt.getAppCallTxnNoteWithClientName(N.V2),foreignAssets:[g,x],rekeyTo:d,suggestedParams:p})).fee=rn(h),y=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:I,amount:nn(h),suggestedParams:p}),(v=[])[ze.FUNDING_TXN]=y,v[ze.VALIDATOR_APP_CALL]=T,_=a.default.assignGroupID(v),(S=[])[ze.FUNDING_TXN]={txn:_[ze.FUNDING_TXN],signers:[c]},S[ze.VALIDATOR_APP_CALL]={txn:_[ze.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",S);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function nn(t){return function(t,e){var n=le(t).schema,r=4e5+28500*n.numLocalInts+5e4*n.numLocalByteSlices;return e||(r+=1e5),r}(N.V2,t)+rn(t)+1e5}function rn(e){return((e?Ye:Ke)+1)*t.ALGORAND_MIN_TX_FEE}function an(){return(an=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.network,o=e.initiatorSigner,s=e.asset1ID,i=e.asset2ID,t.next=3,o([n]);case 3:return c=t.sent,p=w(c,1),l=p[0],d=C(s,i),f=w(d,2),m=f[0],g=f[1],x=pe.generateLogicSigAccountForPool({network:r,asset1ID:m,asset2ID:g}),A=[],I=n.map((function(t,e){if(e===ze.FUNDING_TXN)return A.push(t.txn.txID().toString()),l;var n=a.default.signLogicSigTransactionObject(t.txn,x),r=n.txID,o=n.blob;return A.push(r),o})),t.abrupt("return",{signedTxns:I,txnIDs:A});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function on(){return(on=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,a=e.pool,o=a.asset1ID,s=a.asset2ID,i=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,He(n,i);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(ye.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",Qe.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:s}));case 21:throw t.prev=21,t.t3=t.catch(1),new M(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var sn,un={generateTxns:function(t){return en.apply(this,arguments)},signTxns:function(t){return an.apply(this,arguments)},execute:function(t){return on.apply(this,arguments)},getBootstrapFundingTxnAmount:nn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+nn(e)}};var cn,pn=(f(sn={},N.V1_1,Je),f(sn,N.V2,un),f(sn,"generateTxns",(function(t){return t.contractVersion===N.V1_1?Je.generateTxns(t):un.generateTxns(t)})),f(sn,"signTxns",(function(t){return t.contractVersion===N.V1_1?Je.signTxns(t):un.signTxns(t)})),f(sn,"execute",(function(t){return t.contractVersion===N.V1_1?Je.execute(t):un.execute(t)})),f(sn,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===N.V1_1?Je.getBootstrapFundingTxnAmount(n):un.getBootstrapFundingTxnAmount(n)})),sn);exports.V1_1AddLiquidityTxnIndices=void 0,(cn=exports.V1_1AddLiquidityTxnIndices||(exports.V1_1AddLiquidityTxnIndices={}))[cn.FEE_TXN=0]="FEE_TXN",cn[cn.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",cn[cn.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",cn[cn.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",cn[cn.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN";var ln,dn,fn,mn,gn=5*t.ALGORAND_MIN_TX_FEE;exports.V2AddLiquidityType=void 0,(mn=exports.V2AddLiquidityType||(exports.V2AddLiquidityType={})).SINGLE="single",mn.FLEXIBLE="flexible",mn.INITIAL="initial";var xn,An=(f(ln={},exports.V2AddLiquidityType.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),f(ln,exports.V2AddLiquidityType.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),f(ln,exports.V2AddLiquidityType.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),ln),In=(f(dn={},exports.V2AddLiquidityType.INITIAL,1),f(dn,exports.V2AddLiquidityType.SINGLE,2),f(dn,exports.V2AddLiquidityType.FLEXIBLE,2),dn),hn=(f(fn={},exports.V2AddLiquidityType.INITIAL,3),f(fn,exports.V2AddLiquidityType.FLEXIBLE,3),f(fn,exports.V2AddLiquidityType.SINGLE,2),fn);function Tn(t){var e,n,r,a=t.reserves,o=t.totalFeeShare,s=t.asset1,u=t.asset2,i=a.asset1*a.asset2,c=a.asset1+BigInt(s.amount),p=a.asset2+BigInt(u.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/i))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,x=BigInt(s.amount)-m,A=BigInt(u.amount)-g;if(x>A){var I=x;r=yn(I,o),e={id:s.id,amount:I+r,decimals:s.decimals,reserves:a.asset1},n={id:u.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:u.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=A;r=yn(h,o),e={id:u.id,amount:h+r,decimals:u.decimals,reserves:a.asset2},n={id:s.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:s.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:Ht({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function yn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function vn(e){return(In[e]+1)*t.ALGORAND_MIN_TX_FEE}var wn=lt("add_liquidity"),_n=(f(xn={},N.V1_1,[lt("mint")]),f(xn,N.V2,{INITIAL_LIQUIDITY:[lt("add_initial_liquidity")],SINGLE_ASSET_MODE:[wn,lt("single")],FLEXIBLE_MODE:[wn,lt("flexible")]}),xn);function Sn(){return(Sn=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,o=e.poolAddress,s=e.asset1In,i=e.asset2In,c=e.poolTokenOut,p=e.slippage,l=e.initiatorAddr,d=tt("negative",p,c.amount),t.next=4,n.getTransactionParams().do();case 4:return f=t.sent,m=a.default.makeApplicationNoOpTxnFromObject({from:o,appIndex:xt(r,N.V1_1),appArgs:_n.v1_1,accounts:[l],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==i.id?[s.id,c.id]:[s.id,i.id,c.id],suggestedParams:f}),g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:s.id,amount:s.amount,suggestedParams:f}),x=0===i.id?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:i.amount,suggestedParams:f}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),I=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+A.fee,note:O,suggestedParams:f}),h=a.default.assignGroupID([I,m,g,x,A]),t.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,s=n.account,t.next=4,o([r]);case 4:return i=t.sent,c=w(i,3),p=c[0],l=c[1],d=c[2],f=r.map((function(t,e){return e===exports.V1_1AddLiquidityTxnIndices.FEE_TXN?p:e===exports.V1_1AddLiquidityTxnIndices.ASSET1_IN_TXN?l:e===exports.V1_1AddLiquidityTxnIndices.ASSET2_IN_TXN?d:a.default.signLogicSigTransactionObject(t.txn,s).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function bn(){return(bn=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,A,I,h;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.signedTxns,s=e.initiatorAddr,t.prev=1,i=BigInt(a[exports.V1_1AddLiquidityTxnIndices.LIQUDITY_OUT_TXN].txn.amount),t.next=5,Ie({client:n,pool:r,accountAddr:s});case 5:return c=t.sent,t.next=8,st(n,[o]);case 8:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=it(a),x=ct(a),t.next=17,Ie({client:n,pool:r,accountAddr:s});case 17:return A=t.sent,(I=A.excessPoolTokens-c.excessPoolTokens)<0n&&(I=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:i+I,excessAmount:{excessAmountForAddingLiquidity:I,totalExcessAmount:A.excessPoolTokens},txnID:m,groupID:x});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(h=new M(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&h.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),h;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Dn=Object.freeze({__proto__:null,getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var o=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(o<=BigInt(1e3))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(1e3,", this quote is for ").concat(o,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:o-BigInt(1e3),share:1}}var s=BigInt(r)*n.issuedLiquidity/n.asset1,u=BigInt(a)*n.issuedLiquidity/n.asset2,i=ss)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Jt(Number(e)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");return{assetInID:u,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:et(p,Number(m))/et(i,Number(l)),priceImpact:x}}function $n(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!Qe.isPoolReady(a))throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,s,u=n.id,i=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)o=a.asset2Reserves,s=a.asset1Reserves;else{if(c!==a.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));o=a.asset1Reserves,s=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,o=t.decimals,s=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=BigInt(e*n/(n-r))-e;return a+=BigInt(1)}({inputSupply:e,outputSupply:n,outputAmount:r}),u=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n))));return BigInt(r)-e}({swapAmount:s,totalFeeShare:a}),i=s+u,c=Ht({inputSupply:e,outputSupply:n,assetIn:{amount:i,decimals:o.assetIn},assetOut:{amount:r,decimals:o.assetOut}});return{swapInputAmount:i,totalFeeAmount:u,priceImpact:c}}({inputSupply:o,outputSupply:s,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:i,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,x=f.priceImpact;if(l>s)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Jt(Number(m)))throw new D(exports.SwapQuoteErrorType.LowSwapAmountError,"Swap amount is too low.");return{type:exports.SwapQuoteType.Direct,data:{pool:a,quote:{assetInID:u,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:et(p,Number(l))/et(i,Number(m)),priceImpact:x}}}}function tr(t){return er.apply(this,arguments)}function er(){return(er=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,o=e.isSwapRouterEnabled,s=e.network,i=e.pool,c=[],i?c.push(new Promise((function(t,e){try{var o=Zn({amount:n,assetIn:r,assetOut:a,pool:i});t({type:exports.SwapQuoteType.Direct,data:{pool:i,quote:o}})}catch(t){e(t)}}))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(qt({amount:n,assetInID:r.id,assetOutID:a.id,swapType:exports.SwapType.FixedInput,network:s}).then((function(t){return{type:exports.SwapQuoteType.Router,data:t}}))),t.next=6,Hn(c);case 6:return p=t.sent,t.abrupt("return",ne(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(t){return rr.apply(this,arguments)}function rr(){return(rr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,a=e.assetOut,o=e.isSwapRouterEnabled,s=e.network,i=e.pool,c=[new Promise((function(t,e){try{t($n({amount:n,assetIn:r,assetOut:a,pool:i}))}catch(t){e(t)}}))],o&&c.push(qt({amount:n,assetInID:r.id,assetOutID:a.id,swapType:exports.SwapType.FixedOutput,network:s}).then((function(t){return{type:exports.SwapQuoteType.Router,data:t}}))),t.next=5,Hn(c);case 5:return p=t.sent,t.abrupt("return",ne(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,o=t.decimals,s=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),u=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=n-BigInt(e*n/(e+BigInt(r)));return a-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-s});return{swapOutputAmount:u,totalFeeAmount:s,priceImpact:Ht({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:o.assetIn},assetOut:{amount:u,decimals:o.assetOut}})}}!function(t){t[t.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN"}(Xn||(Xn={}));var or={getQuote:function(t){return Kn.apply(this,arguments)},getFixedInputSwapQuote:tr,getFixedInputDirectSwapQuote:Zn,getFixedOutputDirectSwapQuote:$n,getFixedOutputSwapQuote:nr,generateTxns:function(t){return Jn.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Yn.apply(this,arguments)},calculateFixedInputSwap:ar};function sr(t,e){var n,r,a=BigInt(t),o=e.issuedLiquidity;return o>a+BigInt(1e3)?(n=a*e.asset1/o,r=a*e.asset2/o):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function ur(){return(ur=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.poolTokenIn,i=n.initiatorAddr,c=n.minAsset1Amount,p=n.minAsset2Amount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.account.address(),m=o.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return g=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:m,amount:s,suggestedParams:d}),(x=a.default.makeApplicationNoOpTxnFromObject({from:i,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Un,a.default.encodeUint64(tt("negative",l,c)),a.default.encodeUint64(tt("negative",l,p))],accounts:[f],foreignAssets:[o.asset1ID,o.asset2ID],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(A=[])[Xn.ASSET_TRANSFER_TXN]=g,A[Xn.APP_CALL_TXN]=x,I=a.default.assignGroupID(A),e.abrupt("return",[{txn:I[Xn.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[Xn.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ir(){return(ir=c(u().mark((function e(n){var r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,w;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,o=n.pool,s=n.initiatorAddr,i=n.poolTokenIn,c=n.outputAssetId,p=n.minOutputAssetAmount,l=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(d=e.sent,f=o.asset1ID,m=o.asset2ID,g=o.account.address(),x=o.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,h=tt("negative",l,p),c!==f){e.next=17;break}A=h,I=0,e.next=23;break;case 17:if(c!==m){e.next=22;break}A=0,I=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return T=a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:s,to:g,assetIndex:x,amount:i,suggestedParams:d}),(y=a.default.makeApplicationNoOpTxnFromObject({from:s,appIndex:o.validatorAppID,note:mt.getAppCallTxnNoteWithClientName(N.V2),appArgs:[Un,a.default.encodeUint64(A),a.default.encodeUint64(I)],accounts:[g],foreignAssets:[c],suggestedParams:d})).fee=3*t.ALGORAND_MIN_TX_FEE,(v=[])[Xn.ASSET_TRANSFER_TXN]=T,v[Xn.APP_CALL_TXN]=y,w=a.default.assignGroupID(v),e.abrupt("return",[{txn:w[Xn.ASSET_TRANSFER_TXN],signers:[s]},{txn:w[Xn.APP_CALL_TXN],signers:[s]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function cr(){return(cr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.client,a=e.txGroup,o=e.signedTxns,t.next=3,st(r,[o]);case 3:return s=t.sent,i=w(s,1),c=i[0].txnID,t.next=8,$e(r,a);case 8:if(t.t1=n=t.sent,t.t0=null===t.t1,t.t0){t.next=12;break}t.t0=void 0===n;case 12:if(!t.t0){t.next=16;break}t.t2=void 0,t.next=17;break;case 16:t.t2=n.map((function(t){return{assetId:t.txn.txn.xaid,amount:t.txn.txn.aamt}}));case 17:return p=t.t2,t.abrupt("return",{outputAssets:p,txnID:c});case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pr,lr,dr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=sr(a,n),s=o.asset1OutputAmount,u=o.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:s},asset2Out:{assetId:e.asset2ID,amount:u},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,o=t.assetOutID,s=t.decimals,u=BigInt(a),i=sr(u,r),c=i.asset1OutputAmount,p=i.asset2OutputAmount,l=n.totalFeeShare;if(o===n.asset1ID){var d=or.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:s}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(o!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var x=or.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:s}),A=x.swapOutputAmount,I=x.totalFeeAmount,h=x.priceImpact;e={round:r.round,assetOut:{assetId:o,amount:p+A},poolTokenIn:{assetId:n.poolTokenID,amount:u},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:A},swapFees:{assetId:n.asset2ID,amount:I},priceImpact:h}}}return e},generateTxns:function(t){return ur.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return ir.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return cr.apply(this,arguments)}},fr=(f(pr={},N.V1_1,{generateTxns:function(t){return Gn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),o=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,s=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:e.poolTokenID,poolTokenIn:a,asset1ID:e.asset1ID,asset1Out:o,asset2ID:e.asset2ID,asset2Out:s}},signTxns:function(t){return Wn.apply(this,arguments)},execute:function(t){return zn.apply(this,arguments)}}),f(pr,N.V2,dr),pr),mr=3n,gr=1000n;function xr(){return(xr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.pool,r=e.txGroup,o=e.initiatorSigner,t.next=3,o([r]);case 3:return s=t.sent,i=w(s,2),c=i[0],p=i[1],l=r.map((function(t,e){return e===lr.FEE_TXN_INDEX?c:e===lr.ASSET_IN_TXN_INDEX?p:a.default.signLogicSigTransactionObject(t.txn,n.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(){return(Ar=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m,g,x,A,I,h,T,y,v,w;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.quoteAndPool,o=e.swapType,s=e.slippage,i=e.initiatorAddr,c=r.pool,p=r.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new M({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,n.getTransactionParams().do();case 9:return g=t.sent,x=[lt("swap"),o===exports.SwapType.FixedInput?lt("fi"):lt("fo")],A=a.default.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:x,accounts:[i],note:mt.getAppCallTxnNoteWithClientName(N.V1_1),foreignAssets:0==c.asset2ID?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),I=o===exports.SwapType.FixedOutput?tt("positive",s,p.assetInAmount):p.assetInAmount,h=0===l?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:f,amount:I,suggestedParams:g}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:f,assetIndex:l,amount:I,suggestedParams:g}),T=o===exports.SwapType.FixedInput?tt("negative",s,p.assetOutAmount):p.assetOutAmount,y=0===d?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:i,amount:T,suggestedParams:g}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:i,assetIndex:d,amount:T,suggestedParams:g}),v=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:f,amount:A.fee+y.fee,note:O,suggestedParams:g}),w=a.default.assignGroupID([v,A,h,y]),t.abrupt("return",[{txn:w[0],signers:[i]},{txn:w[1],signers:[f]},{txn:w[2],signers:[i]},{txn:w[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ir(t){var e=t.pool,n=t.reserves,r=t.assetIn,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,u,i,c=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,u=n.asset1,i=n.asset2;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,u=n.asset2,i=n.asset1}var p=c*mr/gr,l=i-u*i/(u+(c-p));if(l>i)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:a.assetIn},assetOut:{amount:l,decimals:a.assetOut}},f={round:n.round,assetInID:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Kt(d),priceImpact:Ht(s({inputSupply:u,outputSupply:i},d))};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:f}}}function hr(t){return Tr.apply(this,arguments)}function Tr(){return(Tr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,Ie({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ie({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,s.id===r.asset1ID?(x=c.excessAsset1,A=g.excessAsset1):(x=c.excessAsset2,A=g.excessAsset2),(I=A-x)<0n&&(I=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:s.id,assetOutAmount:BigInt(s.amount)+I,excessAmount:{assetID:s.id,excessAmountForSwap:I,totalExcessAmount:A},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==exports.PoolStatus.READY)throw new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,s,u,i=BigInt(r.amount);if(r.id===e.asset1ID)o=e.asset2ID,s=n.asset2,u=n.asset1;else{if(r.id!==e.asset2ID)throw new D(exports.SwapQuoteErrorType.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,s=n.asset1,u=n.asset2}if(i>u)throw new D(exports.SwapQuoteErrorType.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=s*u/(u-i)-s,p=c*gr/(gr-mr),l=p-c,d=et(a.assetOut,Number(i))/et(a.assetIn,Number(p)),f=et(a.assetOut,Number(u))/et(a.assetIn,Number(s)),m=rt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:o,assetInAmount:p,assetOutID:r.id,assetOutAmount:i,swapFee:Number(l),rate:d,priceImpact:m};return{type:exports.SwapQuoteType.Direct,data:{pool:e,quote:g}}}function vr(t){return wr.apply(this,arguments)}function wr(){return(wr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d,f,m,g,x,A,I;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.signedTxns,o=e.assetIn,s=e.assetOut,i=e.initiatorAddr,t.next=3,Ie({client:n,pool:r,accountAddr:i});case 3:return c=t.sent,t.next=6,st(n,[a]);case 6:return p=t.sent,l=w(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,Ie({client:n,pool:r,accountAddr:i});case 13:return g=t.sent,o.id===r.asset1ID?(x=c.excessAsset1,A=g.excessAsset1):(x=c.excessAsset2,A=g.excessAsset2),(I=A-x)<0n&&(I=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-I,assetOutID:s.id,assetOutAmount:BigInt(s.amount),excessAmount:{assetID:o.id,excessAmountForSwap:I,totalExcessAmount:A},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _r(){return(_r=c(u().mark((function t(e){var n,r,a,o,i,c,p,l,d,f;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.pool,a=e.swapType,o=e.txGroup,i=e.signedTxns,c=e.initiatorAddr,r.status===exports.PoolStatus.READY){t.next=3;break}throw new M({pool:r,swapType:a,txGroup:o},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:o[lr.ASSET_IN_TXN_INDEX].txn.assetIndex||0,amount:o[lr.ASSET_IN_TXN_INDEX].txn.amount},l={id:o[lr.ASSET_OUT_TXN_INDEX].txn.assetIndex||0,amount:o[lr.ASSET_OUT_TXN_INDEX].txn.amount},a!==exports.SwapType.FixedInput){t.next=12;break}return t.next=9,hr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,vr({client:n,pool:r,signedTxns:i,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",s(s({},d),{},{groupID:ct(o),fees:it(o)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new M(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}!function(t){t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX"}(lr||(lr={}));var Sr,Er={getQuote:function(t,e,n,r,a){return t===exports.SwapType.FixedInput?Ir({pool:e,reserves:n,assetIn:r,decimals:a}):yr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Ir,getFixedOutputSwapQuote:yr,generateTxns:function(t){return Ar.apply(this,arguments)},signTxns:function(t){return xr.apply(this,arguments)},execute:function(t){return _r.apply(this,arguments)},executeFixedOutputSwap:vr};function br(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=w(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(re(n)&&!re(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Dr(){return(Dr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,a=e.assetIn,o=e.assetOut,s=e.isSwapRouterEnabled,i=e.pools,c=[],(p=i.find((function(t){return t.info.contractVersion===N.V1_1})))?c.push(new Promise((function(t,e){try{t(Er.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===N.V2})),c.push(or.getFixedInputSwapQuote({amount:r,assetIn:{id:X(a),decimals:a.decimals},assetOut:{id:X(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:s,network:e.network})),t.next=8,br(c);case 8:return d=t.sent,t.abrupt("return",ne(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,a=e.assetIn,o=e.assetOut,s=e.pools,i=e.isSwapRouterEnabled,c=[],(p=s.find((function(t){return t.info.contractVersion===N.V1_1})))?c.push(new Promise((function(t,e){try{t(Er.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new D(exports.SwapQuoteErrorType.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=s.find((function(t){return t.info.contractVersion===N.V2})),c.push(or.getFixedOutputSwapQuote({amount:r,assetIn:{id:X(a),decimals:a.decimals},assetOut:{id:X(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,br(c);case 8:return d=t.sent,t.abrupt("return",ne(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Nr=(f(Sr={},N.V1_1,Er),f(Sr,N.V2,or),f(Sr,"getQuote",(function(t){var e=t.type;if(e===exports.SwapType.FixedInput)return function(t){return Dr.apply(this,arguments)}(t);if(e===exports.SwapType.FixedOutput)return function(t){return Or.apply(this,arguments)}(t);throw new D(exports.SwapQuoteErrorType.InvalidSwapTypeError,"Invalid swap type")})),f(Sr,"generateTxns",(function(t){return t.quote.type===exports.SwapQuoteType.Direct&&te(t.quote)===N.V1_1?Er.generateTxns(s(s({},t),{},{quoteAndPool:t.quote.data})):or.generateTxns(t)})),f(Sr,"signTxns",(function(t){if(t.quote.type===exports.SwapQuoteType.Direct&&te(t.quote)===N.V1_1){var e=t.quote.data.pool;return Er.signTxns(s(s({},t),{},{pool:e}))}return or.signTxns(t)})),f(Sr,"execute",(function(t){return t.contractVersion===N.V1_1?Er.execute(t):or.execute(t)})),Sr);function Pr(){return(Pr=c(u().mark((function t(e){var n,r,a,o,s,i,c,p,l,d;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,a=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,kr({txGroup:a,pool:r,initiatorSigner:o});case 4:return s=t.sent,t.next=7,st(n,[s]);case 7:return i=t.sent,c=w(i,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:it(a),confirmedRound:d,txnID:l,groupID:ct(a)});case 15:throw t.prev=15,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function kr(t){return Lr.apply(this,arguments)}function Lr(){return(Lr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.txGroup,r=e.pool,o=e.initiatorSigner,t.next=3,o([n]);case 3:return s=t.sent,i=w(s,1),c=i[0],p=r.account.lsig,l=n.map((function(t,e){return 0===e?c:a.default.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rr(){return Rr=c(u().mark((function t(e){var n,r,o,s,i,p;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.data,o=e.initiatorSigner,t.prev=1,s=r.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:it(e),groupID:ct(e),lsig:n.account.lsig}})),t.next=5,o(s.map((function(t){return t.txns})));case 5:return i=t.sent,p=Promise.all(s.map((function(t,e){return new Promise(function(){var r=c(u().mark((function r(o,s){var c,p,l,d,f,m;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,c=t.txns.map((function(n,r){return 0===r?i[e]:a.default.signLogicSigTransactionObject(n.txn,t.lsig).blob})),r.next=4,st(n,[c]);case 4:p=r.sent,l=w(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),r.next=15;break;case 12:r.prev=12,r.t0=r.catch(0),s(r.t0);case 15:case"end":return r.stop()}}),r,null,[[0,12]])})));return function(t,e){return r.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new M(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Rr.apply(this,arguments)}function Fr(){return(Fr=c(u().mark((function t(e){var n,r,o,s,i,c,p,l,d,f,m;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,o=e.assetID,s=e.assetOut,i=e.initiatorAddr,t.next=3,n.getTransactionParams().do();case 3:return c=t.sent,p=r.account.address(),l=a.default.makeApplicationNoOpTxnFromObject({from:p,appIndex:r.validatorAppID,appArgs:[lt("redeem")],note:mt.getAppCallTxnNoteWithClientName(r.contractVersion),accounts:[i],foreignAssets:0==r.asset2ID?[r.asset1ID,r.poolTokenID]:[r.asset1ID,r.asset2ID,r.poolTokenID],suggestedParams:c}),d=0===o?a.default.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:i,amount:BigInt(s),suggestedParams:c}):a.default.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:i,assetIndex:o,amount:BigInt(s),suggestedParams:c}),f=a.default.makePaymentTxnWithSuggestedParamsFromObject({from:i,to:p,amount:l.fee+d.fee,note:O,suggestedParams:c}),m=a.default.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[i]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,o=e.liquidityAssetID,s=e.program,u=e.amount,i=t.encodeUint64(u),c=t.encodeUint64(s.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[o],accounts:[s.accountAddress],appArgs:[lt("commit"),i],note:U([lt("tinymanStaking/v1:b"),c,t.encodeUint64(o),i])})}function Mr(){return(Mr=c(u().mark((function e(n){var r,a,o,s,i,c,p,l,d,f,m;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,s=n.requiredAssetID,i=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Br({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:i,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof s){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[lt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=P,exports.ALGO_ASSET_ID=0,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=jn,exports.BASE_MINIMUM_BALANCE=1e5,exports.Bootstrap=pn,exports.CONTRACT_VERSION=N,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=1e3,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=1e5,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=5e4,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=28500,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=k,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=fr,exports.Swap=Nr,exports.SwapQuoteError=D,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=qn,exports.V2AddLiquidityTxnIndices=An,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=2,exports.applySlippageToAmount=tt,exports.calculateAccountMinimumRequiredBalance=me,exports.calculatePriceImpact=Ht,exports.calculateSwapRate=Kt,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),r=0;r=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=Jt,exports.isSwapQuoteErrorCausedByAmount=re,exports.poolUtils=Qe,exports.prepareCommitTransactions=function(t){return Mr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Rr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Pr.apply(this,arguments)},exports.sendAndWaitRawTransaction=st,exports.sumUpTxnFees=it,exports.tinymanContract_v1_1=ue,exports.tinymanContract_v2=pe,exports.tinymanJSSDKConfig=mt; diff --git a/dist/swap/v2/router/swap-router.d.ts b/dist/swap/v2/router/swap-router.d.ts index 96fddc84..5578c1b9 100644 --- a/dist/swap/v2/router/swap-router.d.ts +++ b/dist/swap/v2/router/swap-router.d.ts @@ -1,5 +1,4 @@ import algosdk, { Algodv2, Transaction } from "algosdk"; -import AlgodClient from "algosdk/dist/types/src/client/v2/algod/algod"; import { SupportedNetwork } from "../../../util/commonTypes"; import { SwapType } from "../../constants"; import { SwapRouterResponse, SwapRoute } from "../../types"; @@ -7,7 +6,7 @@ import { SwapRouterResponse, SwapRoute } from "../../types"; * Generates txns that would opt in the Swap Router Application to the assets used in the swap router */ export declare function generateSwapRouterAssetOptInTransaction({ client, routerAppID, assetIDs, initiatorAddr }: { - client: AlgodClient; + client: Algodv2; routerAppID: number; assetIDs: number[]; initiatorAddr: string; @@ -23,7 +22,7 @@ export declare function generateSwapRouterTxns({ initiatorAddr, client, network, signers: string[]; }[]>; export declare function getSwapRouterAppOptInRequiredAssetIDs({ client, network, assetIDs }: { - client: AlgodClient; + client: Algodv2; network: SupportedNetwork; assetIDs: number[]; }): Promise; From 02ee21bce6326fc4526a2cc1c083f1d8f3f7fc70 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Tue, 28 Mar 2023 16:32:30 +0300 Subject: [PATCH 29/39] chore(swap-router): update mainnet app id --- src/swap/v2/router/constants.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/swap/v2/router/constants.ts b/src/swap/v2/router/constants.ts index 9612481c..4a594a93 100644 --- a/src/swap/v2/router/constants.ts +++ b/src/swap/v2/router/constants.ts @@ -3,8 +3,8 @@ import {SwapType} from "../../constants"; export const SWAP_ROUTER_APP_ID: Record = { testnet: 159521633, - // TODO: Update this when the mainnet app is deployed - mainnet: 0 + // This is temporary APP ID for testing only, remove it when the actual mainnet APP ID is deployed + mainnet: 1071281873 }; /** From 87d8ba46d8ebf6e8adb64ccb63908f47e114a996 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Tue, 4 Apr 2023 16:15:18 +0300 Subject: [PATCH 30/39] feat(swap-router): apply slippage amount to swap router transactions --- dist/index.js | 2 +- dist/swap/v2/router/swap-router.d.ts | 3 ++- src/swap/v2/router/swap-router.ts | 17 ++++++++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/dist/index.js b/dist/index.js index dd741bec..d5d714a1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=h(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={V1_1:"v1_1",V2:"v2"},C=0,j={id:"".concat(C),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},V={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},X="- would result negative",Q="logic eval error:",U="exceeds schema integer count",q=/transaction \w+:/,G=function(t){l(n,A(Error));var e=h(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function J(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Y(t){return Number(t)===C}function K(t){return Number(t.id)}function H(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function Z(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var $=100000n,tt=100000n,et=100000n,nt=25000n+25000n,rt=25000n+3500n;function at(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return $+tt*BigInt(o.length)+et*BigInt(s.length+a.length)+rt*r+nt*n}function st(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function ot(t,e){return it.apply(this,arguments)}function it(){return(it=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,st(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ut(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function ct(t,e){var n=Number(t);return pt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function pt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=x(dt(e),2),s=a[0],o=a[1],i=x(dt(Math.round(Number(lt(s,o+r)))),2),u=i[0],c=i[1];return Number(lt(u,c-r))}return Math.round(e)}function lt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function dt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function ft(t,e){return mt.apply(this,arguments)}function mt(){return(mt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,ot(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new G(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function gt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function At(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function It(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ht(t){return(new TextEncoder).encode(t)}var xt,vt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===M.V1_1?"v1":t;return ht("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),Tt=(p(xt={},M.V1_1,{testnet:62368684,mainnet:552635992}),p(xt,M.V2,{testnet:148607e3,mainnet:1002541853}),xt);function yt(t,e){var n=Tt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function wt(){return(wt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var bt,Et,Dt,St=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Ot=.003,Nt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(bt={},St.FixedInput,1),p(bt,St.FixedOutput,2),bt),kt=2,Lt=ht("swap"),Rt=(p(Et={},St.FixedInput,ht("fixed-input")),p(Et,St.FixedOutput,ht("fixed-output")),Et),Ft={ASSET_OPT_IN:ht("asset_opt_in")},Bt={testnet:159521633,mainnet:0},Mt=(p(Dt={},St.FixedInput,7),p(Dt,St.FixedOutput,8),Dt),Ct={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return ct(r.asset.decimals,Number(r.amount))/ct(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Bt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Ft.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v,T,y,w,_,b,E,D;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,e.next=3,s.getTransactionParams().do();case 3:return c=e.sent,p=[K(u[0].quote.amount_in.asset),K(u[0].quote.amount_out.asset),K(u[1].quote.amount_out.asset)],l=p[0],d=p[1],f=p[2],m=[Number(Qt(u).amount),Number(Xt(u).amount)],g=m[0],A=m[1],I=[u[0].pool.address,u[1].pool.address],h=I[0],x=I[1],v=Y(l),T=Vt(o),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(T),amount:g,suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(T),amount:g,assetIndex:l,suggestedParams:c}),(w=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:T,appArgs:[Lt,Rt[i],t.encodeUint64(A)],foreignApps:[yt(o,M.V2)],foreignAssets:[l,d,f],accounts:[h,x],suggestedParams:c})).fee=t.ALGORAND_MIN_TX_FEE*(Mt[i]+1),_=[y,w],e.next=15,Jt({client:s,network:o,assetIDs:[l,d,f]});case 15:if(!((b=e.sent).length>0)){e.next=21;break}return e.next=19,qt({client:s,initiatorAddr:r,assetIDs:b,routerAppID:T});case 19:E=e.sent,_.unshift(E);case 21:return D=t.assignGroupID(_),e.abrupt("return",D.map((function(t){return{txn:t,signers:[r]}})));case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==C&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Ct[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(Pt[e]+kt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Ot;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Ot)}function re(e){return(Pt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return ct(n.decimals,Number(n.amount))/ct(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=ct(a.decimals,Number(n))/ct(r.decimals,Number(e));return pt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:M.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=h(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=yt(n.network,M.V1_1),a=x(J(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?H({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=ht("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=H({stateArray:I}),x=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=h[x],_=h[v],b=h[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,h,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=H({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,yt(s,M.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[M.V1_1].asset1],asset2ID:u[Oe[M.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=xe(M.V2),u=i.generateLogicSigAccountForPool(e),c=yt(r,M.V2),p=u.address(),l=J(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:M.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,yt(s,M.V2)),u=null,i&&(u={asset1ID:i[Oe[M.V2].asset1],asset2ID:i[Oe[M.V2].asset2],poolTokenID:i[Oe[M.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=ct(t.asset1,e.asset1)/ct(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},M.V1_1,r(r({},Me),Ge)),p(qe,M.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=z(o,i),l=x(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,h=Y(A),v=yt(s,M.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:vt.getAppCallTxnNoteWithClientName(M.V1_1),appArgs:[ht("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:V.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(h),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,h||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=x(c,1),l=p[0],d=J(i,u),f=x(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],h=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:h,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,ot(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r){var s;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return s=e.sent,e.abrupt("return",null==s?void 0:s.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&r.push({id:C,amount:a.amt}),r}),[]));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(){return(ln=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=yt(s,M.V2),l=t.getApplicationAddress(p),d=z(o,i),f=x(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=he.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),h=Y(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[ht("bootstrap")],note:vt.getAppCallTxnNoteWithClientName(M.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=fn(h),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:dn(h),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function dn(t){return function(t,e){var n=xe(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(M.V2,t)+fn(t)+O}function fn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function mn(){return(mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=x(c,1),l=p[0],d=J(i,u),f=x(d,2),m=f[0],g=f[1],A=he.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],h=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:h,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(){return(gn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new G(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(t){return ln.apply(this,arguments)},signTxns:function(t){return mn.apply(this,arguments)},execute:function(t){return gn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+dn(e)}};var hn,xn,vn,Tn,yn=(p(An={},M.V1_1,en),p(An,M.V2,In),p(An,"generateTxns",(function(t){return t.contractVersion===M.V1_1?en.generateTxns(t):In.generateTxns(t)})),p(An,"signTxns",(function(t){return t.contractVersion===M.V1_1?en.signTxns(t):In.signTxns(t)})),p(An,"execute",(function(t){return t.contractVersion===M.V1_1?en.execute(t):In.execute(t)})),p(An,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===M.V1_1?en.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),wn=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),_n=5*t.ALGORAND_MIN_TX_FEE,bn=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),En=(p(hn={},bn.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(hn,bn.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(hn,bn.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),hn),Dn=(p(xn={},bn.INITIAL,1),p(xn,bn.SINGLE,2),p(xn,bn.FLEXIBLE,2),xn),Sn=(p(vn={},bn.INITIAL,3),p(vn,bn.FLEXIBLE,3),p(vn,bn.SINGLE,2),vn);function On(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var h=A;r=Pn(h,s),e={id:o.id,amount:h+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var x=I;r=Pn(x,s),e={id:i.id,amount:x+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Nn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function Pn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function kn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Rn=(p(Tn={},M.V1_1,[ht("mint")]),p(Tn,M.V2,{INITIAL_LIQUIDITY:[ht("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,ht("single")],FLEXIBLE_MODE:[Ln,ht("flexible")]}),Tn);function Fn(){return(Fn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ut("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:yt(s,M.V1_1),appArgs:Rn.v1_1,accounts:[l],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:u.id==C?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),h=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),x=t.assignGroupID([h,m,g,A,I]),e.abrupt("return",[{txn:x[0],signers:[l]},{txn:x[1],signers:[o]},{txn:x[2],signers:[l]},{txn:x[3],signers:[l]},{txn:x[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=x(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===wn.FEE_TXN?p:n===wn.ASSET1_IN_TXN?l:n===wn.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[wn.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,ft(n,[o]);case 8:return p=t.sent,l=x(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=gt(s),A=At(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(h=I.excessPoolTokens-c.excessPoolTokens)<0n&&(h=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+h,excessAmount:{excessAmountForAddingLiquidity:h,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new G(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Cn=Object.freeze({__proto__:null,execute:function(t){return Mn.apply(this,arguments)},generateTxns:function(t){return Fn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:ct(p,Number(m))/ct(u,Number(l)),priceImpact:A}}function lr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:ct(p,Number(l))/ct(u,Number(m)),priceImpact:A}}}}function dr(t){return fr.apply(this,arguments)}function fr(){return(fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=pr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,cr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(lr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,cr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var Ir={getQuote:function(t){return ur.apply(this,arguments)},getFixedInputSwapQuote:dr,getFixedInputDirectSwapQuote:pr,getFixedOutputDirectSwapQuote:lr,getFixedOutputSwapQuote:mr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ir.apply(this,arguments)},calculateFixedInputSwap:Ar};function hr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function xr(){return(xr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[tr,t.encodeUint64(ut("negative",p,u)),t.encodeUint64(ut("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[er.ASSET_TRANSFER_TXN]=m,A[er.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[er.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[er.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,h=ut("negative",p,c),u!==d){e.next=17;break}A=h,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return x=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[tr,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[er.ASSET_TRANSFER_TXN]=x,T[er.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[er.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[er.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,t.next=3,ft(n,[s]);case 3:return o=t.sent,i=x(o,1),u=i[0].txnID,t.prev=6,t.next=9,cn(n,r);case 9:c=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var yr,wr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=hr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=hr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ir.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=Ir.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,h=A.totalFeeAmount,x=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:h},priceImpact:x}}}return e},generateTxns:function(t){return xr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Tr.apply(this,arguments)}},_r=(p(yr={},M.V1_1,sr),p(yr,M.V2,wr),yr),br=3n,Er=1000n,Dr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Dr||{});function Sr(){return(Sr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=x(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Dr.FEE_TXN_INDEX?c:n===Dr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new G({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[ht("swap"),o===St.FixedInput?ht("fi"):ht("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:c.asset2ID==C?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),h=o===St.FixedOutput?ut("positive",i,p.assetInAmount):p.assetInAmount,x=l===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,suggestedParams:g}),v=o===St.FixedInput?ut("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,x,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*br/Er,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function Pr(t){return kr.apply(this,arguments)}function kr(){return(kr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,h;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=x(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(h=I-A)<0n&&(h=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+h,excessAmount:{assetID:i.id,excessAmountForSwap:h,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Er/(Er-br),l=p-c,d=ct(a.assetOut,Number(u))/ct(a.assetIn,Number(p)),f=ct(a.assetOut,Number(i))/ct(a.assetIn,Number(o)),m=pt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Rr(t){return Fr.apply(this,arguments)}function Fr(){return(Fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,h;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=x(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(h=I-A)<0n&&(h=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-h,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:h,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new G({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Dr.ASSET_IN_TXN_INDEX].txn.assetIndex||C,amount:i[Dr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Dr.ASSET_OUT_TXN_INDEX].txn.assetIndex||C,amount:i[Dr.ASSET_OUT_TXN_INDEX].txn.amount},o!==St.FixedInput){t.next=12;break}return t.next=9,Pr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Rr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:At(i),fees:gt(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new G(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Mr,Cr={getQuote:function(t,e,n,r,a){return t===St.FixedInput?Nr({pool:e,reserves:n,assetIn:r,decimals:a}):Lr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Lr,generateTxns:function(t){return Or.apply(this,arguments)},signTxns:function(t){return Sr.apply(this,arguments)},execute:function(t){return Br.apply(this,arguments)},executeFixedOutputSwap:Rr};function jr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=x(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Vr(){return(Vr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===M.V2})),c.push(Ir.getFixedInputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===M.V2})),c.push(Ir.getFixedOutputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qr=(p(Mr={},M.V1_1,Cr),p(Mr,M.V2,Ir),p(Mr,"getQuote",(function(t){var e=t.type;if(e===St.FixedInput)return function(t){return Vr.apply(this,arguments)}(t);if(e===St.FixedOutput)return function(t){return Xr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Mr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1?Cr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):Ir.generateTxns(t)})),p(Mr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1){var e=t.quote.data.pool;return Cr.signTxns(r(r({},t),{},{pool:e}))}return Ir.signTxns(t)})),p(Mr,"execute",(function(t){return t.contractVersion===M.V1_1?Cr.execute(t):Ir.execute(t)})),Mr);function Ur(){return(Ur=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,qr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,ft(n,[i]);case 7:return u=t.sent,c=x(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:gt(s),confirmedRound:d,txnID:l,groupID:At(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function qr(t){return Gr.apply(this,arguments)}function Gr(){return(Gr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=x(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wr(){return Wr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:gt(e),groupID:At(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,ft(r,[u]);case 4:p=a.sent,l=x(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new G(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[ht("redeem")],note:vt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ht("commit"),u],note:Z([ht("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Yr(){return(Yr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Jr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[ht("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=j,exports.ALGO_ASSET_ID=C,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Kn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=yn,exports.CONTRACT_VERSION=M,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=V,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=_r,exports.Swap=Qr,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=St,exports.V1_1AddLiquidityTxnIndices=wn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Zn,exports.V2AddLiquidityTxnIndices=En,exports.V2AddLiquidityType=bn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=$n,exports.applySlippageToAmount=ut,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Ur.apply(this,arguments)},exports.sendAndWaitRawTransaction=ft,exports.sumUpTxnFees=gt,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=he,exports.tinymanJSSDKConfig=vt; +"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={V1_1:"v1_1",V2:"v2"},C=0,j={id:"".concat(C),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},V={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},X="- would result negative",Q="logic eval error:",U="exceeds schema integer count",q=/transaction \w+:/,G=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function J(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Y(t){return Number(t)===C}function K(t){return Number(t.id)}function H(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function Z(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var $=100000n,tt=100000n,et=100000n,nt=25000n+25000n,rt=25000n+3500n;function at(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return $+tt*BigInt(o.length)+et*BigInt(s.length+a.length)+rt*r+nt*n}function st(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function ot(t,e){return it.apply(this,arguments)}function it(){return(it=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,st(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ut(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function ct(t,e){var n=Number(t);return pt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function pt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=h(dt(e),2),s=a[0],o=a[1],i=h(dt(Math.round(Number(lt(s,o+r)))),2),u=i[0],c=i[1];return Number(lt(u,c-r))}return Math.round(e)}function lt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function dt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function ft(t,e){return mt.apply(this,arguments)}function mt(){return(mt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,ot(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new G(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function gt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function At(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function It(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function xt(t){return(new TextEncoder).encode(t)}var ht,vt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===M.V1_1?"v1":t;return xt("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),Tt=(p(ht={},M.V1_1,{testnet:62368684,mainnet:552635992}),p(ht,M.V2,{testnet:148607e3,mainnet:1002541853}),ht);function yt(t,e){var n=Tt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function wt(){return(wt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var bt,Et,Dt,St=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Ot=.003,Nt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(bt={},St.FixedInput,1),p(bt,St.FixedOutput,2),bt),kt=2,Lt=xt("swap"),Rt=(p(Et={},St.FixedInput,xt("fixed-input")),p(Et,St.FixedOutput,xt("fixed-output")),Et),Ft={ASSET_OPT_IN:xt("asset_opt_in")},Bt={testnet:159521633,mainnet:1071281873},Mt=(p(Dt={},St.FixedInput,7),p(Dt,St.FixedOutput,8),Dt),Ct={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return ct(r.asset.decimals,Number(r.amount))/ct(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Bt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Ft.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[K(u[0].quote.amount_in.asset),K(u[0].quote.amount_out.asset),K(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],x=[u[0].pool.address,u[1].pool.address],h=x[0],v=x[1],T=i===St.FixedInput?A:ut("positive",c,A),y=i===St.FixedOutput?I:ut("negative",c,I),w=Y(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Lt,Rt[i],t.encodeUint64(y)],foreignApps:[yt(o,M.V2)],foreignAssets:[d,f,m],accounts:[h,v],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Mt[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==C&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Ct[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(Pt[e]+kt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Ot;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Ot)}function re(e){return(Pt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return ct(n.decimals,Number(n.amount))/ct(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=ct(a.decimals,Number(n))/ct(r.decimals,Number(e));return pt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:M.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=x(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=yt(n.network,M.V1_1),a=h(J(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?H({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=xt("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:x=H({stateArray:I}),h=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=x[h],_=x[v],b=x[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=H({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,yt(s,M.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[M.V1_1].asset1],asset2ID:u[Oe[M.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=he(M.V2),u=i.generateLogicSigAccountForPool(e),c=yt(r,M.V2),p=u.address(),l=J(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:M.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,yt(s,M.V2)),u=null,i&&(u={asset1ID:i[Oe[M.V2].asset1],asset2ID:i[Oe[M.V2].asset2],poolTokenID:i[Oe[M.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=ct(t.asset1,e.asset1)/ct(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},M.V1_1,r(r({},Me),Ge)),p(qe,M.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=z(o,i),l=h(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,x=Y(A),v=yt(s,M.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:vt.getAppCallTxnNoteWithClientName(M.V1_1),appArgs:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:x?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:V.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(x),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,x||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=J(i,u),f=h(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,ot(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r){var s;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return s=e.sent,e.abrupt("return",null==s?void 0:s.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&r.push({id:C,amount:a.amt}),r}),[]));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(){return(ln=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=yt(s,M.V2),l=t.getApplicationAddress(p),d=z(o,i),f=h(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),x=Y(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:vt.getAppCallTxnNoteWithClientName(M.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=fn(x),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:dn(x),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function dn(t){return function(t,e){var n=he(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(M.V2,t)+fn(t)+O}function fn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function mn(){return(mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=J(i,u),f=h(d,2),m=f[0],g=f[1],A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(){return(gn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new G(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(t){return ln.apply(this,arguments)},signTxns:function(t){return mn.apply(this,arguments)},execute:function(t){return gn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+dn(e)}};var xn,hn,vn,Tn,yn=(p(An={},M.V1_1,en),p(An,M.V2,In),p(An,"generateTxns",(function(t){return t.contractVersion===M.V1_1?en.generateTxns(t):In.generateTxns(t)})),p(An,"signTxns",(function(t){return t.contractVersion===M.V1_1?en.signTxns(t):In.signTxns(t)})),p(An,"execute",(function(t){return t.contractVersion===M.V1_1?en.execute(t):In.execute(t)})),p(An,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===M.V1_1?en.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),wn=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),_n=5*t.ALGORAND_MIN_TX_FEE,bn=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),En=(p(xn={},bn.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,bn.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,bn.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Dn=(p(hn={},bn.INITIAL,1),p(hn,bn.SINGLE,2),p(hn,bn.FLEXIBLE,2),hn),Sn=(p(vn={},bn.INITIAL,3),p(vn,bn.FLEXIBLE,3),p(vn,bn.SINGLE,2),vn);function On(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var x=A;r=Pn(x,s),e={id:o.id,amount:x+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=Pn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Nn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function Pn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function kn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Rn=(p(Tn={},M.V1_1,[xt("mint")]),p(Tn,M.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Fn(){return(Fn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ut("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:yt(s,M.V1_1),appArgs:Rn.v1_1,accounts:[l],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:u.id==C?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),x=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),h=t.assignGroupID([x,m,g,A,I]),e.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=h(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===wn.FEE_TXN?p:n===wn.ASSET1_IN_TXN?l:n===wn.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[wn.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,ft(n,[o]);case 8:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=gt(s),A=At(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(x=I.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new G(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Cn=Object.freeze({__proto__:null,execute:function(t){return Mn.apply(this,arguments)},generateTxns:function(t){return Fn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:ct(p,Number(m))/ct(u,Number(l)),priceImpact:A}}function lr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:ct(p,Number(l))/ct(u,Number(m)),priceImpact:A}}}}function dr(t){return fr.apply(this,arguments)}function fr(){return(fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=pr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,cr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(lr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,cr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var Ir={getQuote:function(t){return ur.apply(this,arguments)},getFixedInputSwapQuote:dr,getFixedInputDirectSwapQuote:pr,getFixedOutputDirectSwapQuote:lr,getFixedOutputSwapQuote:mr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ir.apply(this,arguments)},calculateFixedInputSwap:Ar};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function hr(){return(hr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[tr,t.encodeUint64(ut("negative",p,u)),t.encodeUint64(ut("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[er.ASSET_TRANSFER_TXN]=m,A[er.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[er.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[er.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,x=ut("negative",p,c),u!==d){e.next=17;break}A=x,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=x,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return h=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[tr,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[er.ASSET_TRANSFER_TXN]=h,T[er.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[er.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[er.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,t.next=3,ft(n,[s]);case 3:return o=t.sent,i=h(o,1),u=i[0].txnID,t.prev=6,t.next=9,cn(n,r);case 9:c=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var yr,wr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ir.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=Ir.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,x=A.totalFeeAmount,h=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:h}}}return e},generateTxns:function(t){return hr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Tr.apply(this,arguments)}},_r=(p(yr={},M.V1_1,sr),p(yr,M.V2,wr),yr),br=3n,Er=1000n,Dr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Dr||{});function Sr(){return(Sr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=h(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Dr.FEE_TXN_INDEX?c:n===Dr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new G({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[xt("swap"),o===St.FixedInput?xt("fi"):xt("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:c.asset2ID==C?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),x=o===St.FixedOutput?ut("positive",i,p.assetInAmount):p.assetInAmount,h=l===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:x,suggestedParams:g}),v=o===St.FixedInput?ut("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,h,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*br/Er,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function Pr(t){return kr.apply(this,arguments)}function kr(){return(kr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+x,excessAmount:{assetID:i.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Er/(Er-br),l=p-c,d=ct(a.assetOut,Number(u))/ct(a.assetIn,Number(p)),f=ct(a.assetOut,Number(i))/ct(a.assetIn,Number(o)),m=pt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Rr(t){return Fr.apply(this,arguments)}function Fr(){return(Fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-x,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new G({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Dr.ASSET_IN_TXN_INDEX].txn.assetIndex||C,amount:i[Dr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Dr.ASSET_OUT_TXN_INDEX].txn.assetIndex||C,amount:i[Dr.ASSET_OUT_TXN_INDEX].txn.amount},o!==St.FixedInput){t.next=12;break}return t.next=9,Pr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Rr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:At(i),fees:gt(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new G(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Mr,Cr={getQuote:function(t,e,n,r,a){return t===St.FixedInput?Nr({pool:e,reserves:n,assetIn:r,decimals:a}):Lr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Lr,generateTxns:function(t){return Or.apply(this,arguments)},signTxns:function(t){return Sr.apply(this,arguments)},execute:function(t){return Br.apply(this,arguments)},executeFixedOutputSwap:Rr};function jr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=h(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Vr(){return(Vr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===M.V2})),c.push(Ir.getFixedInputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===M.V2})),c.push(Ir.getFixedOutputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qr=(p(Mr={},M.V1_1,Cr),p(Mr,M.V2,Ir),p(Mr,"getQuote",(function(t){var e=t.type;if(e===St.FixedInput)return function(t){return Vr.apply(this,arguments)}(t);if(e===St.FixedOutput)return function(t){return Xr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Mr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1?Cr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):Ir.generateTxns(t)})),p(Mr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1){var e=t.quote.data.pool;return Cr.signTxns(r(r({},t),{},{pool:e}))}return Ir.signTxns(t)})),p(Mr,"execute",(function(t){return t.contractVersion===M.V1_1?Cr.execute(t):Ir.execute(t)})),Mr);function Ur(){return(Ur=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,qr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,ft(n,[i]);case 7:return u=t.sent,c=h(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:gt(s),confirmedRound:d,txnID:l,groupID:At(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function qr(t){return Gr.apply(this,arguments)}function Gr(){return(Gr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=h(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wr(){return Wr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:gt(e),groupID:At(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,ft(r,[u]);case 4:p=a.sent,l=h(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new G(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[xt("redeem")],note:vt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[xt("commit"),u],note:Z([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Yr(){return(Yr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Jr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[xt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=j,exports.ALGO_ASSET_ID=C,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Kn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=yn,exports.CONTRACT_VERSION=M,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=V,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=_r,exports.Swap=Qr,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=St,exports.V1_1AddLiquidityTxnIndices=wn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Zn,exports.V2AddLiquidityTxnIndices=En,exports.V2AddLiquidityType=bn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=$n,exports.applySlippageToAmount=ut,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Ur.apply(this,arguments)},exports.sendAndWaitRawTransaction=ft,exports.sumUpTxnFees=gt,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=xe,exports.tinymanJSSDKConfig=vt; diff --git a/dist/swap/v2/router/swap-router.d.ts b/dist/swap/v2/router/swap-router.d.ts index 5578c1b9..1d864361 100644 --- a/dist/swap/v2/router/swap-router.d.ts +++ b/dist/swap/v2/router/swap-router.d.ts @@ -11,12 +11,13 @@ export declare function generateSwapRouterAssetOptInTransaction({ client, router assetIDs: number[]; initiatorAddr: string; }): Promise; -export declare function generateSwapRouterTxns({ initiatorAddr, client, network, swapType, route }: { +export declare function generateSwapRouterTxns({ initiatorAddr, client, network, swapType, route, slippage }: { client: Algodv2; initiatorAddr: string; swapType: SwapType; route: SwapRoute; network: SupportedNetwork; + slippage: number; }): Promise<{ txn: algosdk.Transaction; signers: string[]; diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index bd1507a9..71fab0d8 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -11,7 +11,7 @@ import {ALGO_ASSET_ID} from "../../../util/asset/assetConstants"; import {getAssetId, isAlgo} from "../../../util/asset/assetUtils"; import {SupportedNetwork} from "../../../util/commonTypes"; import SwapQuoteError, {SwapQuoteErrorType} from "../../../util/error/SwapQuoteError"; -import {hasTinymanApiErrorShape} from "../../../util/util"; +import {applySlippageToAmount, hasTinymanApiErrorShape} from "../../../util/util"; import {getValidatorAppID} from "../../../validator"; import {SwapType} from "../../constants"; import {FetchSwapRouteQuotesPayload, SwapRouterResponse, SwapRoute} from "../../types"; @@ -67,13 +67,15 @@ export async function generateSwapRouterTxns({ client, network, swapType, - route + route, + slippage }: { client: Algodv2; initiatorAddr: string; swapType: SwapType; route: SwapRoute; network: SupportedNetwork; + slippage: number; }) { const suggestedParams = await client.getTransactionParams().do(); @@ -82,12 +84,21 @@ export async function generateSwapRouterTxns({ getAssetId(route[0].quote.amount_out.asset), getAssetId(route[1].quote.amount_out.asset) ]; - const [assetInAmount, assetOutAmount] = [ + const [assetInAmountFromRoute, assetOutAmountFromRoute] = [ Number(getAssetInFromSwapRoute(route).amount), Number(getAssetOutFromSwapRoute(route).amount) ]; const [pool1Address, pool2Address] = [route[0].pool.address, route[1].pool.address]; + const assetInAmount = + swapType === SwapType.FixedInput + ? assetInAmountFromRoute + : applySlippageToAmount("positive", slippage, assetInAmountFromRoute); + const assetOutAmount = + swapType === SwapType.FixedOutput + ? assetOutAmountFromRoute + : applySlippageToAmount("negative", slippage, assetOutAmountFromRoute); + const isAssetInAlgo = isAlgo(assetInID); const routerAppID = getSwapRouterAppID(network); From d23c7ee9257ed4315ba3cd20720f0eb8fa74a9e5 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Tue, 4 Apr 2023 17:20:03 +0300 Subject: [PATCH 31/39] feat: filter by account address while getting inner txn asset data --- dist/add-liquidity/v2/common.d.ts | 3 ++- dist/index.js | 2 +- dist/remove-liquidity/index.d.ts | 3 ++- dist/remove-liquidity/v2/index.d.ts | 3 ++- dist/swap/index.d.ts | 3 ++- dist/swap/types.d.ts | 1 + dist/swap/v2/index.d.ts | 3 ++- dist/util/commonTypes.d.ts | 2 ++ dist/util/transaction/transactionUtils.d.ts | 2 +- src/add-liquidity/v2/common.ts | 6 ++++-- src/remove-liquidity/v2/index.ts | 6 ++++-- src/swap/types.ts | 1 + src/swap/v2/index.ts | 6 ++++-- src/util/commonTypes.ts | 2 ++ src/util/transaction/transactionUtils.ts | 15 +++++++++++---- 15 files changed, 41 insertions(+), 17 deletions(-) diff --git a/dist/add-liquidity/v2/common.d.ts b/dist/add-liquidity/v2/common.d.ts index a1731094..8a5bf567 100644 --- a/dist/add-liquidity/v2/common.d.ts +++ b/dist/add-liquidity/v2/common.d.ts @@ -13,9 +13,10 @@ export declare function signTxns({ txGroup, initiatorSigner }: { * @param params.pool Information for the pool. * @param params.txGroup The transaction group to execute. */ -export declare function execute({ client, pool, txGroup, signedTxns }: { +export declare function execute({ client, pool, txGroup, signedTxns, accountAddress }: { client: Algodv2; pool: V2PoolInfo; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; + accountAddress: string; }): Promise; diff --git a/dist/index.js b/dist/index.js index d5d714a1..b09fb919 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={V1_1:"v1_1",V2:"v2"},C=0,j={id:"".concat(C),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},V={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},X="- would result negative",Q="logic eval error:",U="exceeds schema integer count",q=/transaction \w+:/,G=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function J(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Y(t){return Number(t)===C}function K(t){return Number(t.id)}function H(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function Z(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var $=100000n,tt=100000n,et=100000n,nt=25000n+25000n,rt=25000n+3500n;function at(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return $+tt*BigInt(o.length)+et*BigInt(s.length+a.length)+rt*r+nt*n}function st(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function ot(t,e){return it.apply(this,arguments)}function it(){return(it=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,st(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ut(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function ct(t,e){var n=Number(t);return pt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function pt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=h(dt(e),2),s=a[0],o=a[1],i=h(dt(Math.round(Number(lt(s,o+r)))),2),u=i[0],c=i[1];return Number(lt(u,c-r))}return Math.round(e)}function lt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function dt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function ft(t,e){return mt.apply(this,arguments)}function mt(){return(mt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,ot(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new G(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function gt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function At(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function It(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function xt(t){return(new TextEncoder).encode(t)}var ht,vt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===M.V1_1?"v1":t;return xt("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),Tt=(p(ht={},M.V1_1,{testnet:62368684,mainnet:552635992}),p(ht,M.V2,{testnet:148607e3,mainnet:1002541853}),ht);function yt(t,e){var n=Tt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function wt(){return(wt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var bt,Et,Dt,St=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Ot=.003,Nt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(bt={},St.FixedInput,1),p(bt,St.FixedOutput,2),bt),kt=2,Lt=xt("swap"),Rt=(p(Et={},St.FixedInput,xt("fixed-input")),p(Et,St.FixedOutput,xt("fixed-output")),Et),Ft={ASSET_OPT_IN:xt("asset_opt_in")},Bt={testnet:159521633,mainnet:1071281873},Mt=(p(Dt={},St.FixedInput,7),p(Dt,St.FixedOutput,8),Dt),Ct={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return ct(r.asset.decimals,Number(r.amount))/ct(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Bt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Ft.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[K(u[0].quote.amount_in.asset),K(u[0].quote.amount_out.asset),K(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],x=[u[0].pool.address,u[1].pool.address],h=x[0],v=x[1],T=i===St.FixedInput?A:ut("positive",c,A),y=i===St.FixedOutput?I:ut("negative",c,I),w=Y(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Lt,Rt[i],t.encodeUint64(y)],foreignApps:[yt(o,M.V2)],foreignAssets:[d,f,m],accounts:[h,v],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Mt[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==C&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Ct[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(Pt[e]+kt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Ot;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Ot)}function re(e){return(Pt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return ct(n.decimals,Number(n.amount))/ct(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=ct(a.decimals,Number(n))/ct(r.decimals,Number(e));return pt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:M.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=x(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=yt(n.network,M.V1_1),a=h(J(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?H({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=xt("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:x=H({stateArray:I}),h=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=x[h],_=x[v],b=x[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=H({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,yt(s,M.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[M.V1_1].asset1],asset2ID:u[Oe[M.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=he(M.V2),u=i.generateLogicSigAccountForPool(e),c=yt(r,M.V2),p=u.address(),l=J(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:M.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,yt(s,M.V2)),u=null,i&&(u={asset1ID:i[Oe[M.V2].asset1],asset2ID:i[Oe[M.V2].asset2],poolTokenID:i[Oe[M.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=ct(t.asset1,e.asset1)/ct(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},M.V1_1,r(r({},Me),Ge)),p(qe,M.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=z(o,i),l=h(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,x=Y(A),v=yt(s,M.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:vt.getAppCallTxnNoteWithClientName(M.V1_1),appArgs:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:x?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:V.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(x),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,x||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=J(i,u),f=h(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,ot(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r){var s;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return s=e.sent,e.abrupt("return",null==s?void 0:s.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&r.push({id:C,amount:a.amt}),r}),[]));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(){return(ln=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=yt(s,M.V2),l=t.getApplicationAddress(p),d=z(o,i),f=h(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),x=Y(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:vt.getAppCallTxnNoteWithClientName(M.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=fn(x),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:dn(x),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function dn(t){return function(t,e){var n=he(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(M.V2,t)+fn(t)+O}function fn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function mn(){return(mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=J(i,u),f=h(d,2),m=f[0],g=f[1],A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(){return(gn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new G(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(t){return ln.apply(this,arguments)},signTxns:function(t){return mn.apply(this,arguments)},execute:function(t){return gn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+dn(e)}};var xn,hn,vn,Tn,yn=(p(An={},M.V1_1,en),p(An,M.V2,In),p(An,"generateTxns",(function(t){return t.contractVersion===M.V1_1?en.generateTxns(t):In.generateTxns(t)})),p(An,"signTxns",(function(t){return t.contractVersion===M.V1_1?en.signTxns(t):In.signTxns(t)})),p(An,"execute",(function(t){return t.contractVersion===M.V1_1?en.execute(t):In.execute(t)})),p(An,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===M.V1_1?en.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),wn=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),_n=5*t.ALGORAND_MIN_TX_FEE,bn=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),En=(p(xn={},bn.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,bn.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,bn.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Dn=(p(hn={},bn.INITIAL,1),p(hn,bn.SINGLE,2),p(hn,bn.FLEXIBLE,2),hn),Sn=(p(vn={},bn.INITIAL,3),p(vn,bn.FLEXIBLE,3),p(vn,bn.SINGLE,2),vn);function On(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var x=A;r=Pn(x,s),e={id:o.id,amount:x+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=Pn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Nn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function Pn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function kn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Rn=(p(Tn={},M.V1_1,[xt("mint")]),p(Tn,M.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Fn(){return(Fn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ut("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:yt(s,M.V1_1),appArgs:Rn.v1_1,accounts:[l],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:u.id==C?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),x=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),h=t.assignGroupID([x,m,g,A,I]),e.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=h(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===wn.FEE_TXN?p:n===wn.ASSET1_IN_TXN?l:n===wn.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[wn.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,ft(n,[o]);case 8:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=gt(s),A=At(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(x=I.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new G(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Cn=Object.freeze({__proto__:null,execute:function(t){return Mn.apply(this,arguments)},generateTxns:function(t){return Fn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:ct(p,Number(m))/ct(u,Number(l)),priceImpact:A}}function lr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:ct(p,Number(l))/ct(u,Number(m)),priceImpact:A}}}}function dr(t){return fr.apply(this,arguments)}function fr(){return(fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=pr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,cr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(lr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,cr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var Ir={getQuote:function(t){return ur.apply(this,arguments)},getFixedInputSwapQuote:dr,getFixedInputDirectSwapQuote:pr,getFixedOutputDirectSwapQuote:lr,getFixedOutputSwapQuote:mr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ir.apply(this,arguments)},calculateFixedInputSwap:Ar};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function hr(){return(hr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[tr,t.encodeUint64(ut("negative",p,u)),t.encodeUint64(ut("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[er.ASSET_TRANSFER_TXN]=m,A[er.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[er.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[er.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,x=ut("negative",p,c),u!==d){e.next=17;break}A=x,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=x,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return h=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[tr,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[er.ASSET_TRANSFER_TXN]=h,T[er.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[er.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[er.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,t.next=3,ft(n,[s]);case 3:return o=t.sent,i=h(o,1),u=i[0].txnID,t.prev=6,t.next=9,cn(n,r);case 9:c=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var yr,wr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ir.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=Ir.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,x=A.totalFeeAmount,h=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:h}}}return e},generateTxns:function(t){return hr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Tr.apply(this,arguments)}},_r=(p(yr={},M.V1_1,sr),p(yr,M.V2,wr),yr),br=3n,Er=1000n,Dr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Dr||{});function Sr(){return(Sr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=h(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Dr.FEE_TXN_INDEX?c:n===Dr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new G({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[xt("swap"),o===St.FixedInput?xt("fi"):xt("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:c.asset2ID==C?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),x=o===St.FixedOutput?ut("positive",i,p.assetInAmount):p.assetInAmount,h=l===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:x,suggestedParams:g}),v=o===St.FixedInput?ut("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,h,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*br/Er,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function Pr(t){return kr.apply(this,arguments)}function kr(){return(kr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+x,excessAmount:{assetID:i.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Er/(Er-br),l=p-c,d=ct(a.assetOut,Number(u))/ct(a.assetIn,Number(p)),f=ct(a.assetOut,Number(i))/ct(a.assetIn,Number(o)),m=pt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Rr(t){return Fr.apply(this,arguments)}function Fr(){return(Fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-x,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new G({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Dr.ASSET_IN_TXN_INDEX].txn.assetIndex||C,amount:i[Dr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Dr.ASSET_OUT_TXN_INDEX].txn.assetIndex||C,amount:i[Dr.ASSET_OUT_TXN_INDEX].txn.amount},o!==St.FixedInput){t.next=12;break}return t.next=9,Pr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Rr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:At(i),fees:gt(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new G(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Mr,Cr={getQuote:function(t,e,n,r,a){return t===St.FixedInput?Nr({pool:e,reserves:n,assetIn:r,decimals:a}):Lr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Lr,generateTxns:function(t){return Or.apply(this,arguments)},signTxns:function(t){return Sr.apply(this,arguments)},execute:function(t){return Br.apply(this,arguments)},executeFixedOutputSwap:Rr};function jr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=h(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Vr(){return(Vr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===M.V2})),c.push(Ir.getFixedInputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===M.V2})),c.push(Ir.getFixedOutputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qr=(p(Mr={},M.V1_1,Cr),p(Mr,M.V2,Ir),p(Mr,"getQuote",(function(t){var e=t.type;if(e===St.FixedInput)return function(t){return Vr.apply(this,arguments)}(t);if(e===St.FixedOutput)return function(t){return Xr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Mr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1?Cr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):Ir.generateTxns(t)})),p(Mr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1){var e=t.quote.data.pool;return Cr.signTxns(r(r({},t),{},{pool:e}))}return Ir.signTxns(t)})),p(Mr,"execute",(function(t){return t.contractVersion===M.V1_1?Cr.execute(t):Ir.execute(t)})),Mr);function Ur(){return(Ur=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,qr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,ft(n,[i]);case 7:return u=t.sent,c=h(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:gt(s),confirmedRound:d,txnID:l,groupID:At(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function qr(t){return Gr.apply(this,arguments)}function Gr(){return(Gr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=h(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wr(){return Wr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:gt(e),groupID:At(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,ft(r,[u]);case 4:p=a.sent,l=h(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new G(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[xt("redeem")],note:vt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[xt("commit"),u],note:Z([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Yr(){return(Yr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Jr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[xt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=j,exports.ALGO_ASSET_ID=C,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Kn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=yn,exports.CONTRACT_VERSION=M,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=V,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=_r,exports.Swap=Qr,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=St,exports.V1_1AddLiquidityTxnIndices=wn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Zn,exports.V2AddLiquidityTxnIndices=En,exports.V2AddLiquidityType=bn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=$n,exports.applySlippageToAmount=ut,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Ur.apply(this,arguments)},exports.sendAndWaitRawTransaction=ft,exports.sumUpTxnFees=gt,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=xe,exports.tinymanJSSDKConfig=vt; +"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={V1_1:"v1_1",V2:"v2"},C=0,j={id:"".concat(C),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},V={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},X="- would result negative",Q="logic eval error:",U="exceeds schema integer count",q=/transaction \w+:/,G=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function J(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Y(t){return Number(t)===C}function K(t){return Number(t.id)}function H(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function Z(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var $=100000n,tt=100000n,et=100000n,nt=25000n+25000n,rt=25000n+3500n;function at(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return $+tt*BigInt(o.length)+et*BigInt(s.length+a.length)+rt*r+nt*n}function st(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function ot(t,e){return it.apply(this,arguments)}function it(){return(it=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,st(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ut(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function ct(t,e){var n=Number(t);return pt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function pt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=h(dt(e),2),s=a[0],o=a[1],i=h(dt(Math.round(Number(lt(s,o+r)))),2),u=i[0],c=i[1];return Number(lt(u,c-r))}return Math.round(e)}function lt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function dt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function ft(t,e){return mt.apply(this,arguments)}function mt(){return(mt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,ot(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new G(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function gt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function At(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function It(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function xt(t){return(new TextEncoder).encode(t)}var ht,vt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===M.V1_1?"v1":t;return xt("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),Tt=(p(ht={},M.V1_1,{testnet:62368684,mainnet:552635992}),p(ht,M.V2,{testnet:148607e3,mainnet:1002541853}),ht);function yt(t,e){var n=Tt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function wt(){return(wt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var bt,Et,Dt,St=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Ot=.003,Nt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(bt={},St.FixedInput,1),p(bt,St.FixedOutput,2),bt),kt=2,Lt=xt("swap"),Rt=(p(Et={},St.FixedInput,xt("fixed-input")),p(Et,St.FixedOutput,xt("fixed-output")),Et),Ft={ASSET_OPT_IN:xt("asset_opt_in")},Bt={testnet:159521633,mainnet:1071281873},Mt=(p(Dt={},St.FixedInput,7),p(Dt,St.FixedOutput,8),Dt),Ct={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return ct(r.asset.decimals,Number(r.amount))/ct(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Bt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Ft.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[K(u[0].quote.amount_in.asset),K(u[0].quote.amount_out.asset),K(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],x=[u[0].pool.address,u[1].pool.address],h=x[0],v=x[1],T=i===St.FixedInput?A:ut("positive",c,A),y=i===St.FixedOutput?I:ut("negative",c,I),w=Y(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Lt,Rt[i],t.encodeUint64(y)],foreignApps:[yt(o,M.V2)],foreignAssets:[d,f,m],accounts:[h,v],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Mt[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==C&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Ct[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(Pt[e]+kt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Ot;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Ot)}function re(e){return(Pt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return ct(n.decimals,Number(n.amount))/ct(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=ct(a.decimals,Number(n))/ct(r.decimals,Number(e));return pt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:M.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=x(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=yt(n.network,M.V1_1),a=h(J(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?H({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=xt("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:x=H({stateArray:I}),h=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=x[h],_=x[v],b=x[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=H({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,yt(s,M.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[M.V1_1].asset1],asset2ID:u[Oe[M.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=he(M.V2),u=i.generateLogicSigAccountForPool(e),c=yt(r,M.V2),p=u.address(),l=J(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:M.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,yt(s,M.V2)),u=null,i&&(u={asset1ID:i[Oe[M.V2].asset1],asset2ID:i[Oe[M.V2].asset2],poolTokenID:i[Oe[M.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=ct(t.asset1,e.asset1)/ct(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},M.V1_1,r(r({},Me),Ge)),p(qe,M.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=z(o,i),l=h(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,x=Y(A),v=yt(s,M.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:vt.getAppCallTxnNoteWithClientName(M.V1_1),appArgs:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:x?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:V.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(x),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,x||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=J(i,u),f=h(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,ot(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e,n){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r,s){var o;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return o=e.sent,e.abrupt("return",null==o?void 0:o.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer&&t.encodeAddress(a.arcv)===s?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&t.encodeAddress(a.rcv)===s&&r.push({id:C,amount:a.amt}),r}),[]));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(){return(ln=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=yt(s,M.V2),l=t.getApplicationAddress(p),d=z(o,i),f=h(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),x=Y(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:vt.getAppCallTxnNoteWithClientName(M.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=fn(x),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:dn(x),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function dn(t){return function(t,e){var n=he(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(M.V2,t)+fn(t)+O}function fn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function mn(){return(mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=J(i,u),f=h(d,2),m=f[0],g=f[1],A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(){return(gn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new G(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(t){return ln.apply(this,arguments)},signTxns:function(t){return mn.apply(this,arguments)},execute:function(t){return gn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+dn(e)}};var xn,hn,vn,Tn,yn=(p(An={},M.V1_1,en),p(An,M.V2,In),p(An,"generateTxns",(function(t){return t.contractVersion===M.V1_1?en.generateTxns(t):In.generateTxns(t)})),p(An,"signTxns",(function(t){return t.contractVersion===M.V1_1?en.signTxns(t):In.signTxns(t)})),p(An,"execute",(function(t){return t.contractVersion===M.V1_1?en.execute(t):In.execute(t)})),p(An,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===M.V1_1?en.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),wn=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),_n=5*t.ALGORAND_MIN_TX_FEE,bn=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),En=(p(xn={},bn.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,bn.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,bn.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Dn=(p(hn={},bn.INITIAL,1),p(hn,bn.SINGLE,2),p(hn,bn.FLEXIBLE,2),hn),Sn=(p(vn={},bn.INITIAL,3),p(vn,bn.FLEXIBLE,3),p(vn,bn.SINGLE,2),vn);function On(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var x=A;r=Pn(x,s),e={id:o.id,amount:x+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=Pn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Nn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function Pn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function kn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Rn=(p(Tn={},M.V1_1,[xt("mint")]),p(Tn,M.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Fn(){return(Fn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ut("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:yt(s,M.V1_1),appArgs:Rn.v1_1,accounts:[l],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:u.id==C?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),x=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),h=t.assignGroupID([x,m,g,A,I]),e.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=h(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===wn.FEE_TXN?p:n===wn.ASSET1_IN_TXN?l:n===wn.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[wn.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,ft(n,[o]);case 8:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=gt(s),A=At(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(x=I.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new G(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Cn=Object.freeze({__proto__:null,execute:function(t){return Mn.apply(this,arguments)},generateTxns:function(t){return Fn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:ct(p,Number(m))/ct(u,Number(l)),priceImpact:A}}function lr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:ct(p,Number(l))/ct(u,Number(m)),priceImpact:A}}}}function dr(t){return fr.apply(this,arguments)}function fr(){return(fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=pr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,cr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(lr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,cr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var Ir={getQuote:function(t){return ur.apply(this,arguments)},getFixedInputSwapQuote:dr,getFixedInputDirectSwapQuote:pr,getFixedOutputDirectSwapQuote:lr,getFixedOutputSwapQuote:mr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ir.apply(this,arguments)},calculateFixedInputSwap:Ar};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function hr(){return(hr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[tr,t.encodeUint64(ut("negative",p,u)),t.encodeUint64(ut("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[er.ASSET_TRANSFER_TXN]=m,A[er.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[er.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[er.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,x=ut("negative",p,c),u!==d){e.next=17;break}A=x,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=x,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return h=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[tr,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[er.ASSET_TRANSFER_TXN]=h,T[er.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[er.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[er.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,o=e.accountAddress,t.next=3,ft(n,[s]);case 3:return i=t.sent,u=h(i,1),c=u[0].txnID,t.prev=6,t.next=9,cn(n,r,o);case 9:p=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:p,txnID:c});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var yr,wr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ir.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=Ir.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,x=A.totalFeeAmount,h=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:h}}}return e},generateTxns:function(t){return hr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Tr.apply(this,arguments)}},_r=(p(yr={},M.V1_1,sr),p(yr,M.V2,wr),yr),br=3n,Er=1000n,Dr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Dr||{});function Sr(){return(Sr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=h(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Dr.FEE_TXN_INDEX?c:n===Dr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new G({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[xt("swap"),o===St.FixedInput?xt("fi"):xt("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:c.asset2ID==C?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),x=o===St.FixedOutput?ut("positive",i,p.assetInAmount):p.assetInAmount,h=l===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:x,suggestedParams:g}),v=o===St.FixedInput?ut("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,h,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*br/Er,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function Pr(t){return kr.apply(this,arguments)}function kr(){return(kr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+x,excessAmount:{assetID:i.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Er/(Er-br),l=p-c,d=ct(a.assetOut,Number(u))/ct(a.assetIn,Number(p)),f=ct(a.assetOut,Number(i))/ct(a.assetIn,Number(o)),m=pt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Rr(t){return Fr.apply(this,arguments)}function Fr(){return(Fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-x,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new G({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Dr.ASSET_IN_TXN_INDEX].txn.assetIndex||C,amount:i[Dr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Dr.ASSET_OUT_TXN_INDEX].txn.assetIndex||C,amount:i[Dr.ASSET_OUT_TXN_INDEX].txn.amount},o!==St.FixedInput){t.next=12;break}return t.next=9,Pr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Rr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:At(i),fees:gt(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new G(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Mr,Cr={getQuote:function(t,e,n,r,a){return t===St.FixedInput?Nr({pool:e,reserves:n,assetIn:r,decimals:a}):Lr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Lr,generateTxns:function(t){return Or.apply(this,arguments)},signTxns:function(t){return Sr.apply(this,arguments)},execute:function(t){return Br.apply(this,arguments)},executeFixedOutputSwap:Rr};function jr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=h(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Vr(){return(Vr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===M.V2})),c.push(Ir.getFixedInputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===M.V2})),c.push(Ir.getFixedOutputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qr=(p(Mr={},M.V1_1,Cr),p(Mr,M.V2,Ir),p(Mr,"getQuote",(function(t){var e=t.type;if(e===St.FixedInput)return function(t){return Vr.apply(this,arguments)}(t);if(e===St.FixedOutput)return function(t){return Xr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Mr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1?Cr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):Ir.generateTxns(t)})),p(Mr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1){var e=t.quote.data.pool;return Cr.signTxns(r(r({},t),{},{pool:e}))}return Ir.signTxns(t)})),p(Mr,"execute",(function(t){return t.contractVersion===M.V1_1?Cr.execute(t):Ir.execute(t)})),Mr);function Ur(){return(Ur=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,qr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,ft(n,[i]);case 7:return u=t.sent,c=h(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:gt(s),confirmedRound:d,txnID:l,groupID:At(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function qr(t){return Gr.apply(this,arguments)}function Gr(){return(Gr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=h(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wr(){return Wr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:gt(e),groupID:At(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,ft(r,[u]);case 4:p=a.sent,l=h(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new G(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[xt("redeem")],note:vt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[xt("commit"),u],note:Z([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Yr(){return(Yr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Jr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[xt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=j,exports.ALGO_ASSET_ID=C,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Kn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=yn,exports.CONTRACT_VERSION=M,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=V,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=_r,exports.Swap=Qr,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=St,exports.V1_1AddLiquidityTxnIndices=wn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Zn,exports.V2AddLiquidityTxnIndices=En,exports.V2AddLiquidityType=bn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=$n,exports.applySlippageToAmount=ut,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Ur.apply(this,arguments)},exports.sendAndWaitRawTransaction=ft,exports.sumUpTxnFees=gt,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=xe,exports.tinymanJSSDKConfig=vt; diff --git a/dist/remove-liquidity/index.d.ts b/dist/remove-liquidity/index.d.ts index f2a3b738..fa1d1769 100644 --- a/dist/remove-liquidity/index.d.ts +++ b/dist/remove-liquidity/index.d.ts @@ -61,10 +61,11 @@ export declare const RemoveLiquidity: { txGroup: import("..").SignerTransaction[]; initiatorSigner: import("..").InitiatorSigner; }) => Promise; - execute: ({ client, txGroup, signedTxns }: { + execute: ({ client, txGroup, signedTxns, accountAddress }: { client: import("algosdk").Algodv2; txGroup: import("..").SignerTransaction[]; signedTxns: Uint8Array[]; + accountAddress: string; }) => Promise; }; }; diff --git a/dist/remove-liquidity/v2/index.d.ts b/dist/remove-liquidity/v2/index.d.ts index 3edccb20..0b851519 100644 --- a/dist/remove-liquidity/v2/index.d.ts +++ b/dist/remove-liquidity/v2/index.d.ts @@ -49,10 +49,11 @@ declare function signTxns({ txGroup, initiatorSigner }: { txGroup: SignerTransaction[]; initiatorSigner: InitiatorSigner; }): Promise; -declare function execute({ client, txGroup, signedTxns }: { +declare function execute({ client, txGroup, signedTxns, accountAddress }: { client: Algodv2; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; + accountAddress: string; }): Promise; export declare const RemoveLiquidityV2: { getQuote: typeof getQuote; diff --git a/dist/swap/index.d.ts b/dist/swap/index.d.ts index 677b5e52..f973190a 100644 --- a/dist/swap/index.d.ts +++ b/dist/swap/index.d.ts @@ -89,11 +89,12 @@ export declare const Swap: { txGroup: import("..").SignerTransaction[]; initiatorSigner: import("..").InitiatorSigner; }) => Promise; - execute: ({ client, quote, txGroup, signedTxns }: { + execute: ({ client, quote, txGroup, signedTxns, accountAddress }: { client: import("algosdk").Algodv2; quote: import("./types").SwapQuote; txGroup: import("..").SignerTransaction[]; signedTxns: Uint8Array[]; + accountAddress: string; }) => Promise; calculateFixedInputSwap: ({ inputSupply, outputSupply, swapInputAmount, totalFeeShare, decimals }: { inputSupply: bigint; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index f4c0c6d3..3447eb98 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -153,5 +153,6 @@ export interface ExecuteSwapCommonParams { network: SupportedNetwork; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; + accountAddress: string; assetIn: AssetWithIdAndAmount; } diff --git a/dist/swap/v2/index.d.ts b/dist/swap/v2/index.d.ts index 5032dd0c..dbf8f2cd 100644 --- a/dist/swap/v2/index.d.ts +++ b/dist/swap/v2/index.d.ts @@ -12,11 +12,12 @@ declare function signTxns({ txGroup, initiatorSigner }: { /** * Executes a swap with the desired quantities. */ -declare function execute({ client, quote, txGroup, signedTxns }: { +declare function execute({ client, quote, txGroup, signedTxns, accountAddress }: { client: Algodv2; quote: SwapQuote; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; + accountAddress: string; }): Promise; /** * @param type - Type of the swap diff --git a/dist/util/commonTypes.d.ts b/dist/util/commonTypes.d.ts index 0cc92568..f390d4ca 100644 --- a/dist/util/commonTypes.d.ts +++ b/dist/util/commonTypes.d.ts @@ -28,9 +28,11 @@ export type TxnResponseInnerTxns = { type: TransactionType.axfer; xaid: number; aamt: number; + arcv: Uint8Array; } | { type: TransactionType.pay; amt: number; + rcv: Uint8Array; } | { /** * This is not a real txn type, only added to diff --git a/dist/util/transaction/transactionUtils.d.ts b/dist/util/transaction/transactionUtils.d.ts index d24ccbd4..1510861b 100644 --- a/dist/util/transaction/transactionUtils.d.ts +++ b/dist/util/transaction/transactionUtils.d.ts @@ -12,7 +12,7 @@ export declare function getAppCallInnerTxns(client: Algodv2, txGroup: SignerTran * inner transactions in the app call, and return the extracted asset data. * Useful for getting the asset data after an "execute" operation. */ -export declare function getAppCallInnerAssetData(client: Algodv2, txGroup: SignerTransaction[]): Promise; +export declare function getAppCallInnerAssetData(client: Algodv2, txGroup: SignerTransaction[], accountAddress: string): Promise; /** * Combines the provided signer transaction groups into one signer transaction group, with a new group ID * @param signerTransactions - The signer transaction groups to combine diff --git a/src/add-liquidity/v2/common.ts b/src/add-liquidity/v2/common.ts index 7effa715..f86d6f24 100644 --- a/src/add-liquidity/v2/common.ts +++ b/src/add-liquidity/v2/common.ts @@ -29,12 +29,14 @@ export async function execute({ client, pool, txGroup, - signedTxns + signedTxns, + accountAddress }: { client: Algodv2; pool: V2PoolInfo; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; + accountAddress: string; }): Promise { try { const [{confirmedRound, txnID}] = await sendAndWaitRawTransaction(client, [ @@ -44,7 +46,7 @@ export async function execute({ let assetOut: AssetWithIdAndAmount | undefined; try { - assetOut = (await getAppCallInnerAssetData(client, txGroup))?.find( + assetOut = (await getAppCallInnerAssetData(client, txGroup, accountAddress))?.find( // Output asset is the pool token for add liquidity ({id}) => id === pool.poolTokenID ); diff --git a/src/remove-liquidity/v2/index.ts b/src/remove-liquidity/v2/index.ts index c0175c9f..780aeae0 100644 --- a/src/remove-liquidity/v2/index.ts +++ b/src/remove-liquidity/v2/index.ts @@ -319,17 +319,19 @@ function signTxns({ async function execute({ client, txGroup, - signedTxns + signedTxns, + accountAddress }: { client: Algodv2; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; + accountAddress: string; }): Promise { const [{txnID}] = await sendAndWaitRawTransaction(client, [signedTxns]); let outputAssets: AssetWithIdAndAmount[] | undefined; try { - outputAssets = await getAppCallInnerAssetData(client, txGroup); + outputAssets = await getAppCallInnerAssetData(client, txGroup, accountAddress); } catch (_error) { // We can ignore this error since the main execution was successful } diff --git a/src/swap/types.ts b/src/swap/types.ts index 8acd0acc..7765b5e6 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -173,5 +173,6 @@ export interface ExecuteSwapCommonParams { network: SupportedNetwork; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; + accountAddress: string; assetIn: AssetWithIdAndAmount; } diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index 423aec77..3f15092d 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -151,12 +151,14 @@ async function execute({ client, quote, txGroup, - signedTxns + signedTxns, + accountAddress }: { client: Algodv2; quote: SwapQuote; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; + accountAddress: string; }): Promise { const [{confirmedRound, txnID}] = await sendAndWaitRawTransaction(client, [signedTxns]); const assetOutId = getAssetOutFromSwapQuote(quote).id; @@ -164,7 +166,7 @@ async function execute({ let innerTxnAssetData: AssetWithIdAndAmount[] | undefined; try { - innerTxnAssetData = await getAppCallInnerAssetData(client, txGroup); + innerTxnAssetData = await getAppCallInnerAssetData(client, txGroup, accountAddress); } catch (_error) { // We can ignore this error since the main execution was successful } diff --git a/src/util/commonTypes.ts b/src/util/commonTypes.ts index 58f67380..f08670eb 100644 --- a/src/util/commonTypes.ts +++ b/src/util/commonTypes.ts @@ -36,10 +36,12 @@ export type TxnResponseInnerTxns = { type: TransactionType.axfer; xaid: number; aamt: number; + arcv: Uint8Array; } | { type: TransactionType.pay; amt: number; + rcv: Uint8Array; } | { /** diff --git a/src/util/transaction/transactionUtils.ts b/src/util/transaction/transactionUtils.ts index d3fa6e46..6346825a 100644 --- a/src/util/transaction/transactionUtils.ts +++ b/src/util/transaction/transactionUtils.ts @@ -1,4 +1,4 @@ -import { +import algosdk, { Algodv2, assignGroupID, decodeUnsignedTransaction, @@ -48,7 +48,8 @@ export async function getAppCallInnerTxns( */ export async function getAppCallInnerAssetData( client: Algodv2, - txGroup: SignerTransaction[] + txGroup: SignerTransaction[], + accountAddress: string ): Promise { const innerTxns = await getAppCallInnerTxns(client, txGroup); @@ -56,12 +57,18 @@ export async function getAppCallInnerAssetData( let updatedAssets = assets; const {txn: innerTxn} = txn; - if (innerTxn.type === TransactionType.axfer) { + if ( + innerTxn.type === TransactionType.axfer && + algosdk.encodeAddress(innerTxn.arcv) === accountAddress + ) { updatedAssets.push({ id: innerTxn.xaid, amount: innerTxn.aamt }); - } else if (innerTxn.type === TransactionType.pay) { + } else if ( + innerTxn.type === TransactionType.pay && + algosdk.encodeAddress(innerTxn.rcv) === accountAddress + ) { updatedAssets.push({ // Payment transactions are always in ALGO id: ALGO_ASSET_ID, From 8c983c12bf3fba276ed1844e38b565573747b0bf Mon Sep 17 00:00:00 2001 From: anlerkan Date: Tue, 4 Apr 2023 23:10:10 +0300 Subject: [PATCH 32/39] feat(transaction/util): extract account address from tx group instead of getting it from parameters --- dist/add-liquidity/v2/common.d.ts | 3 +-- dist/index.js | 2 +- dist/remove-liquidity/index.d.ts | 3 +-- dist/remove-liquidity/v2/index.d.ts | 3 +-- dist/swap/index.d.ts | 3 +-- dist/swap/v2/index.d.ts | 3 +-- dist/util/transaction/transactionUtils.d.ts | 8 ++++++- src/add-liquidity/v2/common.ts | 6 ++---- src/remove-liquidity/v2/index.ts | 6 ++---- src/swap/v2/index.ts | 6 ++---- src/util/transaction/transactionUtils.ts | 24 +++++++++++++++++++-- 11 files changed, 41 insertions(+), 26 deletions(-) diff --git a/dist/add-liquidity/v2/common.d.ts b/dist/add-liquidity/v2/common.d.ts index 8a5bf567..a1731094 100644 --- a/dist/add-liquidity/v2/common.d.ts +++ b/dist/add-liquidity/v2/common.d.ts @@ -13,10 +13,9 @@ export declare function signTxns({ txGroup, initiatorSigner }: { * @param params.pool Information for the pool. * @param params.txGroup The transaction group to execute. */ -export declare function execute({ client, pool, txGroup, signedTxns, accountAddress }: { +export declare function execute({ client, pool, txGroup, signedTxns }: { client: Algodv2; pool: V2PoolInfo; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; - accountAddress: string; }): Promise; diff --git a/dist/index.js b/dist/index.js index b09fb919..8914bc0c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={V1_1:"v1_1",V2:"v2"},C=0,j={id:"".concat(C),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},V={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},X="- would result negative",Q="logic eval error:",U="exceeds schema integer count",q=/transaction \w+:/,G=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function J(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Y(t){return Number(t)===C}function K(t){return Number(t.id)}function H(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function Z(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var $=100000n,tt=100000n,et=100000n,nt=25000n+25000n,rt=25000n+3500n;function at(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return $+tt*BigInt(o.length)+et*BigInt(s.length+a.length)+rt*r+nt*n}function st(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function ot(t,e){return it.apply(this,arguments)}function it(){return(it=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,st(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ut(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function ct(t,e){var n=Number(t);return pt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function pt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=h(dt(e),2),s=a[0],o=a[1],i=h(dt(Math.round(Number(lt(s,o+r)))),2),u=i[0],c=i[1];return Number(lt(u,c-r))}return Math.round(e)}function lt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function dt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function ft(t,e){return mt.apply(this,arguments)}function mt(){return(mt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,ot(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new G(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function gt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function At(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function It(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function xt(t){return(new TextEncoder).encode(t)}var ht,vt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===M.V1_1?"v1":t;return xt("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),Tt=(p(ht={},M.V1_1,{testnet:62368684,mainnet:552635992}),p(ht,M.V2,{testnet:148607e3,mainnet:1002541853}),ht);function yt(t,e){var n=Tt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function wt(){return(wt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var bt,Et,Dt,St=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Ot=.003,Nt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(bt={},St.FixedInput,1),p(bt,St.FixedOutput,2),bt),kt=2,Lt=xt("swap"),Rt=(p(Et={},St.FixedInput,xt("fixed-input")),p(Et,St.FixedOutput,xt("fixed-output")),Et),Ft={ASSET_OPT_IN:xt("asset_opt_in")},Bt={testnet:159521633,mainnet:1071281873},Mt=(p(Dt={},St.FixedInput,7),p(Dt,St.FixedOutput,8),Dt),Ct={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return ct(r.asset.decimals,Number(r.amount))/ct(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Bt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Ft.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[K(u[0].quote.amount_in.asset),K(u[0].quote.amount_out.asset),K(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],x=[u[0].pool.address,u[1].pool.address],h=x[0],v=x[1],T=i===St.FixedInput?A:ut("positive",c,A),y=i===St.FixedOutput?I:ut("negative",c,I),w=Y(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Lt,Rt[i],t.encodeUint64(y)],foreignApps:[yt(o,M.V2)],foreignAssets:[d,f,m],accounts:[h,v],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Mt[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==C&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Ct[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(Pt[e]+kt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Ot;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Ot)}function re(e){return(Pt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return ct(n.decimals,Number(n.amount))/ct(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=ct(a.decimals,Number(n))/ct(r.decimals,Number(e));return pt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:M.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=x(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=yt(n.network,M.V1_1),a=h(J(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?H({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=xt("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:x=H({stateArray:I}),h=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=x[h],_=x[v],b=x[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=H({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,yt(s,M.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[M.V1_1].asset1],asset2ID:u[Oe[M.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=he(M.V2),u=i.generateLogicSigAccountForPool(e),c=yt(r,M.V2),p=u.address(),l=J(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:M.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,yt(s,M.V2)),u=null,i&&(u={asset1ID:i[Oe[M.V2].asset1],asset2ID:i[Oe[M.V2].asset2],poolTokenID:i[Oe[M.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=ct(t.asset1,e.asset1)/ct(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},M.V1_1,r(r({},Me),Ge)),p(qe,M.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=z(o,i),l=h(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,x=Y(A),v=yt(s,M.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:vt.getAppCallTxnNoteWithClientName(M.V1_1),appArgs:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:x?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:V.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(x),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,x||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=J(i,u),f=h(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,ot(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e,n){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r,s){var o;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return o=e.sent,e.abrupt("return",null==o?void 0:o.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer&&t.encodeAddress(a.arcv)===s?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&t.encodeAddress(a.rcv)===s&&r.push({id:C,amount:a.amt}),r}),[]));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(){return(ln=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=yt(s,M.V2),l=t.getApplicationAddress(p),d=z(o,i),f=h(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),x=Y(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:vt.getAppCallTxnNoteWithClientName(M.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=fn(x),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:dn(x),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function dn(t){return function(t,e){var n=he(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(M.V2,t)+fn(t)+O}function fn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function mn(){return(mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=J(i,u),f=h(d,2),m=f[0],g=f[1],A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(){return(gn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new G(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(t){return ln.apply(this,arguments)},signTxns:function(t){return mn.apply(this,arguments)},execute:function(t){return gn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+dn(e)}};var xn,hn,vn,Tn,yn=(p(An={},M.V1_1,en),p(An,M.V2,In),p(An,"generateTxns",(function(t){return t.contractVersion===M.V1_1?en.generateTxns(t):In.generateTxns(t)})),p(An,"signTxns",(function(t){return t.contractVersion===M.V1_1?en.signTxns(t):In.signTxns(t)})),p(An,"execute",(function(t){return t.contractVersion===M.V1_1?en.execute(t):In.execute(t)})),p(An,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===M.V1_1?en.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),wn=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),_n=5*t.ALGORAND_MIN_TX_FEE,bn=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),En=(p(xn={},bn.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,bn.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,bn.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Dn=(p(hn={},bn.INITIAL,1),p(hn,bn.SINGLE,2),p(hn,bn.FLEXIBLE,2),hn),Sn=(p(vn={},bn.INITIAL,3),p(vn,bn.FLEXIBLE,3),p(vn,bn.SINGLE,2),vn);function On(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var x=A;r=Pn(x,s),e={id:o.id,amount:x+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=Pn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Nn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function Pn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function kn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Rn=(p(Tn={},M.V1_1,[xt("mint")]),p(Tn,M.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Fn(){return(Fn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ut("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:yt(s,M.V1_1),appArgs:Rn.v1_1,accounts:[l],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:u.id==C?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),x=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),h=t.assignGroupID([x,m,g,A,I]),e.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=h(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===wn.FEE_TXN?p:n===wn.ASSET1_IN_TXN?l:n===wn.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[wn.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,ft(n,[o]);case 8:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=gt(s),A=At(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(x=I.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new G(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Cn=Object.freeze({__proto__:null,execute:function(t){return Mn.apply(this,arguments)},generateTxns:function(t){return Fn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:ct(p,Number(m))/ct(u,Number(l)),priceImpact:A}}function lr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:ct(p,Number(l))/ct(u,Number(m)),priceImpact:A}}}}function dr(t){return fr.apply(this,arguments)}function fr(){return(fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=pr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,cr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(lr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,cr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var Ir={getQuote:function(t){return ur.apply(this,arguments)},getFixedInputSwapQuote:dr,getFixedInputDirectSwapQuote:pr,getFixedOutputDirectSwapQuote:lr,getFixedOutputSwapQuote:mr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ir.apply(this,arguments)},calculateFixedInputSwap:Ar};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function hr(){return(hr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[tr,t.encodeUint64(ut("negative",p,u)),t.encodeUint64(ut("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[er.ASSET_TRANSFER_TXN]=m,A[er.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[er.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[er.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,x=ut("negative",p,c),u!==d){e.next=17;break}A=x,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=x,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return h=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[tr,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[er.ASSET_TRANSFER_TXN]=h,T[er.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[er.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[er.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,o=e.accountAddress,t.next=3,ft(n,[s]);case 3:return i=t.sent,u=h(i,1),c=u[0].txnID,t.prev=6,t.next=9,cn(n,r,o);case 9:p=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:p,txnID:c});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var yr,wr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ir.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=Ir.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,x=A.totalFeeAmount,h=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:h}}}return e},generateTxns:function(t){return hr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Tr.apply(this,arguments)}},_r=(p(yr={},M.V1_1,sr),p(yr,M.V2,wr),yr),br=3n,Er=1000n,Dr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Dr||{});function Sr(){return(Sr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=h(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Dr.FEE_TXN_INDEX?c:n===Dr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new G({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[xt("swap"),o===St.FixedInput?xt("fi"):xt("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:c.asset2ID==C?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),x=o===St.FixedOutput?ut("positive",i,p.assetInAmount):p.assetInAmount,h=l===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:x,suggestedParams:g}),v=o===St.FixedInput?ut("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,h,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*br/Er,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function Pr(t){return kr.apply(this,arguments)}function kr(){return(kr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+x,excessAmount:{assetID:i.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Er/(Er-br),l=p-c,d=ct(a.assetOut,Number(u))/ct(a.assetIn,Number(p)),f=ct(a.assetOut,Number(i))/ct(a.assetIn,Number(o)),m=pt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Rr(t){return Fr.apply(this,arguments)}function Fr(){return(Fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-x,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new G({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Dr.ASSET_IN_TXN_INDEX].txn.assetIndex||C,amount:i[Dr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Dr.ASSET_OUT_TXN_INDEX].txn.assetIndex||C,amount:i[Dr.ASSET_OUT_TXN_INDEX].txn.amount},o!==St.FixedInput){t.next=12;break}return t.next=9,Pr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Rr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:At(i),fees:gt(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new G(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Mr,Cr={getQuote:function(t,e,n,r,a){return t===St.FixedInput?Nr({pool:e,reserves:n,assetIn:r,decimals:a}):Lr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Lr,generateTxns:function(t){return Or.apply(this,arguments)},signTxns:function(t){return Sr.apply(this,arguments)},execute:function(t){return Br.apply(this,arguments)},executeFixedOutputSwap:Rr};function jr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=h(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Vr(){return(Vr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===M.V2})),c.push(Ir.getFixedInputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===M.V2})),c.push(Ir.getFixedOutputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qr=(p(Mr={},M.V1_1,Cr),p(Mr,M.V2,Ir),p(Mr,"getQuote",(function(t){var e=t.type;if(e===St.FixedInput)return function(t){return Vr.apply(this,arguments)}(t);if(e===St.FixedOutput)return function(t){return Xr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Mr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1?Cr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):Ir.generateTxns(t)})),p(Mr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1){var e=t.quote.data.pool;return Cr.signTxns(r(r({},t),{},{pool:e}))}return Ir.signTxns(t)})),p(Mr,"execute",(function(t){return t.contractVersion===M.V1_1?Cr.execute(t):Ir.execute(t)})),Mr);function Ur(){return(Ur=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,qr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,ft(n,[i]);case 7:return u=t.sent,c=h(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:gt(s),confirmedRound:d,txnID:l,groupID:At(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function qr(t){return Gr.apply(this,arguments)}function Gr(){return(Gr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=h(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wr(){return Wr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:gt(e),groupID:At(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,ft(r,[u]);case 4:p=a.sent,l=h(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new G(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[xt("redeem")],note:vt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[xt("commit"),u],note:Z([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Yr(){return(Yr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Jr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[xt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=j,exports.ALGO_ASSET_ID=C,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Kn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=yn,exports.CONTRACT_VERSION=M,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=V,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=_r,exports.Swap=Qr,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=St,exports.V1_1AddLiquidityTxnIndices=wn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Zn,exports.V2AddLiquidityTxnIndices=En,exports.V2AddLiquidityType=bn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=$n,exports.applySlippageToAmount=ut,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Ur.apply(this,arguments)},exports.sendAndWaitRawTransaction=ft,exports.sumUpTxnFees=gt,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=xe,exports.tinymanJSSDKConfig=vt; +"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=h(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={V1_1:"v1_1",V2:"v2"},C=0,j={id:"".concat(C),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},V={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},X="- would result negative",Q="logic eval error:",U="exceeds schema integer count",q=/transaction \w+:/,G=function(t){l(n,A(Error));var e=h(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function J(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Y(t){return Number(t)===C}function K(t){return Number(t.id)}function H(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function Z(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var $=100000n,tt=100000n,et=100000n,nt=25000n+25000n,rt=25000n+3500n;function at(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return $+tt*BigInt(o.length)+et*BigInt(s.length+a.length)+rt*r+nt*n}function st(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function ot(t,e){return it.apply(this,arguments)}function it(){return(it=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,st(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ut(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function ct(t,e){var n=Number(t);return pt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function pt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=x(dt(e),2),s=a[0],o=a[1],i=x(dt(Math.round(Number(lt(s,o+r)))),2),u=i[0],c=i[1];return Number(lt(u,c-r))}return Math.round(e)}function lt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function dt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function ft(t,e){return mt.apply(this,arguments)}function mt(){return(mt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,ot(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new G(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function gt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function At(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function It(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ht(t){return(new TextEncoder).encode(t)}var xt,vt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===M.V1_1?"v1":t;return ht("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),Tt=(p(xt={},M.V1_1,{testnet:62368684,mainnet:552635992}),p(xt,M.V2,{testnet:148607e3,mainnet:1002541853}),xt);function yt(t,e){var n=Tt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function wt(){return(wt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var bt,Et,Dt,St=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Ot=.003,Nt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(bt={},St.FixedInput,1),p(bt,St.FixedOutput,2),bt),kt=2,Lt=ht("swap"),Rt=(p(Et={},St.FixedInput,ht("fixed-input")),p(Et,St.FixedOutput,ht("fixed-output")),Et),Ft={ASSET_OPT_IN:ht("asset_opt_in")},Bt={testnet:159521633,mainnet:1071281873},Mt=(p(Dt={},St.FixedInput,7),p(Dt,St.FixedOutput,8),Dt),Ct={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return ct(r.asset.decimals,Number(r.amount))/ct(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Bt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Ft.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[K(u[0].quote.amount_in.asset),K(u[0].quote.amount_out.asset),K(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],h=[u[0].pool.address,u[1].pool.address],x=h[0],v=h[1],T=i===St.FixedInput?A:ut("positive",c,A),y=i===St.FixedOutput?I:ut("negative",c,I),w=Y(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Lt,Rt[i],t.encodeUint64(y)],foreignApps:[yt(o,M.V2)],foreignAssets:[d,f,m],accounts:[x,v],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Mt[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==C&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Ct[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(Pt[e]+kt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Ot;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Ot)}function re(e){return(Pt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return ct(n.decimals,Number(n.amount))/ct(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=ct(a.decimals,Number(n))/ct(r.decimals,Number(e));return pt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:M.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=h(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=yt(n.network,M.V1_1),a=x(J(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?H({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=ht("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=H({stateArray:I}),x=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=h[x],_=h[v],b=h[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,h,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=H({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,yt(s,M.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[M.V1_1].asset1],asset2ID:u[Oe[M.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=xe(M.V2),u=i.generateLogicSigAccountForPool(e),c=yt(r,M.V2),p=u.address(),l=J(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:M.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,yt(s,M.V2)),u=null,i&&(u={asset1ID:i[Oe[M.V2].asset1],asset2ID:i[Oe[M.V2].asset2],poolTokenID:i[Oe[M.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=ct(t.asset1,e.asset1)/ct(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},M.V1_1,r(r({},Me),Ge)),p(qe,M.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=z(o,i),l=x(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,h=Y(A),v=yt(s,M.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:vt.getAppCallTxnNoteWithClientName(M.V1_1),appArgs:[ht("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:V.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(h),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,h||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=x(c,1),l=p[0],d=J(i,u),f=x(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],h=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:h,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,ot(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r){var s,o;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return s=e.sent,o=ln(r),e.abrupt("return",null==s?void 0:s.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer&&t.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&t.encodeAddress(a.rcv)===o&&r.push({id:C,amount:a.amt}),r}),[]));case 5:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e){if(!e.length)throw new G(e,"Transaction group should not be empty");var n=e[0].txn.from.publicKey;if(!n)throw new G(e,"Transaction group should have a sender address");return t.encodeAddress(n)}function dn(){return(dn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=yt(s,M.V2),l=t.getApplicationAddress(p),d=z(o,i),f=x(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=he.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),h=Y(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[ht("bootstrap")],note:vt.getAppCallTxnNoteWithClientName(M.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=mn(h),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:fn(h),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fn(t){return function(t,e){var n=xe(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(M.V2,t)+mn(t)+O}function mn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function gn(){return(gn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=x(c,1),l=p[0],d=J(i,u),f=x(d,2),m=f[0],g=f[1],A=he.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],h=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:h,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function An(){return(An=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new G(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var In,hn={generateTxns:function(t){return dn.apply(this,arguments)},signTxns:function(t){return gn.apply(this,arguments)},execute:function(t){return An.apply(this,arguments)},getBootstrapFundingTxnAmount:fn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+fn(e)}};var xn,vn,Tn,yn,wn=(p(In={},M.V1_1,en),p(In,M.V2,hn),p(In,"generateTxns",(function(t){return t.contractVersion===M.V1_1?en.generateTxns(t):hn.generateTxns(t)})),p(In,"signTxns",(function(t){return t.contractVersion===M.V1_1?en.signTxns(t):hn.signTxns(t)})),p(In,"execute",(function(t){return t.contractVersion===M.V1_1?en.execute(t):hn.execute(t)})),p(In,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===M.V1_1?en.getBootstrapFundingTxnAmount(n):hn.getBootstrapFundingTxnAmount(n)})),In),_n=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),bn=5*t.ALGORAND_MIN_TX_FEE,En=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),Dn=(p(xn={},En.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,En.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,En.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Sn=(p(vn={},En.INITIAL,1),p(vn,En.SINGLE,2),p(vn,En.FLEXIBLE,2),vn),On=(p(Tn={},En.INITIAL,3),p(Tn,En.FLEXIBLE,3),p(Tn,En.SINGLE,2),Tn);function Nn(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var h=A;r=kn(h,s),e={id:o.id,amount:h+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var x=I;r=kn(x,s),e={id:i.id,amount:x+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Pn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Ln(e){return(Sn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Rn=ht("add_liquidity"),Fn=(p(yn={},M.V1_1,[ht("mint")]),p(yn,M.V2,{INITIAL_LIQUIDITY:[ht("add_initial_liquidity")],SINGLE_ASSET_MODE:[Rn,ht("single")],FLEXIBLE_MODE:[Rn,ht("flexible")]}),yn);function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ut("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:yt(s,M.V1_1),appArgs:Fn.v1_1,accounts:[l],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:u.id==C?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),h=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),x=t.assignGroupID([h,m,g,A,I]),e.abrupt("return",[{txn:x[0],signers:[l]},{txn:x[1],signers:[o]},{txn:x[2],signers:[l]},{txn:x[3],signers:[l]},{txn:x[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=x(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===_n.FEE_TXN?p:n===_n.ASSET1_IN_TXN?l:n===_n.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Cn(){return(Cn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[_n.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,ft(n,[o]);case 8:return p=t.sent,l=x(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=gt(s),A=At(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(h=I.excessPoolTokens-c.excessPoolTokens)<0n&&(h=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+h,excessAmount:{excessAmountForAddingLiquidity:h,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new G(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var jn=Object.freeze({__proto__:null,execute:function(t){return Cn.apply(this,arguments)},generateTxns:function(t){return Bn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:ct(p,Number(m))/ct(u,Number(l)),priceImpact:A}}function dr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:ct(p,Number(l))/ct(u,Number(m)),priceImpact:A}}}}function fr(t){return mr.apply(this,arguments)}function mr(){return(mr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=lr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,pr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function gr(t){return Ar.apply(this,arguments)}function Ar(){return(Ar=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(dr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,pr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ir(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var hr={getQuote:function(t){return cr.apply(this,arguments)},getFixedInputSwapQuote:fr,getFixedInputDirectSwapQuote:lr,getFixedOutputDirectSwapQuote:dr,getFixedOutputSwapQuote:gr,generateTxns:function(t){return ir.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ur.apply(this,arguments)},calculateFixedInputSwap:Ir};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[er,t.encodeUint64(ut("negative",p,u)),t.encodeUint64(ut("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=(tr+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[nr.ASSET_TRANSFER_TXN]=m,A[nr.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[nr.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[nr.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,h=ut("negative",p,c),u!==d){e.next=17;break}A=h,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return x=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[er,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(tr+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[nr.ASSET_TRANSFER_TXN]=x,T[nr.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[nr.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[nr.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function yr(){return(yr=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,t.next=3,ft(n,[s]);case 3:return o=t.sent,i=x(o,1),u=i[0].txnID,t.prev=6,t.next=9,cn(n,r);case 9:c=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var wr,_r={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=hr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=hr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,h=A.totalFeeAmount,x=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:h},priceImpact:x}}}return e},generateTxns:function(t){return vr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Tr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return yr.apply(this,arguments)}},br=(p(wr={},M.V1_1,or),p(wr,M.V2,_r),wr),Er=3n,Dr=1000n,Sr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Sr||{});function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=x(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(){return(Nr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new G({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[ht("swap"),o===St.FixedInput?ht("fi"):ht("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:c.asset2ID==C?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),h=o===St.FixedOutput?ut("positive",i,p.assetInAmount):p.assetInAmount,x=l===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,suggestedParams:g}),v=o===St.FixedInput?ut("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,x,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Pr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Dr,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function kr(t){return Lr.apply(this,arguments)}function Lr(){return(Lr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,h;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=x(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(h=I-A)<0n&&(h=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+h,excessAmount:{assetID:i.id,excessAmountForSwap:h,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Dr/(Dr-Er),l=p-c,d=ct(a.assetOut,Number(u))/ct(a.assetIn,Number(p)),f=ct(a.assetOut,Number(i))/ct(a.assetIn,Number(o)),m=pt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Fr(t){return Br.apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,h;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=x(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(h=I-A)<0n&&(h=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-h,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:h,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Mr(){return(Mr=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new G({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||C,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||C,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==St.FixedInput){t.next=12;break}return t.next=9,kr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Fr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:At(i),fees:gt(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new G(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Cr,jr={getQuote:function(t,e,n,r,a){return t===St.FixedInput?Pr({pool:e,reserves:n,assetIn:r,decimals:a}):Rr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Pr,getFixedOutputSwapQuote:Rr,generateTxns:function(t){return Nr.apply(this,arguments)},signTxns:function(t){return Or.apply(this,arguments)},execute:function(t){return Mr.apply(this,arguments)},executeFixedOutputSwap:Fr};function Vr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=x(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===M.V2})),c.push(hr.getFixedInputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,Vr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Qr(){return(Qr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===M.V2})),c.push(hr.getFixedOutputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,Vr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ur=(p(Cr={},M.V1_1,jr),p(Cr,M.V2,hr),p(Cr,"getQuote",(function(t){var e=t.type;if(e===St.FixedInput)return function(t){return Xr.apply(this,arguments)}(t);if(e===St.FixedOutput)return function(t){return Qr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Cr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1?jr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):hr.generateTxns(t)})),p(Cr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1){var e=t.quote.data.pool;return jr.signTxns(r(r({},t),{},{pool:e}))}return hr.signTxns(t)})),p(Cr,"execute",(function(t){return t.contractVersion===M.V1_1?jr.execute(t):hr.execute(t)})),Cr);function qr(){return(qr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,Gr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,ft(n,[i]);case 7:return u=t.sent,c=x(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:gt(s),confirmedRound:d,txnID:l,groupID:At(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function Gr(t){return Wr.apply(this,arguments)}function Wr(){return(Wr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=x(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zr(){return zr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:gt(e),groupID:At(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,ft(r,[u]);case 4:p=a.sent,l=x(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new G(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),zr.apply(this,arguments)}function Jr(){return(Jr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[ht("redeem")],note:vt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Yr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ht("commit"),u],note:Z([ht("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Kr(){return(Kr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Yr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[ht("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=j,exports.ALGO_ASSET_ID=C,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Hn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=wn,exports.CONTRACT_VERSION=M,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=V,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=br,exports.Swap=Ur,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=St,exports.V1_1AddLiquidityTxnIndices=_n,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=$n,exports.V2AddLiquidityTxnIndices=Dn,exports.V2AddLiquidityType=En,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=tr,exports.applySlippageToAmount=ut,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Kr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return zr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=ft,exports.sumUpTxnFees=gt,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=he,exports.tinymanJSSDKConfig=vt; diff --git a/dist/remove-liquidity/index.d.ts b/dist/remove-liquidity/index.d.ts index fa1d1769..f2a3b738 100644 --- a/dist/remove-liquidity/index.d.ts +++ b/dist/remove-liquidity/index.d.ts @@ -61,11 +61,10 @@ export declare const RemoveLiquidity: { txGroup: import("..").SignerTransaction[]; initiatorSigner: import("..").InitiatorSigner; }) => Promise; - execute: ({ client, txGroup, signedTxns, accountAddress }: { + execute: ({ client, txGroup, signedTxns }: { client: import("algosdk").Algodv2; txGroup: import("..").SignerTransaction[]; signedTxns: Uint8Array[]; - accountAddress: string; }) => Promise; }; }; diff --git a/dist/remove-liquidity/v2/index.d.ts b/dist/remove-liquidity/v2/index.d.ts index 0b851519..3edccb20 100644 --- a/dist/remove-liquidity/v2/index.d.ts +++ b/dist/remove-liquidity/v2/index.d.ts @@ -49,11 +49,10 @@ declare function signTxns({ txGroup, initiatorSigner }: { txGroup: SignerTransaction[]; initiatorSigner: InitiatorSigner; }): Promise; -declare function execute({ client, txGroup, signedTxns, accountAddress }: { +declare function execute({ client, txGroup, signedTxns }: { client: Algodv2; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; - accountAddress: string; }): Promise; export declare const RemoveLiquidityV2: { getQuote: typeof getQuote; diff --git a/dist/swap/index.d.ts b/dist/swap/index.d.ts index f973190a..677b5e52 100644 --- a/dist/swap/index.d.ts +++ b/dist/swap/index.d.ts @@ -89,12 +89,11 @@ export declare const Swap: { txGroup: import("..").SignerTransaction[]; initiatorSigner: import("..").InitiatorSigner; }) => Promise; - execute: ({ client, quote, txGroup, signedTxns, accountAddress }: { + execute: ({ client, quote, txGroup, signedTxns }: { client: import("algosdk").Algodv2; quote: import("./types").SwapQuote; txGroup: import("..").SignerTransaction[]; signedTxns: Uint8Array[]; - accountAddress: string; }) => Promise; calculateFixedInputSwap: ({ inputSupply, outputSupply, swapInputAmount, totalFeeShare, decimals }: { inputSupply: bigint; diff --git a/dist/swap/v2/index.d.ts b/dist/swap/v2/index.d.ts index dbf8f2cd..5032dd0c 100644 --- a/dist/swap/v2/index.d.ts +++ b/dist/swap/v2/index.d.ts @@ -12,12 +12,11 @@ declare function signTxns({ txGroup, initiatorSigner }: { /** * Executes a swap with the desired quantities. */ -declare function execute({ client, quote, txGroup, signedTxns, accountAddress }: { +declare function execute({ client, quote, txGroup, signedTxns }: { client: Algodv2; quote: SwapQuote; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; - accountAddress: string; }): Promise; /** * @param type - Type of the swap diff --git a/dist/util/transaction/transactionUtils.d.ts b/dist/util/transaction/transactionUtils.d.ts index 1510861b..6e4f5755 100644 --- a/dist/util/transaction/transactionUtils.d.ts +++ b/dist/util/transaction/transactionUtils.d.ts @@ -12,10 +12,16 @@ export declare function getAppCallInnerTxns(client: Algodv2, txGroup: SignerTran * inner transactions in the app call, and return the extracted asset data. * Useful for getting the asset data after an "execute" operation. */ -export declare function getAppCallInnerAssetData(client: Algodv2, txGroup: SignerTransaction[], accountAddress: string): Promise; +export declare function getAppCallInnerAssetData(client: Algodv2, txGroup: SignerTransaction[]): Promise; /** * Combines the provided signer transaction groups into one signer transaction group, with a new group ID * @param signerTransactions - The signer transaction groups to combine * @returns the combined signer transaction groups, with a new assigned group ID */ export declare function combineAndRegroupSignerTxns(...signerTransactions: SignerTransaction[][]): SignerTransaction[]; +/** + * Extracts the account address from the first transaction in the provided transaction group. + * @param txGroup - The transaction group to extract the account address from + * @returns the account address + */ +export declare function extractAccountAddressFromTxGroup(txGroup: SignerTransaction[]): string; diff --git a/src/add-liquidity/v2/common.ts b/src/add-liquidity/v2/common.ts index f86d6f24..7effa715 100644 --- a/src/add-liquidity/v2/common.ts +++ b/src/add-liquidity/v2/common.ts @@ -29,14 +29,12 @@ export async function execute({ client, pool, txGroup, - signedTxns, - accountAddress + signedTxns }: { client: Algodv2; pool: V2PoolInfo; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; - accountAddress: string; }): Promise { try { const [{confirmedRound, txnID}] = await sendAndWaitRawTransaction(client, [ @@ -46,7 +44,7 @@ export async function execute({ let assetOut: AssetWithIdAndAmount | undefined; try { - assetOut = (await getAppCallInnerAssetData(client, txGroup, accountAddress))?.find( + assetOut = (await getAppCallInnerAssetData(client, txGroup))?.find( // Output asset is the pool token for add liquidity ({id}) => id === pool.poolTokenID ); diff --git a/src/remove-liquidity/v2/index.ts b/src/remove-liquidity/v2/index.ts index 780aeae0..c0175c9f 100644 --- a/src/remove-liquidity/v2/index.ts +++ b/src/remove-liquidity/v2/index.ts @@ -319,19 +319,17 @@ function signTxns({ async function execute({ client, txGroup, - signedTxns, - accountAddress + signedTxns }: { client: Algodv2; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; - accountAddress: string; }): Promise { const [{txnID}] = await sendAndWaitRawTransaction(client, [signedTxns]); let outputAssets: AssetWithIdAndAmount[] | undefined; try { - outputAssets = await getAppCallInnerAssetData(client, txGroup, accountAddress); + outputAssets = await getAppCallInnerAssetData(client, txGroup); } catch (_error) { // We can ignore this error since the main execution was successful } diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index 3f15092d..423aec77 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -151,14 +151,12 @@ async function execute({ client, quote, txGroup, - signedTxns, - accountAddress + signedTxns }: { client: Algodv2; quote: SwapQuote; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; - accountAddress: string; }): Promise { const [{confirmedRound, txnID}] = await sendAndWaitRawTransaction(client, [signedTxns]); const assetOutId = getAssetOutFromSwapQuote(quote).id; @@ -166,7 +164,7 @@ async function execute({ let innerTxnAssetData: AssetWithIdAndAmount[] | undefined; try { - innerTxnAssetData = await getAppCallInnerAssetData(client, txGroup, accountAddress); + innerTxnAssetData = await getAppCallInnerAssetData(client, txGroup); } catch (_error) { // We can ignore this error since the main execution was successful } diff --git a/src/util/transaction/transactionUtils.ts b/src/util/transaction/transactionUtils.ts index 6346825a..7ec5d551 100644 --- a/src/util/transaction/transactionUtils.ts +++ b/src/util/transaction/transactionUtils.ts @@ -11,6 +11,7 @@ import {ALGO_ASSET_ID} from "../asset/assetConstants"; import {AssetWithIdAndAmount} from "../asset/assetModels"; import {SignerTransaction, TxnResponseInnerTxns} from "../commonTypes"; import {DEFAULT_WAIT_FOR_CONFIRMATION_ROUNDS} from "../constant"; +import TinymanError from "../error/TinymanError"; export async function getAppCallTxnResponse( client: Algodv2, @@ -48,10 +49,10 @@ export async function getAppCallInnerTxns( */ export async function getAppCallInnerAssetData( client: Algodv2, - txGroup: SignerTransaction[], - accountAddress: string + txGroup: SignerTransaction[] ): Promise { const innerTxns = await getAppCallInnerTxns(client, txGroup); + const accountAddress = extractAccountAddressFromTxGroup(txGroup); return innerTxns?.reduce((assets, {txn}) => { let updatedAssets = assets; @@ -111,3 +112,22 @@ export function combineAndRegroupSignerTxns( txn: newTxnGroup[index] })); } + +/** + * Extracts the account address from the first transaction in the provided transaction group. + * @param txGroup - The transaction group to extract the account address from + * @returns the account address + */ +export function extractAccountAddressFromTxGroup(txGroup: SignerTransaction[]): string { + if (!txGroup.length) { + throw new TinymanError(txGroup, "Transaction group should not be empty"); + } + + const senderAddress = txGroup[0].txn.from.publicKey; + + if (!senderAddress) { + throw new TinymanError(txGroup, "Transaction group should have a sender address"); + } + + return algosdk.encodeAddress(senderAddress); +} From f9c8548e0b3bcbe5dd86f2033c041d33ac381fa8 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Thu, 6 Apr 2023 15:56:45 +0300 Subject: [PATCH 33/39] fix(transaction/util): extract sender address from transaction, general code improvements --- dist/index.js | 2 +- dist/swap/types.d.ts | 4 --- dist/swap/utils.d.ts | 2 ++ dist/util/transaction/transactionUtils.d.ts | 10 +++---- src/swap/types.ts | 4 --- src/swap/utils.ts | 1 + src/swap/v2/router/constants.ts | 1 - src/util/transaction/transactionUtils.ts | 30 ++++++++------------- 8 files changed, 20 insertions(+), 34 deletions(-) diff --git a/dist/index.js b/dist/index.js index 8914bc0c..b06aa05c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=h(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={V1_1:"v1_1",V2:"v2"},C=0,j={id:"".concat(C),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},V={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},X="- would result negative",Q="logic eval error:",U="exceeds schema integer count",q=/transaction \w+:/,G=function(t){l(n,A(Error));var e=h(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function J(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Y(t){return Number(t)===C}function K(t){return Number(t.id)}function H(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function Z(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var $=100000n,tt=100000n,et=100000n,nt=25000n+25000n,rt=25000n+3500n;function at(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return $+tt*BigInt(o.length)+et*BigInt(s.length+a.length)+rt*r+nt*n}function st(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function ot(t,e){return it.apply(this,arguments)}function it(){return(it=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,st(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ut(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function ct(t,e){var n=Number(t);return pt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function pt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=x(dt(e),2),s=a[0],o=a[1],i=x(dt(Math.round(Number(lt(s,o+r)))),2),u=i[0],c=i[1];return Number(lt(u,c-r))}return Math.round(e)}function lt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function dt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function ft(t,e){return mt.apply(this,arguments)}function mt(){return(mt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,ot(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new G(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function gt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function At(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function It(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ht(t){return(new TextEncoder).encode(t)}var xt,vt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===M.V1_1?"v1":t;return ht("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),Tt=(p(xt={},M.V1_1,{testnet:62368684,mainnet:552635992}),p(xt,M.V2,{testnet:148607e3,mainnet:1002541853}),xt);function yt(t,e){var n=Tt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function wt(){return(wt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var bt,Et,Dt,St=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Ot=.003,Nt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(bt={},St.FixedInput,1),p(bt,St.FixedOutput,2),bt),kt=2,Lt=ht("swap"),Rt=(p(Et={},St.FixedInput,ht("fixed-input")),p(Et,St.FixedOutput,ht("fixed-output")),Et),Ft={ASSET_OPT_IN:ht("asset_opt_in")},Bt={testnet:159521633,mainnet:1071281873},Mt=(p(Dt={},St.FixedInput,7),p(Dt,St.FixedOutput,8),Dt),Ct={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return ct(r.asset.decimals,Number(r.amount))/ct(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Bt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Ft.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[K(u[0].quote.amount_in.asset),K(u[0].quote.amount_out.asset),K(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],h=[u[0].pool.address,u[1].pool.address],x=h[0],v=h[1],T=i===St.FixedInput?A:ut("positive",c,A),y=i===St.FixedOutput?I:ut("negative",c,I),w=Y(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Lt,Rt[i],t.encodeUint64(y)],foreignApps:[yt(o,M.V2)],foreignAssets:[d,f,m],accounts:[x,v],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Mt[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==C&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Ct[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(Pt[e]+kt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Ot;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Ot)}function re(e){return(Pt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return ct(n.decimals,Number(n.amount))/ct(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=ct(a.decimals,Number(n))/ct(r.decimals,Number(e));return pt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:M.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=h(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=yt(n.network,M.V1_1),a=x(J(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?H({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=ht("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:h=H({stateArray:I}),x=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=h[x],_=h[v],b=h[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,h,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=H({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,yt(s,M.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[M.V1_1].asset1],asset2ID:u[Oe[M.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=xe(M.V2),u=i.generateLogicSigAccountForPool(e),c=yt(r,M.V2),p=u.address(),l=J(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:M.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,yt(s,M.V2)),u=null,i&&(u={asset1ID:i[Oe[M.V2].asset1],asset2ID:i[Oe[M.V2].asset2],poolTokenID:i[Oe[M.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=ct(t.asset1,e.asset1)/ct(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},M.V1_1,r(r({},Me),Ge)),p(qe,M.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=z(o,i),l=x(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,h=Y(A),v=yt(s,M.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:vt.getAppCallTxnNoteWithClientName(M.V1_1),appArgs:[ht("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:V.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(h),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,h||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=x(c,1),l=p[0],d=J(i,u),f=x(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],h=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:h,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,ot(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r){var s,o;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return s=e.sent,o=ln(r),e.abrupt("return",null==s?void 0:s.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer&&t.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&t.encodeAddress(a.rcv)===o&&r.push({id:C,amount:a.amt}),r}),[]));case 5:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e){if(!e.length)throw new G(e,"Transaction group should not be empty");var n=e[0].txn.from.publicKey;if(!n)throw new G(e,"Transaction group should have a sender address");return t.encodeAddress(n)}function dn(){return(dn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=yt(s,M.V2),l=t.getApplicationAddress(p),d=z(o,i),f=x(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=he.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),h=Y(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[ht("bootstrap")],note:vt.getAppCallTxnNoteWithClientName(M.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=mn(h),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:fn(h),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fn(t){return function(t,e){var n=xe(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(M.V2,t)+mn(t)+O}function mn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function gn(){return(gn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=x(c,1),l=p[0],d=J(i,u),f=x(d,2),m=f[0],g=f[1],A=he.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],h=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:h,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function An(){return(An=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new G(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var In,hn={generateTxns:function(t){return dn.apply(this,arguments)},signTxns:function(t){return gn.apply(this,arguments)},execute:function(t){return An.apply(this,arguments)},getBootstrapFundingTxnAmount:fn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+fn(e)}};var xn,vn,Tn,yn,wn=(p(In={},M.V1_1,en),p(In,M.V2,hn),p(In,"generateTxns",(function(t){return t.contractVersion===M.V1_1?en.generateTxns(t):hn.generateTxns(t)})),p(In,"signTxns",(function(t){return t.contractVersion===M.V1_1?en.signTxns(t):hn.signTxns(t)})),p(In,"execute",(function(t){return t.contractVersion===M.V1_1?en.execute(t):hn.execute(t)})),p(In,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===M.V1_1?en.getBootstrapFundingTxnAmount(n):hn.getBootstrapFundingTxnAmount(n)})),In),_n=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),bn=5*t.ALGORAND_MIN_TX_FEE,En=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),Dn=(p(xn={},En.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,En.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,En.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Sn=(p(vn={},En.INITIAL,1),p(vn,En.SINGLE,2),p(vn,En.FLEXIBLE,2),vn),On=(p(Tn={},En.INITIAL,3),p(Tn,En.FLEXIBLE,3),p(Tn,En.SINGLE,2),Tn);function Nn(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var h=A;r=kn(h,s),e={id:o.id,amount:h+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var x=I;r=kn(x,s),e={id:i.id,amount:x+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Pn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Ln(e){return(Sn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Rn=ht("add_liquidity"),Fn=(p(yn={},M.V1_1,[ht("mint")]),p(yn,M.V2,{INITIAL_LIQUIDITY:[ht("add_initial_liquidity")],SINGLE_ASSET_MODE:[Rn,ht("single")],FLEXIBLE_MODE:[Rn,ht("flexible")]}),yn);function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ut("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:yt(s,M.V1_1),appArgs:Fn.v1_1,accounts:[l],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:u.id==C?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),h=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),x=t.assignGroupID([h,m,g,A,I]),e.abrupt("return",[{txn:x[0],signers:[l]},{txn:x[1],signers:[o]},{txn:x[2],signers:[l]},{txn:x[3],signers:[l]},{txn:x[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=x(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===_n.FEE_TXN?p:n===_n.ASSET1_IN_TXN?l:n===_n.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Cn(){return(Cn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[_n.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,ft(n,[o]);case 8:return p=t.sent,l=x(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=gt(s),A=At(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(h=I.excessPoolTokens-c.excessPoolTokens)<0n&&(h=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+h,excessAmount:{excessAmountForAddingLiquidity:h,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new G(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var jn=Object.freeze({__proto__:null,execute:function(t){return Cn.apply(this,arguments)},generateTxns:function(t){return Bn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:ct(p,Number(m))/ct(u,Number(l)),priceImpact:A}}function dr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:ct(p,Number(l))/ct(u,Number(m)),priceImpact:A}}}}function fr(t){return mr.apply(this,arguments)}function mr(){return(mr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=lr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,pr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function gr(t){return Ar.apply(this,arguments)}function Ar(){return(Ar=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(dr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,pr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ir(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var hr={getQuote:function(t){return cr.apply(this,arguments)},getFixedInputSwapQuote:fr,getFixedInputDirectSwapQuote:lr,getFixedOutputDirectSwapQuote:dr,getFixedOutputSwapQuote:gr,generateTxns:function(t){return ir.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ur.apply(this,arguments)},calculateFixedInputSwap:Ir};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[er,t.encodeUint64(ut("negative",p,u)),t.encodeUint64(ut("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=(tr+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[nr.ASSET_TRANSFER_TXN]=m,A[nr.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[nr.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[nr.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,h=ut("negative",p,c),u!==d){e.next=17;break}A=h,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=h,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return x=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[er,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(tr+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[nr.ASSET_TRANSFER_TXN]=x,T[nr.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[nr.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[nr.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function yr(){return(yr=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,t.next=3,ft(n,[s]);case 3:return o=t.sent,i=x(o,1),u=i[0].txnID,t.prev=6,t.next=9,cn(n,r);case 9:c=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var wr,_r={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=hr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=hr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,h=A.totalFeeAmount,x=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:h},priceImpact:x}}}return e},generateTxns:function(t){return vr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return Tr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return yr.apply(this,arguments)}},br=(p(wr={},M.V1_1,or),p(wr,M.V2,_r),wr),Er=3n,Dr=1000n,Sr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Sr||{});function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=x(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(){return(Nr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new G({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[ht("swap"),o===St.FixedInput?ht("fi"):ht("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:c.asset2ID==C?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),h=o===St.FixedOutput?ut("positive",i,p.assetInAmount):p.assetInAmount,x=l===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,suggestedParams:g}),v=o===St.FixedInput?ut("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,x,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Pr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Dr,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function kr(t){return Lr.apply(this,arguments)}function Lr(){return(Lr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,h;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=x(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(h=I-A)<0n&&(h=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+h,excessAmount:{assetID:i.id,excessAmountForSwap:h,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Dr/(Dr-Er),l=p-c,d=ct(a.assetOut,Number(u))/ct(a.assetIn,Number(p)),f=ct(a.assetOut,Number(i))/ct(a.assetIn,Number(o)),m=pt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Fr(t){return Br.apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,h;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=x(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(h=I-A)<0n&&(h=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-h,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:h,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Mr(){return(Mr=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new G({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||C,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||C,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==St.FixedInput){t.next=12;break}return t.next=9,kr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Fr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:At(i),fees:gt(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new G(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Cr,jr={getQuote:function(t,e,n,r,a){return t===St.FixedInput?Pr({pool:e,reserves:n,assetIn:r,decimals:a}):Rr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Pr,getFixedOutputSwapQuote:Rr,generateTxns:function(t){return Nr.apply(this,arguments)},signTxns:function(t){return Or.apply(this,arguments)},execute:function(t){return Mr.apply(this,arguments)},executeFixedOutputSwap:Fr};function Vr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=x(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===M.V2})),c.push(hr.getFixedInputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,Vr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Qr(){return(Qr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===M.V2})),c.push(hr.getFixedOutputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,Vr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Ur=(p(Cr={},M.V1_1,jr),p(Cr,M.V2,hr),p(Cr,"getQuote",(function(t){var e=t.type;if(e===St.FixedInput)return function(t){return Xr.apply(this,arguments)}(t);if(e===St.FixedOutput)return function(t){return Qr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Cr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1?jr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):hr.generateTxns(t)})),p(Cr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1){var e=t.quote.data.pool;return jr.signTxns(r(r({},t),{},{pool:e}))}return hr.signTxns(t)})),p(Cr,"execute",(function(t){return t.contractVersion===M.V1_1?jr.execute(t):hr.execute(t)})),Cr);function qr(){return(qr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,Gr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,ft(n,[i]);case 7:return u=t.sent,c=x(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:gt(s),confirmedRound:d,txnID:l,groupID:At(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function Gr(t){return Wr.apply(this,arguments)}function Wr(){return(Wr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=x(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zr(){return zr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:gt(e),groupID:At(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,ft(r,[u]);case 4:p=a.sent,l=x(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new G(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),zr.apply(this,arguments)}function Jr(){return(Jr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[ht("redeem")],note:vt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Yr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ht("commit"),u],note:Z([ht("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Kr(){return(Kr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Yr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[ht("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=j,exports.ALGO_ASSET_ID=C,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Hn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=wn,exports.CONTRACT_VERSION=M,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=V,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=br,exports.Swap=Ur,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=St,exports.V1_1AddLiquidityTxnIndices=_n,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=$n,exports.V2AddLiquidityTxnIndices=Dn,exports.V2AddLiquidityType=En,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=tr,exports.applySlippageToAmount=ut,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Kr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return zr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=ft,exports.sumUpTxnFees=gt,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=he,exports.tinymanJSSDKConfig=vt; +"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={V1_1:"v1_1",V2:"v2"},C=0,j={id:"".concat(C),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},V={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},X="- would result negative",Q="logic eval error:",U="exceeds schema integer count",q=/transaction \w+:/,G=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function J(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Y(t){return Number(t)===C}function K(t){return Number(t.id)}function H(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function Z(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var $=100000n,tt=100000n,et=100000n,nt=25000n+25000n,rt=25000n+3500n;function at(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return $+tt*BigInt(o.length)+et*BigInt(s.length+a.length)+rt*r+nt*n}function st(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function ot(t,e){return it.apply(this,arguments)}function it(){return(it=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,st(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ut(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function ct(t,e){var n=Number(t);return pt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function pt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=h(dt(e),2),s=a[0],o=a[1],i=h(dt(Math.round(Number(lt(s,o+r)))),2),u=i[0],c=i[1];return Number(lt(u,c-r))}return Math.round(e)}function lt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function dt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function ft(t,e){return mt.apply(this,arguments)}function mt(){return(mt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,ot(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new G(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function gt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function At(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function It(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function xt(t){return(new TextEncoder).encode(t)}var ht,vt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===M.V1_1?"v1":t;return xt("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),Tt=(p(ht={},M.V1_1,{testnet:62368684,mainnet:552635992}),p(ht,M.V2,{testnet:148607e3,mainnet:1002541853}),ht);function yt(t,e){var n=Tt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function wt(){return(wt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var bt,Et,Dt,St=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Ot=.003,Nt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(bt={},St.FixedInput,1),p(bt,St.FixedOutput,2),bt),kt=2,Lt=xt("swap"),Rt=(p(Et={},St.FixedInput,xt("fixed-input")),p(Et,St.FixedOutput,xt("fixed-output")),Et),Ft={ASSET_OPT_IN:xt("asset_opt_in")},Bt={testnet:159521633,mainnet:1071281873},Mt=(p(Dt={},St.FixedInput,7),p(Dt,St.FixedOutput,8),Dt),Ct={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return ct(r.asset.decimals,Number(r.amount))/ct(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Bt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Ft.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[K(u[0].quote.amount_in.asset),K(u[0].quote.amount_out.asset),K(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],x=[u[0].pool.address,u[1].pool.address],h=x[0],v=x[1],T=i===St.FixedInput?A:ut("positive",c,A),y=i===St.FixedOutput?I:ut("negative",c,I),w=Y(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Lt,Rt[i],t.encodeUint64(y)],foreignApps:[yt(o,M.V2)],foreignAssets:[d,f,m],accounts:[h,v],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Mt[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==C&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Ct[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(Pt[e]+kt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Ot;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Ot)}function re(e){return(Pt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return ct(n.decimals,Number(n.amount))/ct(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=ct(a.decimals,Number(n))/ct(r.decimals,Number(e));return pt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:M.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=x(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=yt(n.network,M.V1_1),a=h(J(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?H({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=xt("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:x=H({stateArray:I}),h=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=x[h],_=x[v],b=x[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=H({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,yt(s,M.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[M.V1_1].asset1],asset2ID:u[Oe[M.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=he(M.V2),u=i.generateLogicSigAccountForPool(e),c=yt(r,M.V2),p=u.address(),l=J(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:M.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,yt(s,M.V2)),u=null,i&&(u={asset1ID:i[Oe[M.V2].asset1],asset2ID:i[Oe[M.V2].asset2],poolTokenID:i[Oe[M.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=ct(t.asset1,e.asset1)/ct(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},M.V1_1,r(r({},Me),Ge)),p(qe,M.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=z(o,i),l=h(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,x=Y(A),v=yt(s,M.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:vt.getAppCallTxnNoteWithClientName(M.V1_1),appArgs:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:x?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:V.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(x),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,x||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=J(i,u),f=h(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,ot(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r){var s,o;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return s=e.sent,a=r[0].txn,o=t.encodeAddress(a.from.publicKey),e.abrupt("return",null==s?void 0:s.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer&&t.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&t.encodeAddress(a.rcv)===o&&r.push({id:C,amount:a.amt}),r}),[]));case 5:case"end":return e.stop()}var a}),e)})))).apply(this,arguments)}function ln(){return(ln=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=yt(s,M.V2),l=t.getApplicationAddress(p),d=z(o,i),f=h(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),x=Y(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:vt.getAppCallTxnNoteWithClientName(M.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=fn(x),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:dn(x),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function dn(t){return function(t,e){var n=he(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(M.V2,t)+fn(t)+O}function fn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function mn(){return(mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=J(i,u),f=h(d,2),m=f[0],g=f[1],A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(){return(gn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new G(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(t){return ln.apply(this,arguments)},signTxns:function(t){return mn.apply(this,arguments)},execute:function(t){return gn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+dn(e)}};var xn,hn,vn,Tn,yn=(p(An={},M.V1_1,en),p(An,M.V2,In),p(An,"generateTxns",(function(t){return t.contractVersion===M.V1_1?en.generateTxns(t):In.generateTxns(t)})),p(An,"signTxns",(function(t){return t.contractVersion===M.V1_1?en.signTxns(t):In.signTxns(t)})),p(An,"execute",(function(t){return t.contractVersion===M.V1_1?en.execute(t):In.execute(t)})),p(An,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===M.V1_1?en.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),wn=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),_n=5*t.ALGORAND_MIN_TX_FEE,bn=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),En=(p(xn={},bn.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,bn.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,bn.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Dn=(p(hn={},bn.INITIAL,1),p(hn,bn.SINGLE,2),p(hn,bn.FLEXIBLE,2),hn),Sn=(p(vn={},bn.INITIAL,3),p(vn,bn.FLEXIBLE,3),p(vn,bn.SINGLE,2),vn);function On(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var x=A;r=Pn(x,s),e={id:o.id,amount:x+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=Pn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Nn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function Pn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function kn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Rn=(p(Tn={},M.V1_1,[xt("mint")]),p(Tn,M.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Fn(){return(Fn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ut("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:yt(s,M.V1_1),appArgs:Rn.v1_1,accounts:[l],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:u.id==C?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),x=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),h=t.assignGroupID([x,m,g,A,I]),e.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=h(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===wn.FEE_TXN?p:n===wn.ASSET1_IN_TXN?l:n===wn.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[wn.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,ft(n,[o]);case 8:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=gt(s),A=At(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(x=I.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new G(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Cn=Object.freeze({__proto__:null,execute:function(t){return Mn.apply(this,arguments)},generateTxns:function(t){return Fn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:ct(p,Number(m))/ct(u,Number(l)),priceImpact:A}}function lr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:ct(p,Number(l))/ct(u,Number(m)),priceImpact:A}}}}function dr(t){return fr.apply(this,arguments)}function fr(){return(fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=pr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,cr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(lr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,cr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var Ir={getQuote:function(t){return ur.apply(this,arguments)},getFixedInputSwapQuote:dr,getFixedInputDirectSwapQuote:pr,getFixedOutputDirectSwapQuote:lr,getFixedOutputSwapQuote:mr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ir.apply(this,arguments)},calculateFixedInputSwap:Ar};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function hr(){return(hr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[tr,t.encodeUint64(ut("negative",p,u)),t.encodeUint64(ut("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[er.ASSET_TRANSFER_TXN]=m,A[er.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[er.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[er.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,x=ut("negative",p,c),u!==d){e.next=17;break}A=x,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=x,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return h=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[tr,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[er.ASSET_TRANSFER_TXN]=h,T[er.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[er.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[er.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,t.next=3,ft(n,[s]);case 3:return o=t.sent,i=h(o,1),u=i[0].txnID,t.prev=6,t.next=9,cn(n,r);case 9:c=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var yr,wr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ir.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=Ir.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,x=A.totalFeeAmount,h=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:h}}}return e},generateTxns:function(t){return hr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Tr.apply(this,arguments)}},_r=(p(yr={},M.V1_1,sr),p(yr,M.V2,wr),yr),br=3n,Er=1000n,Dr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Dr||{});function Sr(){return(Sr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=h(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Dr.FEE_TXN_INDEX?c:n===Dr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new G({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[xt("swap"),o===St.FixedInput?xt("fi"):xt("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:c.asset2ID==C?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),x=o===St.FixedOutput?ut("positive",i,p.assetInAmount):p.assetInAmount,h=l===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:x,suggestedParams:g}),v=o===St.FixedInput?ut("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,h,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*br/Er,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function Pr(t){return kr.apply(this,arguments)}function kr(){return(kr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+x,excessAmount:{assetID:i.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Er/(Er-br),l=p-c,d=ct(a.assetOut,Number(u))/ct(a.assetIn,Number(p)),f=ct(a.assetOut,Number(i))/ct(a.assetIn,Number(o)),m=pt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Rr(t){return Fr.apply(this,arguments)}function Fr(){return(Fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-x,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new G({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Dr.ASSET_IN_TXN_INDEX].txn.assetIndex||C,amount:i[Dr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Dr.ASSET_OUT_TXN_INDEX].txn.assetIndex||C,amount:i[Dr.ASSET_OUT_TXN_INDEX].txn.amount},o!==St.FixedInput){t.next=12;break}return t.next=9,Pr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Rr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:At(i),fees:gt(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new G(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Mr,Cr={getQuote:function(t,e,n,r,a){return t===St.FixedInput?Nr({pool:e,reserves:n,assetIn:r,decimals:a}):Lr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Lr,generateTxns:function(t){return Or.apply(this,arguments)},signTxns:function(t){return Sr.apply(this,arguments)},execute:function(t){return Br.apply(this,arguments)},executeFixedOutputSwap:Rr};function jr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=h(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Vr(){return(Vr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===M.V2})),c.push(Ir.getFixedInputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===M.V2})),c.push(Ir.getFixedOutputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qr=(p(Mr={},M.V1_1,Cr),p(Mr,M.V2,Ir),p(Mr,"getQuote",(function(t){var e=t.type;if(e===St.FixedInput)return function(t){return Vr.apply(this,arguments)}(t);if(e===St.FixedOutput)return function(t){return Xr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Mr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1?Cr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):Ir.generateTxns(t)})),p(Mr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1){var e=t.quote.data.pool;return Cr.signTxns(r(r({},t),{},{pool:e}))}return Ir.signTxns(t)})),p(Mr,"execute",(function(t){return t.contractVersion===M.V1_1?Cr.execute(t):Ir.execute(t)})),Mr);function Ur(){return(Ur=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,qr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,ft(n,[i]);case 7:return u=t.sent,c=h(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:gt(s),confirmedRound:d,txnID:l,groupID:At(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function qr(t){return Gr.apply(this,arguments)}function Gr(){return(Gr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=h(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wr(){return Wr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:gt(e),groupID:At(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,ft(r,[u]);case 4:p=a.sent,l=h(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new G(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[xt("redeem")],note:vt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[xt("commit"),u],note:Z([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Yr(){return(Yr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Jr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[xt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=j,exports.ALGO_ASSET_ID=C,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Kn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=yn,exports.CONTRACT_VERSION=M,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=V,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=_r,exports.Swap=Qr,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=St,exports.V1_1AddLiquidityTxnIndices=wn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Zn,exports.V2AddLiquidityTxnIndices=En,exports.V2AddLiquidityType=bn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=$n,exports.applySlippageToAmount=ut,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Ur.apply(this,arguments)},exports.sendAndWaitRawTransaction=ft,exports.sumUpTxnFees=gt,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=xe,exports.tinymanJSSDKConfig=vt; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index 3447eb98..94143b80 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -148,11 +148,7 @@ export interface V2SwapExecution { round: number; } export interface ExecuteSwapCommonParams { - swapType: SwapType; client: Algodv2; - network: SupportedNetwork; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; - accountAddress: string; - assetIn: AssetWithIdAndAmount; } diff --git a/dist/swap/utils.d.ts b/dist/swap/utils.d.ts index f5b55d60..cf404059 100644 --- a/dist/swap/utils.d.ts +++ b/dist/swap/utils.d.ts @@ -2,6 +2,7 @@ import { CONTRACT_VERSION } from "../contract/constants"; import { InitiatorSigner, SignerTransaction } from "../util/commonTypes"; import { V1PoolInfo } from "../util/pool/poolTypes"; import { GetSwapQuoteBySwapTypeParams, GenerateSwapTxnsParams, GetSwapQuoteParams, SwapQuote, ExecuteSwapCommonParams } from "./types"; +import { SwapType } from "./constants"; /** * Gets the best quote for swap from the pools and swap router and returns the best option. */ @@ -26,6 +27,7 @@ export declare function execute(params: ({ contractVersion: typeof CONTRACT_VERSION.V1_1; initiatorAddr: string; pool: V1PoolInfo; + swapType: SwapType; } | { contractVersion: typeof CONTRACT_VERSION.V2; quote: SwapQuote; diff --git a/dist/util/transaction/transactionUtils.d.ts b/dist/util/transaction/transactionUtils.d.ts index 6e4f5755..ac42a434 100644 --- a/dist/util/transaction/transactionUtils.d.ts +++ b/dist/util/transaction/transactionUtils.d.ts @@ -1,4 +1,4 @@ -import { Algodv2 } from "algosdk"; +import { Algodv2, Transaction } from "algosdk"; import { AssetWithIdAndAmount } from "../asset/assetModels"; import { SignerTransaction, TxnResponseInnerTxns } from "../commonTypes"; export declare function getAppCallTxnResponse(client: Algodv2, txGroup: SignerTransaction[]): Promise | undefined>; @@ -20,8 +20,8 @@ export declare function getAppCallInnerAssetData(client: Algodv2, txGroup: Signe */ export declare function combineAndRegroupSignerTxns(...signerTransactions: SignerTransaction[][]): SignerTransaction[]; /** - * Extracts the account address from the first transaction in the provided transaction group. - * @param txGroup - The transaction group to extract the account address from - * @returns the account address + * Extracts the account address from the provided transaction. + * @param txn - The transaction to extract the sender address from + * @returns the account address of the sender */ -export declare function extractAccountAddressFromTxGroup(txGroup: SignerTransaction[]): string; +export declare function extractSenderAddressFromTransaction(txn: Transaction): string; diff --git a/src/swap/types.ts b/src/swap/types.ts index 7765b5e6..79d60e5f 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -168,11 +168,7 @@ export interface V2SwapExecution { } export interface ExecuteSwapCommonParams { - swapType: SwapType; client: Algodv2; - network: SupportedNetwork; txGroup: SignerTransaction[]; signedTxns: Uint8Array[]; - accountAddress: string; - assetIn: AssetWithIdAndAmount; } diff --git a/src/swap/utils.ts b/src/swap/utils.ts index bc63632d..602aadd9 100644 --- a/src/swap/utils.ts +++ b/src/swap/utils.ts @@ -239,6 +239,7 @@ export function execute( contractVersion: typeof CONTRACT_VERSION.V1_1; initiatorAddr: string; pool: V1PoolInfo; + swapType: SwapType; } | {contractVersion: typeof CONTRACT_VERSION.V2; quote: SwapQuote} ) & diff --git a/src/swap/v2/router/constants.ts b/src/swap/v2/router/constants.ts index 4a594a93..1152f60c 100644 --- a/src/swap/v2/router/constants.ts +++ b/src/swap/v2/router/constants.ts @@ -13,7 +13,6 @@ export const SWAP_ROUTER_APP_ID: Record = { export const SWAP_ROUTER_INNER_TXN_COUNT: Record = { [SwapType.FixedInput]: 7, [SwapType.FixedOutput]: 8 - // ... } as const; const TINYMAN_ANALYTICS_API_BASE_URLS: Record< diff --git a/src/util/transaction/transactionUtils.ts b/src/util/transaction/transactionUtils.ts index 7ec5d551..bc660620 100644 --- a/src/util/transaction/transactionUtils.ts +++ b/src/util/transaction/transactionUtils.ts @@ -3,6 +3,7 @@ import algosdk, { assignGroupID, decodeUnsignedTransaction, encodeUnsignedTransaction, + Transaction, TransactionType, waitForConfirmation } from "algosdk"; @@ -11,7 +12,6 @@ import {ALGO_ASSET_ID} from "../asset/assetConstants"; import {AssetWithIdAndAmount} from "../asset/assetModels"; import {SignerTransaction, TxnResponseInnerTxns} from "../commonTypes"; import {DEFAULT_WAIT_FOR_CONFIRMATION_ROUNDS} from "../constant"; -import TinymanError from "../error/TinymanError"; export async function getAppCallTxnResponse( client: Algodv2, @@ -52,7 +52,9 @@ export async function getAppCallInnerAssetData( txGroup: SignerTransaction[] ): Promise { const innerTxns = await getAppCallInnerTxns(client, txGroup); - const accountAddress = extractAccountAddressFromTxGroup(txGroup); + // Get the account address that will receive the assets after a certain operation is successful. + // This equals to the address of the account that signs the txGroup. + const receivingAccountAddress = extractSenderAddressFromTransaction(txGroup[0].txn); return innerTxns?.reduce((assets, {txn}) => { let updatedAssets = assets; @@ -60,7 +62,7 @@ export async function getAppCallInnerAssetData( if ( innerTxn.type === TransactionType.axfer && - algosdk.encodeAddress(innerTxn.arcv) === accountAddress + algosdk.encodeAddress(innerTxn.arcv) === receivingAccountAddress ) { updatedAssets.push({ id: innerTxn.xaid, @@ -68,7 +70,7 @@ export async function getAppCallInnerAssetData( }); } else if ( innerTxn.type === TransactionType.pay && - algosdk.encodeAddress(innerTxn.rcv) === accountAddress + algosdk.encodeAddress(innerTxn.rcv) === receivingAccountAddress ) { updatedAssets.push({ // Payment transactions are always in ALGO @@ -114,20 +116,10 @@ export function combineAndRegroupSignerTxns( } /** - * Extracts the account address from the first transaction in the provided transaction group. - * @param txGroup - The transaction group to extract the account address from - * @returns the account address + * Extracts the account address from the provided transaction. + * @param txn - The transaction to extract the sender address from + * @returns the account address of the sender */ -export function extractAccountAddressFromTxGroup(txGroup: SignerTransaction[]): string { - if (!txGroup.length) { - throw new TinymanError(txGroup, "Transaction group should not be empty"); - } - - const senderAddress = txGroup[0].txn.from.publicKey; - - if (!senderAddress) { - throw new TinymanError(txGroup, "Transaction group should have a sender address"); - } - - return algosdk.encodeAddress(senderAddress); +export function extractSenderAddressFromTransaction(txn: Transaction): string { + return algosdk.encodeAddress(txn.from.publicKey); } From a9658a284e3121c916083236910739df0624798e Mon Sep 17 00:00:00 2001 From: anlerkan Date: Mon, 10 Apr 2023 13:15:05 +0300 Subject: [PATCH 34/39] chore: move analytic api base urls constant --- dist/index.js | 2 +- dist/swap/v2/router/constants.d.ts | 5 ----- dist/util/constant.d.ts | 5 +++++ src/swap/v2/router/constants.ts | 16 ---------------- src/swap/v2/router/swap-router.ts | 3 ++- src/util/constant.ts | 16 ++++++++++++++++ 6 files changed, 24 insertions(+), 23 deletions(-) diff --git a/dist/index.js b/dist/index.js index b06aa05c..762a49fc 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={V1_1:"v1_1",V2:"v2"},C=0,j={id:"".concat(C),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},V={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},X="- would result negative",Q="logic eval error:",U="exceeds schema integer count",q=/transaction \w+:/,G=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function J(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Y(t){return Number(t)===C}function K(t){return Number(t.id)}function H(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function Z(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var $=100000n,tt=100000n,et=100000n,nt=25000n+25000n,rt=25000n+3500n;function at(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return $+tt*BigInt(o.length)+et*BigInt(s.length+a.length)+rt*r+nt*n}function st(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function ot(t,e){return it.apply(this,arguments)}function it(){return(it=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,st(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ut(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function ct(t,e){var n=Number(t);return pt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function pt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=h(dt(e),2),s=a[0],o=a[1],i=h(dt(Math.round(Number(lt(s,o+r)))),2),u=i[0],c=i[1];return Number(lt(u,c-r))}return Math.round(e)}function lt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function dt(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function ft(t,e){return mt.apply(this,arguments)}function mt(){return(mt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,ot(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new G(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function gt(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function At(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function It(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function xt(t){return(new TextEncoder).encode(t)}var ht,vt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===M.V1_1?"v1":t;return xt("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),Tt=(p(ht={},M.V1_1,{testnet:62368684,mainnet:552635992}),p(ht,M.V2,{testnet:148607e3,mainnet:1002541853}),ht);function yt(t,e){var n=Tt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function wt(){return(wt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:yt(s,o),note:vt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var bt,Et,Dt,St=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Ot=.003,Nt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(bt={},St.FixedInput,1),p(bt,St.FixedOutput,2),bt),kt=2,Lt=xt("swap"),Rt=(p(Et={},St.FixedInput,xt("fixed-input")),p(Et,St.FixedOutput,xt("fixed-output")),Et),Ft={ASSET_OPT_IN:xt("asset_opt_in")},Bt={testnet:159521633,mainnet:1071281873},Mt=(p(Dt={},St.FixedInput,7),p(Dt,St.FixedOutput,8),Dt),Ct={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}};function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return ct(r.asset.decimals,Number(r.amount))/ct(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Bt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Ft.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[K(u[0].quote.amount_in.asset),K(u[0].quote.amount_out.asset),K(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],x=[u[0].pool.address,u[1].pool.address],h=x[0],v=x[1],T=i===St.FixedInput?A:ut("positive",c,A),y=i===St.FixedOutput?I:ut("negative",c,I),w=Y(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Lt,Rt[i],t.encodeUint64(y)],foreignApps:[yt(o,M.V2)],foreignAssets:[d,f,m],accounts:[h,v],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Mt[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==C&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(Ct[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(Pt[e]+kt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Ot;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Ot)}function re(e){return(Pt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return ct(n.decimals,Number(n.amount))/ct(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=ct(a.decimals,Number(n))/ct(r.decimals,Number(e));return pt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:K(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:M.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=x(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=yt(n.network,M.V1_1),a=h(J(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?H({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=xt("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:x=H({stateArray:I}),h=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray(Z([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=x[h],_=x[v],b=x[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=H({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,yt(s,M.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[M.V1_1].asset1],asset2ID:u[Oe[M.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=he(M.V2),u=i.generateLogicSigAccountForPool(e),c=yt(r,M.V2),p=u.address(),l=J(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:M.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,yt(s,M.V2)),u=null,i&&(u={asset1ID:i[Oe[M.V2].asset1],asset2ID:i[Oe[M.V2].asset2],poolTokenID:i[Oe[M.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=ct(t.asset1,e.asset1)/ct(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},M.V1_1,r(r({},Me),Ge)),p(qe,M.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=z(o,i),l=h(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,x=Y(A),v=yt(s,M.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:vt.getAppCallTxnNoteWithClientName(M.V1_1),appArgs:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:x?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:V.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(x),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,x||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=J(i,u),f=h(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,ot(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r){var s,o;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return s=e.sent,a=r[0].txn,o=t.encodeAddress(a.from.publicKey),e.abrupt("return",null==s?void 0:s.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer&&t.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&t.encodeAddress(a.rcv)===o&&r.push({id:C,amount:a.amt}),r}),[]));case 5:case"end":return e.stop()}var a}),e)})))).apply(this,arguments)}function ln(){return(ln=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=yt(s,M.V2),l=t.getApplicationAddress(p),d=z(o,i),f=h(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),x=Y(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:vt.getAppCallTxnNoteWithClientName(M.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=fn(x),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:dn(x),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function dn(t){return function(t,e){var n=he(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(M.V2,t)+fn(t)+O}function fn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function mn(){return(mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=J(i,u),f=h(d,2),m=f[0],g=f[1],A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(){return(gn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new G(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(t){return ln.apply(this,arguments)},signTxns:function(t){return mn.apply(this,arguments)},execute:function(t){return gn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+dn(e)}};var xn,hn,vn,Tn,yn=(p(An={},M.V1_1,en),p(An,M.V2,In),p(An,"generateTxns",(function(t){return t.contractVersion===M.V1_1?en.generateTxns(t):In.generateTxns(t)})),p(An,"signTxns",(function(t){return t.contractVersion===M.V1_1?en.signTxns(t):In.signTxns(t)})),p(An,"execute",(function(t){return t.contractVersion===M.V1_1?en.execute(t):In.execute(t)})),p(An,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===M.V1_1?en.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),wn=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),_n=5*t.ALGORAND_MIN_TX_FEE,bn=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),En=(p(xn={},bn.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,bn.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,bn.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Dn=(p(hn={},bn.INITIAL,1),p(hn,bn.SINGLE,2),p(hn,bn.FLEXIBLE,2),hn),Sn=(p(vn={},bn.INITIAL,3),p(vn,bn.FLEXIBLE,3),p(vn,bn.SINGLE,2),vn);function On(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var x=A;r=Pn(x,s),e={id:o.id,amount:x+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=Pn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Nn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function Pn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function kn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Rn=(p(Tn={},M.V1_1,[xt("mint")]),p(Tn,M.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Fn(){return(Fn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ut("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:yt(s,M.V1_1),appArgs:Rn.v1_1,accounts:[l],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:u.id==C?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),x=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),h=t.assignGroupID([x,m,g,A,I]),e.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=h(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===wn.FEE_TXN?p:n===wn.ASSET1_IN_TXN?l:n===wn.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[wn.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,ft(n,[o]);case 8:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=gt(s),A=At(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(x=I.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new G(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Cn=Object.freeze({__proto__:null,execute:function(t){return Mn.apply(this,arguments)},generateTxns:function(t){return Fn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:ct(p,Number(m))/ct(u,Number(l)),priceImpact:A}}function lr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:ct(p,Number(l))/ct(u,Number(m)),priceImpact:A}}}}function dr(t){return fr.apply(this,arguments)}function fr(){return(fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=pr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,cr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(lr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:St.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,cr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var Ir={getQuote:function(t){return ur.apply(this,arguments)},getFixedInputSwapQuote:dr,getFixedInputDirectSwapQuote:pr,getFixedOutputDirectSwapQuote:lr,getFixedOutputSwapQuote:mr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ir.apply(this,arguments)},calculateFixedInputSwap:Ar};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function hr(){return(hr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[tr,t.encodeUint64(ut("negative",p,u)),t.encodeUint64(ut("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[er.ASSET_TRANSFER_TXN]=m,A[er.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[er.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[er.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,x=ut("negative",p,c),u!==d){e.next=17;break}A=x,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=x,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return h=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:vt.getAppCallTxnNoteWithClientName(M.V2),appArgs:[tr,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[er.ASSET_TRANSFER_TXN]=h,T[er.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[er.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[er.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,t.next=3,ft(n,[s]);case 3:return o=t.sent,i=h(o,1),u=i[0].txnID,t.prev=6,t.next=9,cn(n,r);case 9:c=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var yr,wr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ir.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=Ir.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,x=A.totalFeeAmount,h=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:h}}}return e},generateTxns:function(t){return hr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Tr.apply(this,arguments)}},_r=(p(yr={},M.V1_1,sr),p(yr,M.V2,wr),yr),br=3n,Er=1000n,Dr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Dr||{});function Sr(){return(Sr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=h(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Dr.FEE_TXN_INDEX?c:n===Dr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new G({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[xt("swap"),o===St.FixedInput?xt("fi"):xt("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:vt.getAppCallTxnNoteWithClientName(M.V1_1),foreignAssets:c.asset2ID==C?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),x=o===St.FixedOutput?ut("positive",i,p.assetInAmount):p.assetInAmount,h=l===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:x,suggestedParams:g}),v=o===St.FixedInput?ut("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===C?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,h,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*br/Er,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function Pr(t){return kr.apply(this,arguments)}function kr(){return(kr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+x,excessAmount:{assetID:i.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Er/(Er-br),l=p-c,d=ct(a.assetOut,Number(u))/ct(a.assetIn,Number(p)),f=ct(a.assetOut,Number(i))/ct(a.assetIn,Number(o)),m=pt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Rr(t){return Fr.apply(this,arguments)}function Fr(){return(Fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,ft(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-x,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new G({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Dr.ASSET_IN_TXN_INDEX].txn.assetIndex||C,amount:i[Dr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Dr.ASSET_OUT_TXN_INDEX].txn.assetIndex||C,amount:i[Dr.ASSET_OUT_TXN_INDEX].txn.amount},o!==St.FixedInput){t.next=12;break}return t.next=9,Pr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Rr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:At(i),fees:gt(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new G(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Mr,Cr={getQuote:function(t,e,n,r,a){return t===St.FixedInput?Nr({pool:e,reserves:n,assetIn:r,decimals:a}):Lr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Lr,generateTxns:function(t){return Or.apply(this,arguments)},signTxns:function(t){return Sr.apply(this,arguments)},execute:function(t){return Br.apply(this,arguments)},executeFixedOutputSwap:Rr};function jr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=h(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Vr(){return(Vr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===M.V2})),c.push(Ir.getFixedInputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===M.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===M.V2})),c.push(Ir.getFixedOutputSwapQuote({amount:r,assetIn:{id:K(s),decimals:s.decimals},assetOut:{id:K(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qr=(p(Mr={},M.V1_1,Cr),p(Mr,M.V2,Ir),p(Mr,"getQuote",(function(t){var e=t.type;if(e===St.FixedInput)return function(t){return Vr.apply(this,arguments)}(t);if(e===St.FixedOutput)return function(t){return Xr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Mr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1?Cr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):Ir.generateTxns(t)})),p(Mr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===M.V1_1){var e=t.quote.data.pool;return Cr.signTxns(r(r({},t),{},{pool:e}))}return Ir.signTxns(t)})),p(Mr,"execute",(function(t){return t.contractVersion===M.V1_1?Cr.execute(t):Ir.execute(t)})),Mr);function Ur(){return(Ur=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,qr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,ft(n,[i]);case 7:return u=t.sent,c=h(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:gt(s),confirmedRound:d,txnID:l,groupID:At(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new G(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function qr(t){return Gr.apply(this,arguments)}function Gr(){return(Gr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=h(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wr(){return Wr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:gt(e),groupID:At(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,ft(r,[u]);case 4:p=a.sent,l=h(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new G(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[xt("redeem")],note:vt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[xt("commit"),u],note:Z([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Yr(){return(Yr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Jr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[xt("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=j,exports.ALGO_ASSET_ID=C,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Kn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=yn,exports.CONTRACT_VERSION=M,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=V,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=_r,exports.Swap=Qr,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=St,exports.V1_1AddLiquidityTxnIndices=wn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Zn,exports.V2AddLiquidityTxnIndices=En,exports.V2AddLiquidityType=bn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=$n,exports.applySlippageToAmount=ut,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Ur.apply(this,arguments)},exports.sendAndWaitRawTransaction=ft,exports.sumUpTxnFees=gt,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=xe,exports.tinymanJSSDKConfig=vt; +"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}},C={V1_1:"v1_1",V2:"v2"},j=0,V={id:"".concat(j),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},X={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Q="- would result negative",U="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function Y(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function K(t){return Number(t)===j}function H(t){return Number(t.id)}function Z(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function $(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var tt=100000n,et=100000n,nt=100000n,rt=25000n+25000n,at=25000n+3500n;function st(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return tt+et*BigInt(o.length)+nt*BigInt(s.length+a.length)+at*r+rt*n}function ot(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function it(t,e){return ut.apply(this,arguments)}function ut(){return(ut=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,ot(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ct(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function pt(t,e){var n=Number(t);return lt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function lt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=h(ft(e),2),s=a[0],o=a[1],i=h(ft(Math.round(Number(dt(s,o+r)))),2),u=i[0],c=i[1];return Number(dt(u,c-r))}return Math.round(e)}function dt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ft(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function mt(t,e){return gt.apply(this,arguments)}function gt(){return(gt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,it(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new W(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function At(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function It(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function xt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ht(t){return(new TextEncoder).encode(t)}var vt,Tt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===C.V1_1?"v1":t;return ht("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),yt=(p(vt={},C.V1_1,{testnet:62368684,mainnet:552635992}),p(vt,C.V2,{testnet:148607e3,mainnet:1002541853}),vt);function wt(t,e){var n=yt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:wt(s,o),note:Tt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function bt(){return(bt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:wt(s,o),note:Tt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Et,Dt,St,Ot=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Nt=.003,Pt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),kt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Rt=ht("swap"),Ft=(p(Dt={},Ot.FixedInput,ht("fixed-input")),p(Dt,Ot.FixedOutput,ht("fixed-output")),Dt),Bt={ASSET_OPT_IN:ht("asset_opt_in")},Mt={testnet:159521633,mainnet:1071281873},Ct=(p(St={},Ot.FixedInput,7),p(St,Ot.FixedOutput,8),St);function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return pt(r.asset.decimals,Number(r.amount))/pt(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Mt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Bt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[H(u[0].quote.amount_in.asset),H(u[0].quote.amount_out.asset),H(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],x=[u[0].pool.address,u[1].pool.address],h=x[0],v=x[1],T=i===Ot.FixedInput?A:ct("positive",c,A),y=i===Ot.FixedOutput?I:ct("negative",c,I),w=K(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Rt,Ft[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[h,v],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Ct[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(M[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(kt[e]+Lt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Nt;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Nt)}function re(e){return(kt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return pt(n.decimals,Number(n.amount))/pt(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=pt(a.decimals,Number(n))/pt(r.decimals,Number(e));return lt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:H(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:H(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:C.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=x(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=wt(n.network,C.V1_1),a=h(Y(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Z({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=ht("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:x=Z({stateArray:I}),h=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=x[h],_=x[v],b=x[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,wt(s,C.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[C.V1_1].asset1],asset2ID:u[Oe[C.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=he(C.V2),u=i.generateLogicSigAccountForPool(e),c=wt(r,C.V2),p=u.address(),l=Y(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:C.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,wt(s,C.V2)),u=null,i&&(u={asset1ID:i[Oe[C.V2].asset1],asset2ID:i[Oe[C.V2].asset2],poolTokenID:i[Oe[C.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=pt(t.asset1,e.asset1)/pt(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},C.V1_1,r(r({},Me),Ge)),p(qe,C.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=J(o,i),l=h(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,x=K(A),v=wt(s,C.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),appArgs:[ht("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:x?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:X.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(x),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,x||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=Y(i,u),f=h(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,it(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new W(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r){var s,o;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return s=e.sent,a=r[0].txn,o=t.encodeAddress(a.from.publicKey),e.abrupt("return",null==s?void 0:s.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer&&t.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&t.encodeAddress(a.rcv)===o&&r.push({id:j,amount:a.amt}),r}),[]));case 5:case"end":return e.stop()}var a}),e)})))).apply(this,arguments)}function ln(){return(ln=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(o,i),f=h(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),x=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[ht("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=fn(x),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:dn(x),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function dn(t){return function(t,e){var n=he(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(C.V2,t)+fn(t)+O}function fn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function mn(){return(mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=Y(i,u),f=h(d,2),m=f[0],g=f[1],A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(){return(gn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new W(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(t){return ln.apply(this,arguments)},signTxns:function(t){return mn.apply(this,arguments)},execute:function(t){return gn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+dn(e)}};var xn,hn,vn,Tn,yn=(p(An={},C.V1_1,en),p(An,C.V2,In),p(An,"generateTxns",(function(t){return t.contractVersion===C.V1_1?en.generateTxns(t):In.generateTxns(t)})),p(An,"signTxns",(function(t){return t.contractVersion===C.V1_1?en.signTxns(t):In.signTxns(t)})),p(An,"execute",(function(t){return t.contractVersion===C.V1_1?en.execute(t):In.execute(t)})),p(An,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===C.V1_1?en.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),wn=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),_n=5*t.ALGORAND_MIN_TX_FEE,bn=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),En=(p(xn={},bn.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,bn.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,bn.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Dn=(p(hn={},bn.INITIAL,1),p(hn,bn.SINGLE,2),p(hn,bn.FLEXIBLE,2),hn),Sn=(p(vn={},bn.INITIAL,3),p(vn,bn.FLEXIBLE,3),p(vn,bn.SINGLE,2),vn);function On(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var x=A;r=Pn(x,s),e={id:o.id,amount:x+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=Pn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Nn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function Pn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function kn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Rn=(p(Tn={},C.V1_1,[ht("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[ht("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,ht("single")],FLEXIBLE_MODE:[Ln,ht("flexible")]}),Tn);function Fn(){return(Fn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ct("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:wt(s,C.V1_1),appArgs:Rn.v1_1,accounts:[l],note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),foreignAssets:u.id==j?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),x=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),h=t.assignGroupID([x,m,g,A,I]),e.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=h(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===wn.FEE_TXN?p:n===wn.ASSET1_IN_TXN?l:n===wn.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[wn.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,mt(n,[o]);case 8:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=At(s),A=It(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(x=I.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new W(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Cn=Object.freeze({__proto__:null,execute:function(t){return Mn.apply(this,arguments)},generateTxns:function(t){return Fn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:pt(p,Number(m))/pt(u,Number(l)),priceImpact:A}}function lr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:pt(p,Number(l))/pt(u,Number(m)),priceImpact:A}}}}function dr(t){return fr.apply(this,arguments)}function fr(){return(fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=pr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:Ot.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,cr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(lr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:Ot.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,cr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var Ir={getQuote:function(t){return ur.apply(this,arguments)},getFixedInputSwapQuote:dr,getFixedInputDirectSwapQuote:pr,getFixedOutputDirectSwapQuote:lr,getFixedOutputSwapQuote:mr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ir.apply(this,arguments)},calculateFixedInputSwap:Ar};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function hr(){return(hr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:Tt.getAppCallTxnNoteWithClientName(C.V2),appArgs:[tr,t.encodeUint64(ct("negative",p,u)),t.encodeUint64(ct("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[er.ASSET_TRANSFER_TXN]=m,A[er.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[er.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[er.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,x=ct("negative",p,c),u!==d){e.next=17;break}A=x,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=x,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return h=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:Tt.getAppCallTxnNoteWithClientName(C.V2),appArgs:[tr,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[er.ASSET_TRANSFER_TXN]=h,T[er.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[er.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[er.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,t.next=3,mt(n,[s]);case 3:return o=t.sent,i=h(o,1),u=i[0].txnID,t.prev=6,t.next=9,cn(n,r);case 9:c=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var yr,wr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ir.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=Ir.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,x=A.totalFeeAmount,h=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:h}}}return e},generateTxns:function(t){return hr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Tr.apply(this,arguments)}},_r=(p(yr={},C.V1_1,sr),p(yr,C.V2,wr),yr),br=3n,Er=1000n,Dr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Dr||{});function Sr(){return(Sr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=h(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Dr.FEE_TXN_INDEX?c:n===Dr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new W({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[ht("swap"),o===Ot.FixedInput?ht("fi"):ht("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),foreignAssets:c.asset2ID==j?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),x=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,h=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:x,suggestedParams:g}),v=o===Ot.FixedInput?ct("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,h,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*br/Er,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function Pr(t){return kr.apply(this,arguments)}function kr(){return(kr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,mt(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+x,excessAmount:{assetID:i.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Er/(Er-br),l=p-c,d=pt(a.assetOut,Number(u))/pt(a.assetIn,Number(p)),f=pt(a.assetOut,Number(i))/pt(a.assetIn,Number(o)),m=lt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Rr(t){return Fr.apply(this,arguments)}function Fr(){return(Fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,mt(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-x,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new W({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Dr.ASSET_IN_TXN_INDEX].txn.assetIndex||j,amount:i[Dr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Dr.ASSET_OUT_TXN_INDEX].txn.assetIndex||j,amount:i[Dr.ASSET_OUT_TXN_INDEX].txn.amount},o!==Ot.FixedInput){t.next=12;break}return t.next=9,Pr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Rr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:It(i),fees:At(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new W(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Mr,Cr={getQuote:function(t,e,n,r,a){return t===Ot.FixedInput?Nr({pool:e,reserves:n,assetIn:r,decimals:a}):Lr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Lr,generateTxns:function(t){return Or.apply(this,arguments)},signTxns:function(t){return Sr.apply(this,arguments)},execute:function(t){return Br.apply(this,arguments)},executeFixedOutputSwap:Rr};function jr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=h(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Vr(){return(Vr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===C.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===C.V2})),c.push(Ir.getFixedInputSwapQuote({amount:r,assetIn:{id:H(s),decimals:s.decimals},assetOut:{id:H(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===C.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===C.V2})),c.push(Ir.getFixedOutputSwapQuote({amount:r,assetIn:{id:H(s),decimals:s.decimals},assetOut:{id:H(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qr=(p(Mr={},C.V1_1,Cr),p(Mr,C.V2,Ir),p(Mr,"getQuote",(function(t){var e=t.type;if(e===Ot.FixedInput)return function(t){return Vr.apply(this,arguments)}(t);if(e===Ot.FixedOutput)return function(t){return Xr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Mr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===C.V1_1?Cr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):Ir.generateTxns(t)})),p(Mr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===C.V1_1){var e=t.quote.data.pool;return Cr.signTxns(r(r({},t),{},{pool:e}))}return Ir.signTxns(t)})),p(Mr,"execute",(function(t){return t.contractVersion===C.V1_1?Cr.execute(t):Ir.execute(t)})),Mr);function Ur(){return(Ur=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,qr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,mt(n,[i]);case 7:return u=t.sent,c=h(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:At(s),confirmedRound:d,txnID:l,groupID:It(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new W(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function qr(t){return Gr.apply(this,arguments)}function Gr(){return(Gr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=h(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wr(){return Wr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:At(e),groupID:It(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,mt(r,[u]);case 4:p=a.sent,l=h(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new W(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[ht("redeem")],note:Tt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ht("commit"),u],note:$([ht("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Yr(){return(Yr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Jr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[ht("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=V,exports.ALGO_ASSET_ID=j,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Kn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=yn,exports.CONTRACT_VERSION=C,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=X,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=_r,exports.Swap=Qr,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=Ot,exports.V1_1AddLiquidityTxnIndices=wn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Zn,exports.V2AddLiquidityTxnIndices=En,exports.V2AddLiquidityType=bn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=$n,exports.applySlippageToAmount=ct,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Ur.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=xe,exports.tinymanJSSDKConfig=Tt; diff --git a/dist/swap/v2/router/constants.d.ts b/dist/swap/v2/router/constants.d.ts index 9dc40598..1202a2df 100644 --- a/dist/swap/v2/router/constants.d.ts +++ b/dist/swap/v2/router/constants.d.ts @@ -5,8 +5,3 @@ export declare const SWAP_ROUTER_APP_ID: Record; * Inner txn counts according to the swap type */ export declare const SWAP_ROUTER_INNER_TXN_COUNT: Record; -declare const TINYMAN_ANALYTICS_API_BASE_URLS: Record; -export { TINYMAN_ANALYTICS_API_BASE_URLS }; diff --git a/dist/util/constant.d.ts b/dist/util/constant.d.ts index 8bce945a..7e1d0084 100644 --- a/dist/util/constant.d.ts +++ b/dist/util/constant.d.ts @@ -1,3 +1,4 @@ +import { SupportedNetwork } from "./commonTypes"; export declare const MAX_SLIPPAGE_FRACTION_DIGITS = 6; export declare const DEFAULT_FEE_TXN_NOTE: Uint8Array; export declare const BASE_MINIMUM_BALANCE = 100000; @@ -9,3 +10,7 @@ export declare const MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA = 50000; export declare const MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE = 28500; export declare const MINIMUM_ADD_LIQUIDITY_AMOUNT = 1000; export declare const DEFAULT_WAIT_FOR_CONFIRMATION_ROUNDS = 1000; +export declare const TINYMAN_ANALYTICS_API_BASE_URLS: Record; diff --git a/src/swap/v2/router/constants.ts b/src/swap/v2/router/constants.ts index 1152f60c..b74efbba 100644 --- a/src/swap/v2/router/constants.ts +++ b/src/swap/v2/router/constants.ts @@ -14,19 +14,3 @@ export const SWAP_ROUTER_INNER_TXN_COUNT: Record = { [SwapType.FixedInput]: 7, [SwapType.FixedOutput]: 8 } as const; - -const TINYMAN_ANALYTICS_API_BASE_URLS: Record< - SupportedNetwork, - {base: string; v1: string} -> = { - mainnet: { - base: "https://mainnet.analytics.tinyman.org/api", - v1: "https://mainnet.analytics.tinyman.org/api/v1" - }, - testnet: { - base: "https://testnet.analytics.tinyman.org/api", - v1: "https://testnet.analytics.tinyman.org/api/v1" - } -}; - -export {TINYMAN_ANALYTICS_API_BASE_URLS}; diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index 71fab0d8..2cb6910d 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -20,12 +20,13 @@ import { V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED, V2_SWAP_ROUTER_APP_ARGS_ENCODED } from "../constants"; -import {SWAP_ROUTER_INNER_TXN_COUNT, TINYMAN_ANALYTICS_API_BASE_URLS} from "./constants"; +import {SWAP_ROUTER_INNER_TXN_COUNT} from "./constants"; import { getAssetInFromSwapRoute, getAssetOutFromSwapRoute, getSwapRouterAppID } from "./util"; +import {TINYMAN_ANALYTICS_API_BASE_URLS} from "../../../util/constant"; /** * Generates txns that would opt in the Swap Router Application to the assets used in the swap router diff --git a/src/util/constant.ts b/src/util/constant.ts index ced87930..ad88cc23 100644 --- a/src/util/constant.ts +++ b/src/util/constant.ts @@ -1,3 +1,5 @@ +import {SupportedNetwork} from "./commonTypes"; + export const MAX_SLIPPAGE_FRACTION_DIGITS = 6; // The fee paying transaction at group index 0 should have a note value set to distinguish it from other Pay transactions in the group which might have the exact same value. @@ -14,3 +16,17 @@ export const MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE = 28_500; export const MINIMUM_ADD_LIQUIDITY_AMOUNT = 1000; export const DEFAULT_WAIT_FOR_CONFIRMATION_ROUNDS = 1000; + +export const TINYMAN_ANALYTICS_API_BASE_URLS: Record< + SupportedNetwork, + {base: string; v1: string} +> = { + mainnet: { + base: "https://mainnet.analytics.tinyman.org/api", + v1: "https://mainnet.analytics.tinyman.org/api/v1" + }, + testnet: { + base: "https://testnet.analytics.tinyman.org/api", + v1: "https://testnet.analytics.tinyman.org/api/v1" + } +}; From c6271f10ea5429214aabf9b0030232d187ec1bc4 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Wed, 12 Apr 2023 13:49:08 +0300 Subject: [PATCH 35/39] chore(swap-router): update swap router testnet app id --- dist/index.js | 2 +- src/swap/v2/router/constants.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 762a49fc..8dd8fffc 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}},C={V1_1:"v1_1",V2:"v2"},j=0,V={id:"".concat(j),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},X={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Q="- would result negative",U="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function Y(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function K(t){return Number(t)===j}function H(t){return Number(t.id)}function Z(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function $(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var tt=100000n,et=100000n,nt=100000n,rt=25000n+25000n,at=25000n+3500n;function st(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return tt+et*BigInt(o.length)+nt*BigInt(s.length+a.length)+at*r+rt*n}function ot(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function it(t,e){return ut.apply(this,arguments)}function ut(){return(ut=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,ot(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ct(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function pt(t,e){var n=Number(t);return lt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function lt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=h(ft(e),2),s=a[0],o=a[1],i=h(ft(Math.round(Number(dt(s,o+r)))),2),u=i[0],c=i[1];return Number(dt(u,c-r))}return Math.round(e)}function dt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ft(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function mt(t,e){return gt.apply(this,arguments)}function gt(){return(gt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,it(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new W(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function At(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function It(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function xt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ht(t){return(new TextEncoder).encode(t)}var vt,Tt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===C.V1_1?"v1":t;return ht("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),yt=(p(vt={},C.V1_1,{testnet:62368684,mainnet:552635992}),p(vt,C.V2,{testnet:148607e3,mainnet:1002541853}),vt);function wt(t,e){var n=yt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:wt(s,o),note:Tt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function bt(){return(bt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:wt(s,o),note:Tt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Et,Dt,St,Ot=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Nt=.003,Pt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),kt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Rt=ht("swap"),Ft=(p(Dt={},Ot.FixedInput,ht("fixed-input")),p(Dt,Ot.FixedOutput,ht("fixed-output")),Dt),Bt={ASSET_OPT_IN:ht("asset_opt_in")},Mt={testnet:159521633,mainnet:1071281873},Ct=(p(St={},Ot.FixedInput,7),p(St,Ot.FixedOutput,8),St);function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return pt(r.asset.decimals,Number(r.amount))/pt(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Mt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Bt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[H(u[0].quote.amount_in.asset),H(u[0].quote.amount_out.asset),H(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],x=[u[0].pool.address,u[1].pool.address],h=x[0],v=x[1],T=i===Ot.FixedInput?A:ct("positive",c,A),y=i===Ot.FixedOutput?I:ct("negative",c,I),w=K(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Rt,Ft[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[h,v],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Ct[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(M[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(kt[e]+Lt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Nt;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Nt)}function re(e){return(kt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return pt(n.decimals,Number(n.amount))/pt(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=pt(a.decimals,Number(n))/pt(r.decimals,Number(e));return lt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:H(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:H(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:C.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=x(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=wt(n.network,C.V1_1),a=h(Y(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Z({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=ht("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:x=Z({stateArray:I}),h=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=x[h],_=x[v],b=x[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,wt(s,C.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[C.V1_1].asset1],asset2ID:u[Oe[C.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=he(C.V2),u=i.generateLogicSigAccountForPool(e),c=wt(r,C.V2),p=u.address(),l=Y(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:C.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,wt(s,C.V2)),u=null,i&&(u={asset1ID:i[Oe[C.V2].asset1],asset2ID:i[Oe[C.V2].asset2],poolTokenID:i[Oe[C.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=pt(t.asset1,e.asset1)/pt(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},C.V1_1,r(r({},Me),Ge)),p(qe,C.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=J(o,i),l=h(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,x=K(A),v=wt(s,C.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),appArgs:[ht("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:x?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:X.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(x),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,x||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=Y(i,u),f=h(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,it(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new W(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r){var s,o;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return s=e.sent,a=r[0].txn,o=t.encodeAddress(a.from.publicKey),e.abrupt("return",null==s?void 0:s.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer&&t.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&t.encodeAddress(a.rcv)===o&&r.push({id:j,amount:a.amt}),r}),[]));case 5:case"end":return e.stop()}var a}),e)})))).apply(this,arguments)}function ln(){return(ln=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(o,i),f=h(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),x=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[ht("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=fn(x),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:dn(x),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function dn(t){return function(t,e){var n=he(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(C.V2,t)+fn(t)+O}function fn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function mn(){return(mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=Y(i,u),f=h(d,2),m=f[0],g=f[1],A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(){return(gn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new W(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(t){return ln.apply(this,arguments)},signTxns:function(t){return mn.apply(this,arguments)},execute:function(t){return gn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+dn(e)}};var xn,hn,vn,Tn,yn=(p(An={},C.V1_1,en),p(An,C.V2,In),p(An,"generateTxns",(function(t){return t.contractVersion===C.V1_1?en.generateTxns(t):In.generateTxns(t)})),p(An,"signTxns",(function(t){return t.contractVersion===C.V1_1?en.signTxns(t):In.signTxns(t)})),p(An,"execute",(function(t){return t.contractVersion===C.V1_1?en.execute(t):In.execute(t)})),p(An,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===C.V1_1?en.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),wn=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),_n=5*t.ALGORAND_MIN_TX_FEE,bn=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),En=(p(xn={},bn.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,bn.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,bn.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Dn=(p(hn={},bn.INITIAL,1),p(hn,bn.SINGLE,2),p(hn,bn.FLEXIBLE,2),hn),Sn=(p(vn={},bn.INITIAL,3),p(vn,bn.FLEXIBLE,3),p(vn,bn.SINGLE,2),vn);function On(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var x=A;r=Pn(x,s),e={id:o.id,amount:x+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=Pn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Nn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function Pn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function kn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Rn=(p(Tn={},C.V1_1,[ht("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[ht("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,ht("single")],FLEXIBLE_MODE:[Ln,ht("flexible")]}),Tn);function Fn(){return(Fn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ct("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:wt(s,C.V1_1),appArgs:Rn.v1_1,accounts:[l],note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),foreignAssets:u.id==j?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),x=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),h=t.assignGroupID([x,m,g,A,I]),e.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=h(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===wn.FEE_TXN?p:n===wn.ASSET1_IN_TXN?l:n===wn.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[wn.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,mt(n,[o]);case 8:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=At(s),A=It(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(x=I.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new W(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Cn=Object.freeze({__proto__:null,execute:function(t){return Mn.apply(this,arguments)},generateTxns:function(t){return Fn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:pt(p,Number(m))/pt(u,Number(l)),priceImpact:A}}function lr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:pt(p,Number(l))/pt(u,Number(m)),priceImpact:A}}}}function dr(t){return fr.apply(this,arguments)}function fr(){return(fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=pr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:Ot.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,cr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(lr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:Ot.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,cr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var Ir={getQuote:function(t){return ur.apply(this,arguments)},getFixedInputSwapQuote:dr,getFixedInputDirectSwapQuote:pr,getFixedOutputDirectSwapQuote:lr,getFixedOutputSwapQuote:mr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ir.apply(this,arguments)},calculateFixedInputSwap:Ar};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function hr(){return(hr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:Tt.getAppCallTxnNoteWithClientName(C.V2),appArgs:[tr,t.encodeUint64(ct("negative",p,u)),t.encodeUint64(ct("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[er.ASSET_TRANSFER_TXN]=m,A[er.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[er.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[er.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,x=ct("negative",p,c),u!==d){e.next=17;break}A=x,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=x,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return h=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:Tt.getAppCallTxnNoteWithClientName(C.V2),appArgs:[tr,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[er.ASSET_TRANSFER_TXN]=h,T[er.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[er.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[er.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,t.next=3,mt(n,[s]);case 3:return o=t.sent,i=h(o,1),u=i[0].txnID,t.prev=6,t.next=9,cn(n,r);case 9:c=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var yr,wr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ir.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=Ir.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,x=A.totalFeeAmount,h=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:h}}}return e},generateTxns:function(t){return hr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Tr.apply(this,arguments)}},_r=(p(yr={},C.V1_1,sr),p(yr,C.V2,wr),yr),br=3n,Er=1000n,Dr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Dr||{});function Sr(){return(Sr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=h(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Dr.FEE_TXN_INDEX?c:n===Dr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new W({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[ht("swap"),o===Ot.FixedInput?ht("fi"):ht("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),foreignAssets:c.asset2ID==j?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),x=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,h=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:x,suggestedParams:g}),v=o===Ot.FixedInput?ct("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,h,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*br/Er,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function Pr(t){return kr.apply(this,arguments)}function kr(){return(kr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,mt(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+x,excessAmount:{assetID:i.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Er/(Er-br),l=p-c,d=pt(a.assetOut,Number(u))/pt(a.assetIn,Number(p)),f=pt(a.assetOut,Number(i))/pt(a.assetIn,Number(o)),m=lt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Rr(t){return Fr.apply(this,arguments)}function Fr(){return(Fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,mt(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-x,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new W({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Dr.ASSET_IN_TXN_INDEX].txn.assetIndex||j,amount:i[Dr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Dr.ASSET_OUT_TXN_INDEX].txn.assetIndex||j,amount:i[Dr.ASSET_OUT_TXN_INDEX].txn.amount},o!==Ot.FixedInput){t.next=12;break}return t.next=9,Pr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Rr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:It(i),fees:At(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new W(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Mr,Cr={getQuote:function(t,e,n,r,a){return t===Ot.FixedInput?Nr({pool:e,reserves:n,assetIn:r,decimals:a}):Lr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Lr,generateTxns:function(t){return Or.apply(this,arguments)},signTxns:function(t){return Sr.apply(this,arguments)},execute:function(t){return Br.apply(this,arguments)},executeFixedOutputSwap:Rr};function jr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=h(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Vr(){return(Vr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===C.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===C.V2})),c.push(Ir.getFixedInputSwapQuote({amount:r,assetIn:{id:H(s),decimals:s.decimals},assetOut:{id:H(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===C.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===C.V2})),c.push(Ir.getFixedOutputSwapQuote({amount:r,assetIn:{id:H(s),decimals:s.decimals},assetOut:{id:H(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qr=(p(Mr={},C.V1_1,Cr),p(Mr,C.V2,Ir),p(Mr,"getQuote",(function(t){var e=t.type;if(e===Ot.FixedInput)return function(t){return Vr.apply(this,arguments)}(t);if(e===Ot.FixedOutput)return function(t){return Xr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Mr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===C.V1_1?Cr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):Ir.generateTxns(t)})),p(Mr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===C.V1_1){var e=t.quote.data.pool;return Cr.signTxns(r(r({},t),{},{pool:e}))}return Ir.signTxns(t)})),p(Mr,"execute",(function(t){return t.contractVersion===C.V1_1?Cr.execute(t):Ir.execute(t)})),Mr);function Ur(){return(Ur=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,qr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,mt(n,[i]);case 7:return u=t.sent,c=h(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:At(s),confirmedRound:d,txnID:l,groupID:It(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new W(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function qr(t){return Gr.apply(this,arguments)}function Gr(){return(Gr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=h(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wr(){return Wr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:At(e),groupID:It(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,mt(r,[u]);case 4:p=a.sent,l=h(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new W(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[ht("redeem")],note:Tt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ht("commit"),u],note:$([ht("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Yr(){return(Yr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Jr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[ht("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=V,exports.ALGO_ASSET_ID=j,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Kn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=yn,exports.CONTRACT_VERSION=C,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=X,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=_r,exports.Swap=Qr,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=Ot,exports.V1_1AddLiquidityTxnIndices=wn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Zn,exports.V2AddLiquidityTxnIndices=En,exports.V2AddLiquidityType=bn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=$n,exports.applySlippageToAmount=ct,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Ur.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=xe,exports.tinymanJSSDKConfig=Tt; +"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}},C={V1_1:"v1_1",V2:"v2"},j=0,V={id:"".concat(j),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},X={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Q="- would result negative",U="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function Y(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function K(t){return Number(t)===j}function H(t){return Number(t.id)}function Z(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function $(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var tt=100000n,et=100000n,nt=100000n,rt=25000n+25000n,at=25000n+3500n;function st(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return tt+et*BigInt(o.length)+nt*BigInt(s.length+a.length)+at*r+rt*n}function ot(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function it(t,e){return ut.apply(this,arguments)}function ut(){return(ut=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,ot(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ct(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function pt(t,e){var n=Number(t);return lt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function lt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=h(ft(e),2),s=a[0],o=a[1],i=h(ft(Math.round(Number(dt(s,o+r)))),2),u=i[0],c=i[1];return Number(dt(u,c-r))}return Math.round(e)}function dt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ft(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function mt(t,e){return gt.apply(this,arguments)}function gt(){return(gt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,it(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new W(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function At(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function It(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function xt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ht(t){return(new TextEncoder).encode(t)}var vt,Tt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===C.V1_1?"v1":t;return ht("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),yt=(p(vt={},C.V1_1,{testnet:62368684,mainnet:552635992}),p(vt,C.V2,{testnet:148607e3,mainnet:1002541853}),vt);function wt(t,e){var n=yt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:wt(s,o),note:Tt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function bt(){return(bt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:wt(s,o),note:Tt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Et,Dt,St,Ot=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Nt=.003,Pt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),kt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Rt=ht("swap"),Ft=(p(Dt={},Ot.FixedInput,ht("fixed-input")),p(Dt,Ot.FixedOutput,ht("fixed-output")),Dt),Bt={ASSET_OPT_IN:ht("asset_opt_in")},Mt={testnet:184778019,mainnet:1071281873},Ct=(p(St={},Ot.FixedInput,7),p(St,Ot.FixedOutput,8),St);function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return pt(r.asset.decimals,Number(r.amount))/pt(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Mt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Bt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[H(u[0].quote.amount_in.asset),H(u[0].quote.amount_out.asset),H(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],x=[u[0].pool.address,u[1].pool.address],h=x[0],v=x[1],T=i===Ot.FixedInput?A:ct("positive",c,A),y=i===Ot.FixedOutput?I:ct("negative",c,I),w=K(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Rt,Ft[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[h,v],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Ct[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(M[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(kt[e]+Lt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Nt;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Nt)}function re(e){return(kt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return pt(n.decimals,Number(n.amount))/pt(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=pt(a.decimals,Number(n))/pt(r.decimals,Number(e));return lt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:H(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:H(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:C.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=x(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=wt(n.network,C.V1_1),a=h(Y(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Z({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=ht("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:x=Z({stateArray:I}),h=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=x[h],_=x[v],b=x[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,wt(s,C.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[C.V1_1].asset1],asset2ID:u[Oe[C.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=he(C.V2),u=i.generateLogicSigAccountForPool(e),c=wt(r,C.V2),p=u.address(),l=Y(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:C.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,wt(s,C.V2)),u=null,i&&(u={asset1ID:i[Oe[C.V2].asset1],asset2ID:i[Oe[C.V2].asset2],poolTokenID:i[Oe[C.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=pt(t.asset1,e.asset1)/pt(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},C.V1_1,r(r({},Me),Ge)),p(qe,C.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=J(o,i),l=h(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,x=K(A),v=wt(s,C.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),appArgs:[ht("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:x?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:X.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(x),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,x||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=Y(i,u),f=h(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,it(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new W(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r){var s,o;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return s=e.sent,a=r[0].txn,o=t.encodeAddress(a.from.publicKey),e.abrupt("return",null==s?void 0:s.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer&&t.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&t.encodeAddress(a.rcv)===o&&r.push({id:j,amount:a.amt}),r}),[]));case 5:case"end":return e.stop()}var a}),e)})))).apply(this,arguments)}function ln(){return(ln=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(o,i),f=h(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),x=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[ht("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=fn(x),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:dn(x),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function dn(t){return function(t,e){var n=he(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(C.V2,t)+fn(t)+O}function fn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function mn(){return(mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=Y(i,u),f=h(d,2),m=f[0],g=f[1],A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(){return(gn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new W(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(t){return ln.apply(this,arguments)},signTxns:function(t){return mn.apply(this,arguments)},execute:function(t){return gn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+dn(e)}};var xn,hn,vn,Tn,yn=(p(An={},C.V1_1,en),p(An,C.V2,In),p(An,"generateTxns",(function(t){return t.contractVersion===C.V1_1?en.generateTxns(t):In.generateTxns(t)})),p(An,"signTxns",(function(t){return t.contractVersion===C.V1_1?en.signTxns(t):In.signTxns(t)})),p(An,"execute",(function(t){return t.contractVersion===C.V1_1?en.execute(t):In.execute(t)})),p(An,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===C.V1_1?en.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),wn=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),_n=5*t.ALGORAND_MIN_TX_FEE,bn=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),En=(p(xn={},bn.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,bn.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,bn.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Dn=(p(hn={},bn.INITIAL,1),p(hn,bn.SINGLE,2),p(hn,bn.FLEXIBLE,2),hn),Sn=(p(vn={},bn.INITIAL,3),p(vn,bn.FLEXIBLE,3),p(vn,bn.SINGLE,2),vn);function On(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var x=A;r=Pn(x,s),e={id:o.id,amount:x+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=Pn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Nn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function Pn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function kn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Rn=(p(Tn={},C.V1_1,[ht("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[ht("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,ht("single")],FLEXIBLE_MODE:[Ln,ht("flexible")]}),Tn);function Fn(){return(Fn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ct("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:wt(s,C.V1_1),appArgs:Rn.v1_1,accounts:[l],note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),foreignAssets:u.id==j?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),x=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),h=t.assignGroupID([x,m,g,A,I]),e.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=h(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===wn.FEE_TXN?p:n===wn.ASSET1_IN_TXN?l:n===wn.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[wn.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,mt(n,[o]);case 8:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=At(s),A=It(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(x=I.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new W(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Cn=Object.freeze({__proto__:null,execute:function(t){return Mn.apply(this,arguments)},generateTxns:function(t){return Fn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:pt(p,Number(m))/pt(u,Number(l)),priceImpact:A}}function lr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:pt(p,Number(l))/pt(u,Number(m)),priceImpact:A}}}}function dr(t){return fr.apply(this,arguments)}function fr(){return(fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=pr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:Ot.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,cr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(lr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:Ot.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,cr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var Ir={getQuote:function(t){return ur.apply(this,arguments)},getFixedInputSwapQuote:dr,getFixedInputDirectSwapQuote:pr,getFixedOutputDirectSwapQuote:lr,getFixedOutputSwapQuote:mr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ir.apply(this,arguments)},calculateFixedInputSwap:Ar};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function hr(){return(hr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:Tt.getAppCallTxnNoteWithClientName(C.V2),appArgs:[tr,t.encodeUint64(ct("negative",p,u)),t.encodeUint64(ct("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[er.ASSET_TRANSFER_TXN]=m,A[er.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[er.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[er.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,x=ct("negative",p,c),u!==d){e.next=17;break}A=x,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=x,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return h=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:Tt.getAppCallTxnNoteWithClientName(C.V2),appArgs:[tr,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[er.ASSET_TRANSFER_TXN]=h,T[er.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[er.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[er.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,t.next=3,mt(n,[s]);case 3:return o=t.sent,i=h(o,1),u=i[0].txnID,t.prev=6,t.next=9,cn(n,r);case 9:c=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var yr,wr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ir.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=Ir.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,x=A.totalFeeAmount,h=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:h}}}return e},generateTxns:function(t){return hr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Tr.apply(this,arguments)}},_r=(p(yr={},C.V1_1,sr),p(yr,C.V2,wr),yr),br=3n,Er=1000n,Dr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Dr||{});function Sr(){return(Sr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=h(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Dr.FEE_TXN_INDEX?c:n===Dr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new W({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[ht("swap"),o===Ot.FixedInput?ht("fi"):ht("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),foreignAssets:c.asset2ID==j?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),x=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,h=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:x,suggestedParams:g}),v=o===Ot.FixedInput?ct("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,h,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*br/Er,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function Pr(t){return kr.apply(this,arguments)}function kr(){return(kr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,mt(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+x,excessAmount:{assetID:i.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Er/(Er-br),l=p-c,d=pt(a.assetOut,Number(u))/pt(a.assetIn,Number(p)),f=pt(a.assetOut,Number(i))/pt(a.assetIn,Number(o)),m=lt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Rr(t){return Fr.apply(this,arguments)}function Fr(){return(Fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,mt(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-x,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new W({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Dr.ASSET_IN_TXN_INDEX].txn.assetIndex||j,amount:i[Dr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Dr.ASSET_OUT_TXN_INDEX].txn.assetIndex||j,amount:i[Dr.ASSET_OUT_TXN_INDEX].txn.amount},o!==Ot.FixedInput){t.next=12;break}return t.next=9,Pr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Rr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:It(i),fees:At(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new W(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Mr,Cr={getQuote:function(t,e,n,r,a){return t===Ot.FixedInput?Nr({pool:e,reserves:n,assetIn:r,decimals:a}):Lr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Lr,generateTxns:function(t){return Or.apply(this,arguments)},signTxns:function(t){return Sr.apply(this,arguments)},execute:function(t){return Br.apply(this,arguments)},executeFixedOutputSwap:Rr};function jr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=h(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Vr(){return(Vr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===C.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===C.V2})),c.push(Ir.getFixedInputSwapQuote({amount:r,assetIn:{id:H(s),decimals:s.decimals},assetOut:{id:H(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===C.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===C.V2})),c.push(Ir.getFixedOutputSwapQuote({amount:r,assetIn:{id:H(s),decimals:s.decimals},assetOut:{id:H(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qr=(p(Mr={},C.V1_1,Cr),p(Mr,C.V2,Ir),p(Mr,"getQuote",(function(t){var e=t.type;if(e===Ot.FixedInput)return function(t){return Vr.apply(this,arguments)}(t);if(e===Ot.FixedOutput)return function(t){return Xr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Mr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===C.V1_1?Cr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):Ir.generateTxns(t)})),p(Mr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===C.V1_1){var e=t.quote.data.pool;return Cr.signTxns(r(r({},t),{},{pool:e}))}return Ir.signTxns(t)})),p(Mr,"execute",(function(t){return t.contractVersion===C.V1_1?Cr.execute(t):Ir.execute(t)})),Mr);function Ur(){return(Ur=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,qr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,mt(n,[i]);case 7:return u=t.sent,c=h(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:At(s),confirmedRound:d,txnID:l,groupID:It(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new W(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function qr(t){return Gr.apply(this,arguments)}function Gr(){return(Gr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=h(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wr(){return Wr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:At(e),groupID:It(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,mt(r,[u]);case 4:p=a.sent,l=h(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new W(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[ht("redeem")],note:Tt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ht("commit"),u],note:$([ht("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Yr(){return(Yr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Jr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[ht("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=V,exports.ALGO_ASSET_ID=j,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Kn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=yn,exports.CONTRACT_VERSION=C,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=X,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=_r,exports.Swap=Qr,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=Ot,exports.V1_1AddLiquidityTxnIndices=wn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Zn,exports.V2AddLiquidityTxnIndices=En,exports.V2AddLiquidityType=bn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=$n,exports.applySlippageToAmount=ct,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Ur.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=xe,exports.tinymanJSSDKConfig=Tt; diff --git a/src/swap/v2/router/constants.ts b/src/swap/v2/router/constants.ts index b74efbba..12317486 100644 --- a/src/swap/v2/router/constants.ts +++ b/src/swap/v2/router/constants.ts @@ -2,7 +2,7 @@ import {SupportedNetwork} from "../../../util/commonTypes"; import {SwapType} from "../../constants"; export const SWAP_ROUTER_APP_ID: Record = { - testnet: 159521633, + testnet: 184778019, // This is temporary APP ID for testing only, remove it when the actual mainnet APP ID is deployed mainnet: 1071281873 }; From c02892811186d5682d06751a5869bc29c102f881 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Wed, 12 Apr 2023 17:22:13 +0300 Subject: [PATCH 36/39] fix(swap-router): add note to swap router app call --- src/swap/v2/router/swap-router.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index 2cb6910d..b9a15986 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -27,6 +27,7 @@ import { getSwapRouterAppID } from "./util"; import {TINYMAN_ANALYTICS_API_BASE_URLS} from "../../../util/constant"; +import {tinymanJSSDKConfig} from "../../../config"; /** * Generates txns that would opt in the Swap Router Application to the assets used in the swap router @@ -130,7 +131,8 @@ export async function generateSwapRouterTxns({ foreignApps: [getValidatorAppID(network, CONTRACT_VERSION.V2)], foreignAssets: [assetInID, intermediaryAssetID, assetOutID], accounts: [pool1Address, pool2Address], - suggestedParams + suggestedParams, + note: tinymanJSSDKConfig.getAppCallTxnNoteWithClientName(CONTRACT_VERSION.V2) }); routerAppCallTxn.fee = From 34b33338ccca2e54bbed84bc33fc8f2cd24b87b5 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Wed, 12 Apr 2023 17:23:03 +0300 Subject: [PATCH 37/39] chore: get build --- dist/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/index.js b/dist/index.js index 762a49fc..b83da083 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}},C={V1_1:"v1_1",V2:"v2"},j=0,V={id:"".concat(j),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},X={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Q="- would result negative",U="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function Y(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function K(t){return Number(t)===j}function H(t){return Number(t.id)}function Z(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function $(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var tt=100000n,et=100000n,nt=100000n,rt=25000n+25000n,at=25000n+3500n;function st(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return tt+et*BigInt(o.length)+nt*BigInt(s.length+a.length)+at*r+rt*n}function ot(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function it(t,e){return ut.apply(this,arguments)}function ut(){return(ut=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,ot(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ct(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function pt(t,e){var n=Number(t);return lt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function lt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=h(ft(e),2),s=a[0],o=a[1],i=h(ft(Math.round(Number(dt(s,o+r)))),2),u=i[0],c=i[1];return Number(dt(u,c-r))}return Math.round(e)}function dt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ft(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function mt(t,e){return gt.apply(this,arguments)}function gt(){return(gt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,it(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new W(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function At(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function It(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function xt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ht(t){return(new TextEncoder).encode(t)}var vt,Tt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===C.V1_1?"v1":t;return ht("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),yt=(p(vt={},C.V1_1,{testnet:62368684,mainnet:552635992}),p(vt,C.V2,{testnet:148607e3,mainnet:1002541853}),vt);function wt(t,e){var n=yt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:wt(s,o),note:Tt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function bt(){return(bt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:wt(s,o),note:Tt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Et,Dt,St,Ot=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Nt=.003,Pt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),kt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Rt=ht("swap"),Ft=(p(Dt={},Ot.FixedInput,ht("fixed-input")),p(Dt,Ot.FixedOutput,ht("fixed-output")),Dt),Bt={ASSET_OPT_IN:ht("asset_opt_in")},Mt={testnet:159521633,mainnet:1071281873},Ct=(p(St={},Ot.FixedInput,7),p(St,Ot.FixedOutput,8),St);function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return pt(r.asset.decimals,Number(r.amount))/pt(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Mt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Bt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[H(u[0].quote.amount_in.asset),H(u[0].quote.amount_out.asset),H(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],x=[u[0].pool.address,u[1].pool.address],h=x[0],v=x[1],T=i===Ot.FixedInput?A:ct("positive",c,A),y=i===Ot.FixedOutput?I:ct("negative",c,I),w=K(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Rt,Ft[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[h,v],suggestedParams:p})).fee=t.ALGORAND_MIN_TX_FEE*(Ct[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(M[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(kt[e]+Lt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Nt;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Nt)}function re(e){return(kt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return pt(n.decimals,Number(n.amount))/pt(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=pt(a.decimals,Number(n))/pt(r.decimals,Number(e));return lt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:H(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:H(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:C.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=x(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=wt(n.network,C.V1_1),a=h(Y(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Z({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=ht("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:x=Z({stateArray:I}),h=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=x[h],_=x[v],b=x[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,wt(s,C.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[C.V1_1].asset1],asset2ID:u[Oe[C.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=he(C.V2),u=i.generateLogicSigAccountForPool(e),c=wt(r,C.V2),p=u.address(),l=Y(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:C.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,wt(s,C.V2)),u=null,i&&(u={asset1ID:i[Oe[C.V2].asset1],asset2ID:i[Oe[C.V2].asset2],poolTokenID:i[Oe[C.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=pt(t.asset1,e.asset1)/pt(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},C.V1_1,r(r({},Me),Ge)),p(qe,C.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=J(o,i),l=h(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,x=K(A),v=wt(s,C.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),appArgs:[ht("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:x?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:X.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(x),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,x||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=Y(i,u),f=h(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,it(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new W(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r){var s,o;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return s=e.sent,a=r[0].txn,o=t.encodeAddress(a.from.publicKey),e.abrupt("return",null==s?void 0:s.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer&&t.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&t.encodeAddress(a.rcv)===o&&r.push({id:j,amount:a.amt}),r}),[]));case 5:case"end":return e.stop()}var a}),e)})))).apply(this,arguments)}function ln(){return(ln=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(o,i),f=h(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),x=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[ht("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=fn(x),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:dn(x),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function dn(t){return function(t,e){var n=he(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(C.V2,t)+fn(t)+O}function fn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function mn(){return(mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=Y(i,u),f=h(d,2),m=f[0],g=f[1],A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(){return(gn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new W(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(t){return ln.apply(this,arguments)},signTxns:function(t){return mn.apply(this,arguments)},execute:function(t){return gn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+dn(e)}};var xn,hn,vn,Tn,yn=(p(An={},C.V1_1,en),p(An,C.V2,In),p(An,"generateTxns",(function(t){return t.contractVersion===C.V1_1?en.generateTxns(t):In.generateTxns(t)})),p(An,"signTxns",(function(t){return t.contractVersion===C.V1_1?en.signTxns(t):In.signTxns(t)})),p(An,"execute",(function(t){return t.contractVersion===C.V1_1?en.execute(t):In.execute(t)})),p(An,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===C.V1_1?en.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),wn=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),_n=5*t.ALGORAND_MIN_TX_FEE,bn=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),En=(p(xn={},bn.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,bn.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,bn.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Dn=(p(hn={},bn.INITIAL,1),p(hn,bn.SINGLE,2),p(hn,bn.FLEXIBLE,2),hn),Sn=(p(vn={},bn.INITIAL,3),p(vn,bn.FLEXIBLE,3),p(vn,bn.SINGLE,2),vn);function On(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var x=A;r=Pn(x,s),e={id:o.id,amount:x+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=Pn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Nn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function Pn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function kn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Rn=(p(Tn={},C.V1_1,[ht("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[ht("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,ht("single")],FLEXIBLE_MODE:[Ln,ht("flexible")]}),Tn);function Fn(){return(Fn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ct("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:wt(s,C.V1_1),appArgs:Rn.v1_1,accounts:[l],note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),foreignAssets:u.id==j?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),x=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),h=t.assignGroupID([x,m,g,A,I]),e.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=h(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===wn.FEE_TXN?p:n===wn.ASSET1_IN_TXN?l:n===wn.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[wn.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,mt(n,[o]);case 8:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=At(s),A=It(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(x=I.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new W(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Cn=Object.freeze({__proto__:null,execute:function(t){return Mn.apply(this,arguments)},generateTxns:function(t){return Fn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:pt(p,Number(m))/pt(u,Number(l)),priceImpact:A}}function lr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:pt(p,Number(l))/pt(u,Number(m)),priceImpact:A}}}}function dr(t){return fr.apply(this,arguments)}function fr(){return(fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=pr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:Ot.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,cr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(lr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:Ot.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,cr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var Ir={getQuote:function(t){return ur.apply(this,arguments)},getFixedInputSwapQuote:dr,getFixedInputDirectSwapQuote:pr,getFixedOutputDirectSwapQuote:lr,getFixedOutputSwapQuote:mr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ir.apply(this,arguments)},calculateFixedInputSwap:Ar};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function hr(){return(hr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:Tt.getAppCallTxnNoteWithClientName(C.V2),appArgs:[tr,t.encodeUint64(ct("negative",p,u)),t.encodeUint64(ct("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[er.ASSET_TRANSFER_TXN]=m,A[er.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[er.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[er.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,x=ct("negative",p,c),u!==d){e.next=17;break}A=x,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=x,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return h=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:Tt.getAppCallTxnNoteWithClientName(C.V2),appArgs:[tr,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[er.ASSET_TRANSFER_TXN]=h,T[er.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[er.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[er.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,t.next=3,mt(n,[s]);case 3:return o=t.sent,i=h(o,1),u=i[0].txnID,t.prev=6,t.next=9,cn(n,r);case 9:c=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var yr,wr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ir.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=Ir.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,x=A.totalFeeAmount,h=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:h}}}return e},generateTxns:function(t){return hr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Tr.apply(this,arguments)}},_r=(p(yr={},C.V1_1,sr),p(yr,C.V2,wr),yr),br=3n,Er=1000n,Dr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Dr||{});function Sr(){return(Sr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=h(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Dr.FEE_TXN_INDEX?c:n===Dr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new W({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[ht("swap"),o===Ot.FixedInput?ht("fi"):ht("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),foreignAssets:c.asset2ID==j?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),x=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,h=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:x,suggestedParams:g}),v=o===Ot.FixedInput?ct("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,h,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*br/Er,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function Pr(t){return kr.apply(this,arguments)}function kr(){return(kr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,mt(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+x,excessAmount:{assetID:i.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Er/(Er-br),l=p-c,d=pt(a.assetOut,Number(u))/pt(a.assetIn,Number(p)),f=pt(a.assetOut,Number(i))/pt(a.assetIn,Number(o)),m=lt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Rr(t){return Fr.apply(this,arguments)}function Fr(){return(Fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,mt(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-x,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new W({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Dr.ASSET_IN_TXN_INDEX].txn.assetIndex||j,amount:i[Dr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Dr.ASSET_OUT_TXN_INDEX].txn.assetIndex||j,amount:i[Dr.ASSET_OUT_TXN_INDEX].txn.amount},o!==Ot.FixedInput){t.next=12;break}return t.next=9,Pr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Rr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:It(i),fees:At(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new W(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Mr,Cr={getQuote:function(t,e,n,r,a){return t===Ot.FixedInput?Nr({pool:e,reserves:n,assetIn:r,decimals:a}):Lr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Lr,generateTxns:function(t){return Or.apply(this,arguments)},signTxns:function(t){return Sr.apply(this,arguments)},execute:function(t){return Br.apply(this,arguments)},executeFixedOutputSwap:Rr};function jr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=h(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Vr(){return(Vr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===C.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===C.V2})),c.push(Ir.getFixedInputSwapQuote({amount:r,assetIn:{id:H(s),decimals:s.decimals},assetOut:{id:H(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===C.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===C.V2})),c.push(Ir.getFixedOutputSwapQuote({amount:r,assetIn:{id:H(s),decimals:s.decimals},assetOut:{id:H(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qr=(p(Mr={},C.V1_1,Cr),p(Mr,C.V2,Ir),p(Mr,"getQuote",(function(t){var e=t.type;if(e===Ot.FixedInput)return function(t){return Vr.apply(this,arguments)}(t);if(e===Ot.FixedOutput)return function(t){return Xr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Mr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===C.V1_1?Cr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):Ir.generateTxns(t)})),p(Mr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===C.V1_1){var e=t.quote.data.pool;return Cr.signTxns(r(r({},t),{},{pool:e}))}return Ir.signTxns(t)})),p(Mr,"execute",(function(t){return t.contractVersion===C.V1_1?Cr.execute(t):Ir.execute(t)})),Mr);function Ur(){return(Ur=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,qr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,mt(n,[i]);case 7:return u=t.sent,c=h(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:At(s),confirmedRound:d,txnID:l,groupID:It(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new W(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function qr(t){return Gr.apply(this,arguments)}function Gr(){return(Gr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=h(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wr(){return Wr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:At(e),groupID:It(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,mt(r,[u]);case 4:p=a.sent,l=h(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new W(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[ht("redeem")],note:Tt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ht("commit"),u],note:$([ht("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Yr(){return(Yr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Jr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[ht("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=V,exports.ALGO_ASSET_ID=j,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Kn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=yn,exports.CONTRACT_VERSION=C,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=X,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=_r,exports.Swap=Qr,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=Ot,exports.V1_1AddLiquidityTxnIndices=wn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Zn,exports.V2AddLiquidityTxnIndices=En,exports.V2AddLiquidityType=bn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=$n,exports.applySlippageToAmount=ct,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Ur.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=xe,exports.tinymanJSSDKConfig=Tt; +"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}},C={V1_1:"v1_1",V2:"v2"},j=0,V={id:"".concat(j),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},X={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Q="- would result negative",U="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function Y(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function K(t){return Number(t)===j}function H(t){return Number(t.id)}function Z(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function $(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var tt=100000n,et=100000n,nt=100000n,rt=25000n+25000n,at=25000n+3500n;function st(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return tt+et*BigInt(o.length)+nt*BigInt(s.length+a.length)+at*r+rt*n}function ot(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function it(t,e){return ut.apply(this,arguments)}function ut(){return(ut=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,ot(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ct(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function pt(t,e){var n=Number(t);return lt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function lt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=h(ft(e),2),s=a[0],o=a[1],i=h(ft(Math.round(Number(dt(s,o+r)))),2),u=i[0],c=i[1];return Number(dt(u,c-r))}return Math.round(e)}function dt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ft(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function mt(t,e){return gt.apply(this,arguments)}function gt(){return(gt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,it(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new W(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function At(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function It(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function xt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ht(t){return(new TextEncoder).encode(t)}var vt,Tt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===C.V1_1?"v1":t;return ht("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),yt=(p(vt={},C.V1_1,{testnet:62368684,mainnet:552635992}),p(vt,C.V2,{testnet:148607e3,mainnet:1002541853}),vt);function wt(t,e){var n=yt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:wt(s,o),note:Tt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function bt(){return(bt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:wt(s,o),note:Tt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Et,Dt,St,Ot=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Nt=.003,Pt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),kt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Rt=ht("swap"),Ft=(p(Dt={},Ot.FixedInput,ht("fixed-input")),p(Dt,Ot.FixedOutput,ht("fixed-output")),Dt),Bt={ASSET_OPT_IN:ht("asset_opt_in")},Mt={testnet:159521633,mainnet:1071281873},Ct=(p(St={},Ot.FixedInput,7),p(St,Ot.FixedOutput,8),St);function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return pt(r.asset.decimals,Number(r.amount))/pt(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Mt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Bt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[H(u[0].quote.amount_in.asset),H(u[0].quote.amount_out.asset),H(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],x=[u[0].pool.address,u[1].pool.address],h=x[0],v=x[1],T=i===Ot.FixedInput?A:ct("positive",c,A),y=i===Ot.FixedOutput?I:ct("negative",c,I),w=K(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Rt,Ft[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[h,v],suggestedParams:p,note:Tt.getAppCallTxnNoteWithClientName(C.V2)})).fee=t.ALGORAND_MIN_TX_FEE*(Ct[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(M[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(kt[e]+Lt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Nt;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Nt)}function re(e){return(kt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return pt(n.decimals,Number(n.amount))/pt(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=pt(a.decimals,Number(n))/pt(r.decimals,Number(e));return lt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:H(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:H(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:C.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=x(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=wt(n.network,C.V1_1),a=h(Y(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Z({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=ht("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:x=Z({stateArray:I}),h=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=x[h],_=x[v],b=x[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,wt(s,C.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[C.V1_1].asset1],asset2ID:u[Oe[C.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=he(C.V2),u=i.generateLogicSigAccountForPool(e),c=wt(r,C.V2),p=u.address(),l=Y(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:C.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,wt(s,C.V2)),u=null,i&&(u={asset1ID:i[Oe[C.V2].asset1],asset2ID:i[Oe[C.V2].asset2],poolTokenID:i[Oe[C.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=pt(t.asset1,e.asset1)/pt(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},C.V1_1,r(r({},Me),Ge)),p(qe,C.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=J(o,i),l=h(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,x=K(A),v=wt(s,C.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),appArgs:[ht("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:x?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:X.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(x),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,x||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=Y(i,u),f=h(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,it(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new W(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r){var s,o;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return s=e.sent,a=r[0].txn,o=t.encodeAddress(a.from.publicKey),e.abrupt("return",null==s?void 0:s.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer&&t.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&t.encodeAddress(a.rcv)===o&&r.push({id:j,amount:a.amt}),r}),[]));case 5:case"end":return e.stop()}var a}),e)})))).apply(this,arguments)}function ln(){return(ln=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(o,i),f=h(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),x=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[ht("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=fn(x),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:dn(x),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function dn(t){return function(t,e){var n=he(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(C.V2,t)+fn(t)+O}function fn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function mn(){return(mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=Y(i,u),f=h(d,2),m=f[0],g=f[1],A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(){return(gn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new W(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(t){return ln.apply(this,arguments)},signTxns:function(t){return mn.apply(this,arguments)},execute:function(t){return gn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+dn(e)}};var xn,hn,vn,Tn,yn=(p(An={},C.V1_1,en),p(An,C.V2,In),p(An,"generateTxns",(function(t){return t.contractVersion===C.V1_1?en.generateTxns(t):In.generateTxns(t)})),p(An,"signTxns",(function(t){return t.contractVersion===C.V1_1?en.signTxns(t):In.signTxns(t)})),p(An,"execute",(function(t){return t.contractVersion===C.V1_1?en.execute(t):In.execute(t)})),p(An,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===C.V1_1?en.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),wn=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),_n=5*t.ALGORAND_MIN_TX_FEE,bn=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),En=(p(xn={},bn.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,bn.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,bn.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Dn=(p(hn={},bn.INITIAL,1),p(hn,bn.SINGLE,2),p(hn,bn.FLEXIBLE,2),hn),Sn=(p(vn={},bn.INITIAL,3),p(vn,bn.FLEXIBLE,3),p(vn,bn.SINGLE,2),vn);function On(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var x=A;r=Pn(x,s),e={id:o.id,amount:x+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=Pn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Nn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function Pn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function kn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Rn=(p(Tn={},C.V1_1,[ht("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[ht("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,ht("single")],FLEXIBLE_MODE:[Ln,ht("flexible")]}),Tn);function Fn(){return(Fn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ct("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:wt(s,C.V1_1),appArgs:Rn.v1_1,accounts:[l],note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),foreignAssets:u.id==j?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),x=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),h=t.assignGroupID([x,m,g,A,I]),e.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=h(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===wn.FEE_TXN?p:n===wn.ASSET1_IN_TXN?l:n===wn.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[wn.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,mt(n,[o]);case 8:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=At(s),A=It(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(x=I.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new W(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Cn=Object.freeze({__proto__:null,execute:function(t){return Mn.apply(this,arguments)},generateTxns:function(t){return Fn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:pt(p,Number(m))/pt(u,Number(l)),priceImpact:A}}function lr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:pt(p,Number(l))/pt(u,Number(m)),priceImpact:A}}}}function dr(t){return fr.apply(this,arguments)}function fr(){return(fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=pr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:Ot.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,cr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(lr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:Ot.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,cr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var Ir={getQuote:function(t){return ur.apply(this,arguments)},getFixedInputSwapQuote:dr,getFixedInputDirectSwapQuote:pr,getFixedOutputDirectSwapQuote:lr,getFixedOutputSwapQuote:mr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ir.apply(this,arguments)},calculateFixedInputSwap:Ar};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function hr(){return(hr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:Tt.getAppCallTxnNoteWithClientName(C.V2),appArgs:[tr,t.encodeUint64(ct("negative",p,u)),t.encodeUint64(ct("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[er.ASSET_TRANSFER_TXN]=m,A[er.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[er.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[er.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,x=ct("negative",p,c),u!==d){e.next=17;break}A=x,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=x,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return h=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:Tt.getAppCallTxnNoteWithClientName(C.V2),appArgs:[tr,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[er.ASSET_TRANSFER_TXN]=h,T[er.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[er.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[er.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,t.next=3,mt(n,[s]);case 3:return o=t.sent,i=h(o,1),u=i[0].txnID,t.prev=6,t.next=9,cn(n,r);case 9:c=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var yr,wr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ir.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=Ir.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,x=A.totalFeeAmount,h=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:h}}}return e},generateTxns:function(t){return hr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Tr.apply(this,arguments)}},_r=(p(yr={},C.V1_1,sr),p(yr,C.V2,wr),yr),br=3n,Er=1000n,Dr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Dr||{});function Sr(){return(Sr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=h(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Dr.FEE_TXN_INDEX?c:n===Dr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new W({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[ht("swap"),o===Ot.FixedInput?ht("fi"):ht("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),foreignAssets:c.asset2ID==j?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),x=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,h=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:x,suggestedParams:g}),v=o===Ot.FixedInput?ct("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,h,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*br/Er,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function Pr(t){return kr.apply(this,arguments)}function kr(){return(kr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,mt(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+x,excessAmount:{assetID:i.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Er/(Er-br),l=p-c,d=pt(a.assetOut,Number(u))/pt(a.assetIn,Number(p)),f=pt(a.assetOut,Number(i))/pt(a.assetIn,Number(o)),m=lt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Rr(t){return Fr.apply(this,arguments)}function Fr(){return(Fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,mt(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-x,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new W({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Dr.ASSET_IN_TXN_INDEX].txn.assetIndex||j,amount:i[Dr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Dr.ASSET_OUT_TXN_INDEX].txn.assetIndex||j,amount:i[Dr.ASSET_OUT_TXN_INDEX].txn.amount},o!==Ot.FixedInput){t.next=12;break}return t.next=9,Pr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Rr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:It(i),fees:At(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new W(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Mr,Cr={getQuote:function(t,e,n,r,a){return t===Ot.FixedInput?Nr({pool:e,reserves:n,assetIn:r,decimals:a}):Lr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Lr,generateTxns:function(t){return Or.apply(this,arguments)},signTxns:function(t){return Sr.apply(this,arguments)},execute:function(t){return Br.apply(this,arguments)},executeFixedOutputSwap:Rr};function jr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=h(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Vr(){return(Vr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===C.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===C.V2})),c.push(Ir.getFixedInputSwapQuote({amount:r,assetIn:{id:H(s),decimals:s.decimals},assetOut:{id:H(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===C.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===C.V2})),c.push(Ir.getFixedOutputSwapQuote({amount:r,assetIn:{id:H(s),decimals:s.decimals},assetOut:{id:H(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qr=(p(Mr={},C.V1_1,Cr),p(Mr,C.V2,Ir),p(Mr,"getQuote",(function(t){var e=t.type;if(e===Ot.FixedInput)return function(t){return Vr.apply(this,arguments)}(t);if(e===Ot.FixedOutput)return function(t){return Xr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Mr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===C.V1_1?Cr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):Ir.generateTxns(t)})),p(Mr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===C.V1_1){var e=t.quote.data.pool;return Cr.signTxns(r(r({},t),{},{pool:e}))}return Ir.signTxns(t)})),p(Mr,"execute",(function(t){return t.contractVersion===C.V1_1?Cr.execute(t):Ir.execute(t)})),Mr);function Ur(){return(Ur=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,qr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,mt(n,[i]);case 7:return u=t.sent,c=h(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:At(s),confirmedRound:d,txnID:l,groupID:It(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new W(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function qr(t){return Gr.apply(this,arguments)}function Gr(){return(Gr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=h(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wr(){return Wr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:At(e),groupID:It(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,mt(r,[u]);case 4:p=a.sent,l=h(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new W(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[ht("redeem")],note:Tt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ht("commit"),u],note:$([ht("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Yr(){return(Yr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Jr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[ht("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=V,exports.ALGO_ASSET_ID=j,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Kn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=yn,exports.CONTRACT_VERSION=C,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=X,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=_r,exports.Swap=Qr,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=Ot,exports.V1_1AddLiquidityTxnIndices=wn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Zn,exports.V2AddLiquidityTxnIndices=En,exports.V2AddLiquidityType=bn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=$n,exports.applySlippageToAmount=ct,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Ur.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=xe,exports.tinymanJSSDKConfig=Tt; From 63895bf1ee01e03f66463cd796975ca3e4462709 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Thu, 13 Apr 2023 16:13:06 +0300 Subject: [PATCH 38/39] chore(swap-router): update swap router mainnet app id --- dist/index.js | 2 +- src/swap/v2/router/constants.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/dist/index.js b/dist/index.js index 0756f658..b29614ad 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}},C={V1_1:"v1_1",V2:"v2"},j=0,V={id:"".concat(j),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},X={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Q="- would result negative",U="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function Y(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function K(t){return Number(t)===j}function H(t){return Number(t.id)}function Z(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function $(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var tt=100000n,et=100000n,nt=100000n,rt=25000n+25000n,at=25000n+3500n;function st(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return tt+et*BigInt(o.length)+nt*BigInt(s.length+a.length)+at*r+rt*n}function ot(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function it(t,e){return ut.apply(this,arguments)}function ut(){return(ut=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,ot(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ct(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function pt(t,e){var n=Number(t);return lt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function lt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=h(ft(e),2),s=a[0],o=a[1],i=h(ft(Math.round(Number(dt(s,o+r)))),2),u=i[0],c=i[1];return Number(dt(u,c-r))}return Math.round(e)}function dt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ft(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function mt(t,e){return gt.apply(this,arguments)}function gt(){return(gt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,it(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new W(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function At(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function It(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function xt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ht(t){return(new TextEncoder).encode(t)}var vt,Tt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===C.V1_1?"v1":t;return ht("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),yt=(p(vt={},C.V1_1,{testnet:62368684,mainnet:552635992}),p(vt,C.V2,{testnet:148607e3,mainnet:1002541853}),vt);function wt(t,e){var n=yt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:wt(s,o),note:Tt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function bt(){return(bt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:wt(s,o),note:Tt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Et,Dt,St,Ot=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Nt=.003,Pt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),kt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Rt=ht("swap"),Ft=(p(Dt={},Ot.FixedInput,ht("fixed-input")),p(Dt,Ot.FixedOutput,ht("fixed-output")),Dt),Bt={ASSET_OPT_IN:ht("asset_opt_in")},Mt={testnet:184778019,mainnet:1071281873},Ct=(p(St={},Ot.FixedInput,7),p(St,Ot.FixedOutput,8),St);function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return pt(r.asset.decimals,Number(r.amount))/pt(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Mt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Bt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[H(u[0].quote.amount_in.asset),H(u[0].quote.amount_out.asset),H(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],x=[u[0].pool.address,u[1].pool.address],h=x[0],v=x[1],T=i===Ot.FixedInput?A:ct("positive",c,A),y=i===Ot.FixedOutput?I:ct("negative",c,I),w=K(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Rt,Ft[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[h,v],suggestedParams:p,note:Tt.getAppCallTxnNoteWithClientName(C.V2)})).fee=t.ALGORAND_MIN_TX_FEE*(Ct[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(M[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(kt[e]+Lt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Nt;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Nt)}function re(e){return(kt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return pt(n.decimals,Number(n.amount))/pt(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=pt(a.decimals,Number(n))/pt(r.decimals,Number(e));return lt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:H(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:H(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:C.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=x(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=wt(n.network,C.V1_1),a=h(Y(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Z({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=ht("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:x=Z({stateArray:I}),h=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=x[h],_=x[v],b=x[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,wt(s,C.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[C.V1_1].asset1],asset2ID:u[Oe[C.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=he(C.V2),u=i.generateLogicSigAccountForPool(e),c=wt(r,C.V2),p=u.address(),l=Y(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:C.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,wt(s,C.V2)),u=null,i&&(u={asset1ID:i[Oe[C.V2].asset1],asset2ID:i[Oe[C.V2].asset2],poolTokenID:i[Oe[C.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=pt(t.asset1,e.asset1)/pt(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},C.V1_1,r(r({},Me),Ge)),p(qe,C.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=J(o,i),l=h(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,x=K(A),v=wt(s,C.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),appArgs:[ht("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:x?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:X.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(x),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,x||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=Y(i,u),f=h(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,it(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new W(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r){var s,o;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return s=e.sent,a=r[0].txn,o=t.encodeAddress(a.from.publicKey),e.abrupt("return",null==s?void 0:s.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer&&t.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&t.encodeAddress(a.rcv)===o&&r.push({id:j,amount:a.amt}),r}),[]));case 5:case"end":return e.stop()}var a}),e)})))).apply(this,arguments)}function ln(){return(ln=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(o,i),f=h(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),x=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[ht("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=fn(x),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:dn(x),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function dn(t){return function(t,e){var n=he(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(C.V2,t)+fn(t)+O}function fn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function mn(){return(mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=Y(i,u),f=h(d,2),m=f[0],g=f[1],A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(){return(gn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new W(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(t){return ln.apply(this,arguments)},signTxns:function(t){return mn.apply(this,arguments)},execute:function(t){return gn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+dn(e)}};var xn,hn,vn,Tn,yn=(p(An={},C.V1_1,en),p(An,C.V2,In),p(An,"generateTxns",(function(t){return t.contractVersion===C.V1_1?en.generateTxns(t):In.generateTxns(t)})),p(An,"signTxns",(function(t){return t.contractVersion===C.V1_1?en.signTxns(t):In.signTxns(t)})),p(An,"execute",(function(t){return t.contractVersion===C.V1_1?en.execute(t):In.execute(t)})),p(An,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===C.V1_1?en.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),wn=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),_n=5*t.ALGORAND_MIN_TX_FEE,bn=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),En=(p(xn={},bn.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,bn.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,bn.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Dn=(p(hn={},bn.INITIAL,1),p(hn,bn.SINGLE,2),p(hn,bn.FLEXIBLE,2),hn),Sn=(p(vn={},bn.INITIAL,3),p(vn,bn.FLEXIBLE,3),p(vn,bn.SINGLE,2),vn);function On(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var x=A;r=Pn(x,s),e={id:o.id,amount:x+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=Pn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Nn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function Pn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function kn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Rn=(p(Tn={},C.V1_1,[ht("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[ht("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,ht("single")],FLEXIBLE_MODE:[Ln,ht("flexible")]}),Tn);function Fn(){return(Fn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ct("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:wt(s,C.V1_1),appArgs:Rn.v1_1,accounts:[l],note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),foreignAssets:u.id==j?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),x=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),h=t.assignGroupID([x,m,g,A,I]),e.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=h(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===wn.FEE_TXN?p:n===wn.ASSET1_IN_TXN?l:n===wn.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[wn.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,mt(n,[o]);case 8:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=At(s),A=It(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(x=I.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new W(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Cn=Object.freeze({__proto__:null,execute:function(t){return Mn.apply(this,arguments)},generateTxns:function(t){return Fn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:pt(p,Number(m))/pt(u,Number(l)),priceImpact:A}}function lr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:pt(p,Number(l))/pt(u,Number(m)),priceImpact:A}}}}function dr(t){return fr.apply(this,arguments)}function fr(){return(fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=pr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:Ot.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,cr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(lr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:Ot.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,cr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var Ir={getQuote:function(t){return ur.apply(this,arguments)},getFixedInputSwapQuote:dr,getFixedInputDirectSwapQuote:pr,getFixedOutputDirectSwapQuote:lr,getFixedOutputSwapQuote:mr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ir.apply(this,arguments)},calculateFixedInputSwap:Ar};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function hr(){return(hr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:Tt.getAppCallTxnNoteWithClientName(C.V2),appArgs:[tr,t.encodeUint64(ct("negative",p,u)),t.encodeUint64(ct("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[er.ASSET_TRANSFER_TXN]=m,A[er.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[er.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[er.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,x=ct("negative",p,c),u!==d){e.next=17;break}A=x,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=x,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return h=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:Tt.getAppCallTxnNoteWithClientName(C.V2),appArgs:[tr,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[er.ASSET_TRANSFER_TXN]=h,T[er.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[er.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[er.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,t.next=3,mt(n,[s]);case 3:return o=t.sent,i=h(o,1),u=i[0].txnID,t.prev=6,t.next=9,cn(n,r);case 9:c=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var yr,wr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ir.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=Ir.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,x=A.totalFeeAmount,h=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:h}}}return e},generateTxns:function(t){return hr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Tr.apply(this,arguments)}},_r=(p(yr={},C.V1_1,sr),p(yr,C.V2,wr),yr),br=3n,Er=1000n,Dr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Dr||{});function Sr(){return(Sr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=h(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Dr.FEE_TXN_INDEX?c:n===Dr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new W({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[ht("swap"),o===Ot.FixedInput?ht("fi"):ht("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),foreignAssets:c.asset2ID==j?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),x=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,h=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:x,suggestedParams:g}),v=o===Ot.FixedInput?ct("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,h,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*br/Er,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function Pr(t){return kr.apply(this,arguments)}function kr(){return(kr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,mt(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+x,excessAmount:{assetID:i.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Er/(Er-br),l=p-c,d=pt(a.assetOut,Number(u))/pt(a.assetIn,Number(p)),f=pt(a.assetOut,Number(i))/pt(a.assetIn,Number(o)),m=lt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Rr(t){return Fr.apply(this,arguments)}function Fr(){return(Fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,mt(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-x,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new W({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Dr.ASSET_IN_TXN_INDEX].txn.assetIndex||j,amount:i[Dr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Dr.ASSET_OUT_TXN_INDEX].txn.assetIndex||j,amount:i[Dr.ASSET_OUT_TXN_INDEX].txn.amount},o!==Ot.FixedInput){t.next=12;break}return t.next=9,Pr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Rr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:It(i),fees:At(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new W(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Mr,Cr={getQuote:function(t,e,n,r,a){return t===Ot.FixedInput?Nr({pool:e,reserves:n,assetIn:r,decimals:a}):Lr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Lr,generateTxns:function(t){return Or.apply(this,arguments)},signTxns:function(t){return Sr.apply(this,arguments)},execute:function(t){return Br.apply(this,arguments)},executeFixedOutputSwap:Rr};function jr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=h(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Vr(){return(Vr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===C.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===C.V2})),c.push(Ir.getFixedInputSwapQuote({amount:r,assetIn:{id:H(s),decimals:s.decimals},assetOut:{id:H(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===C.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===C.V2})),c.push(Ir.getFixedOutputSwapQuote({amount:r,assetIn:{id:H(s),decimals:s.decimals},assetOut:{id:H(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qr=(p(Mr={},C.V1_1,Cr),p(Mr,C.V2,Ir),p(Mr,"getQuote",(function(t){var e=t.type;if(e===Ot.FixedInput)return function(t){return Vr.apply(this,arguments)}(t);if(e===Ot.FixedOutput)return function(t){return Xr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Mr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===C.V1_1?Cr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):Ir.generateTxns(t)})),p(Mr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===C.V1_1){var e=t.quote.data.pool;return Cr.signTxns(r(r({},t),{},{pool:e}))}return Ir.signTxns(t)})),p(Mr,"execute",(function(t){return t.contractVersion===C.V1_1?Cr.execute(t):Ir.execute(t)})),Mr);function Ur(){return(Ur=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,qr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,mt(n,[i]);case 7:return u=t.sent,c=h(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:At(s),confirmedRound:d,txnID:l,groupID:It(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new W(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function qr(t){return Gr.apply(this,arguments)}function Gr(){return(Gr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=h(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wr(){return Wr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:At(e),groupID:It(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,mt(r,[u]);case 4:p=a.sent,l=h(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new W(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[ht("redeem")],note:Tt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ht("commit"),u],note:$([ht("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Yr(){return(Yr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Jr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[ht("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=V,exports.ALGO_ASSET_ID=j,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Kn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=yn,exports.CONTRACT_VERSION=C,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=X,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=_r,exports.Swap=Qr,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=Ot,exports.V1_1AddLiquidityTxnIndices=wn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Zn,exports.V2AddLiquidityTxnIndices=En,exports.V2AddLiquidityType=bn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=$n,exports.applySlippageToAmount=ct,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Ur.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=xe,exports.tinymanJSSDKConfig=Tt; +"use strict";var t=require("algosdk"),e=require("base64-js");function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function r(t){for(var e=1;e=0;--a){var s=this.tryEntries[a],o=s.completion;if("root"===s.tryLoc)return r("end");if(s.tryLoc<=this.prev){var i=n.call(s,"catchLoc"),u=n.call(s,"finallyLoc");if(i&&u){if(this.prev=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),b(n),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var a=r.arg;b(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:D(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},t}function s(t,e,n,r,a,s,o){try{var i=t[s](o),u=i.value}catch(t){return void n(t)}i.done?e(u):Promise.resolve(u).then(r,a)}function o(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var o=t.apply(e,n);function i(t){s(o,r,a,i,u,"next",t)}function u(t){s(o,r,a,i,u,"throw",t)}i(void 0)}))}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){for(var n=0;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,i=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return o=t.done,t},e:function(t){i=!0,s=t},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function _(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var b=function(t){return t.SwapRouterStaleDataError="SwapRouterStaleDataError",t.SwapRouterNoRouteError="SwapRouterNoRouteError",t.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",t.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",t.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",t.NoAvailablePoolError="NoAvailablePoolError",t.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",t.UnknownError="UnknownError",t.LowSwapAmountError="LowSwapAmountError",t.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",t.InvalidSwapTypeError="InvalidSwapTypeError",t}({}),E=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;return i(this,n),(a=e.call(this,r)).type=t,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(I(a),n),a}return c(n)}(),D=Uint8Array.from([1]),S=1e5,O=1e5,N=1e5,P=1e5,k=1e5,L=5e4,R=28500,F=1e3,B=1e3,M={mainnet:{base:"https://mainnet.analytics.tinyman.org/api",v1:"https://mainnet.analytics.tinyman.org/api/v1"},testnet:{base:"https://testnet.analytics.tinyman.org/api",v1:"https://testnet.analytics.tinyman.org/api/v1"}},C={V1_1:"v1_1",V2:"v2"},j=0,V={id:"".concat(j),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},X={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Q="- would result negative",U="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=function(t){l(n,A(Error));var e=x(n);function n(t,r){var a;i(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),u=2;ua?[r(r({},t),{},{id:n}),r(r({},e),{},{id:a})]:[r(r({},e),{},{id:a}),r(r({},t),{},{id:n})]}function Y(t,e){var n=[t,e];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function K(t){return Number(t)===j}function H(t){return Number(t.id)}function Z(t){var e,n=t.stateArray,r=void 0===n?[]:n,a=t.shouldDecodeKeys,s=void 0!==a&&a,o={},i=w(r);try{for(i.s();!(e=i.n()).done;){var u=e.value,c=u.key,p=void 0;if(1==u.value.type)p=u.value.bytes;else{if(2!=u.value.type)throw new Error("Unexpected state type: ".concat(u.value.type));p=u.value.uint}o[s?atob(c):c]=p}}catch(t){i.e(t)}finally{i.f()}return o}function $(t){var e,n=t.reduce((function(t,e){return t+e.length}),0),r=new Uint8Array(n),a=0,s=w(t);try{for(s.s();!(e=s.n()).done;){var o=e.value;r.set(o,a),a+=o.length}}catch(t){s.e(t)}finally{s.f()}return r}var tt=100000n,et=100000n,nt=100000n,rt=25000n+25000n,at=25000n+3500n;function st(t){var e=t["apps-total-schema"],n=0n,r=0n;e&&(e["num-byte-slice"]&&(n=e["num-byte-slice"]),e["num-uint"]&&(r=e["num-uint"]));var a=t["apps-local-state"]||[],s=t["created-apps"]||[],o=t.assets||[];return tt+et*BigInt(o.length)+nt*BigInt(s.length+a.length)+at*r+rt*n}function ot(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}function it(t,e){return ut.apply(this,arguments)}function ut(){return(ut=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=3,ot(1e3);case 3:return r=null,t.prev=4,t.next=7,e.pendingTransactionInformation(n).do();case 7:r=t.sent,t.next=12;break;case 10:t.prev=10,t.t0=t.catch(4);case 12:if(!r){t.next=17;break}if(!r["confirmed-round"]){t.next=15;break}return t.abrupt("return",r);case 15:if(!r["pool-error"]){t.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:t.next=0;break;case 19:case"end":return t.stop()}}),t,null,[[4,10]])})))).apply(this,arguments)}function ct(t,e,n){if(e>1||e<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(e));var r;try{var a="negative"===t?1-e:1+e;r=BigInt(Math.floor(Number(n)*a))}catch(t){throw new Error(t.message)}return r}function pt(t,e){var n=Number(t);return lt({decimalPlaces:n},Math.pow(10,-n)*Number(e))}function lt(t,e){var n=t.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=h(ft(e),2),s=a[0],o=a[1],i=h(ft(Math.round(Number(dt(s,o+r)))),2),u=i[0],c=i[1];return Number(dt(u,c-r))}return Math.round(e)}function dt(t,e){return t+(e<0?"e".concat(e):"e+".concat(e))}function ft(t){if(t.toString().includes("e")){var e=t.toString().split("e");return[parseFloat(e[0]),parseFloat(e[1])]}return[t,0]}function mt(t,e){return gt.apply(this,arguments)}function gt(){return(gt=o(a().mark((function t(e,n){var r,s,o,i,u,c,p,l;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:t.prev=0,r=[],s=w(n),t.prev=3,s.s();case 5:if((o=s.n()).done){t.next=18;break}return i=o.value,t.next=9,e.sendRawTransaction(i).do();case 9:return u=t.sent,c=u.txId,t.next=13,it(e,c);case 13:p=t.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:t.next=5;break;case 18:t.next=23;break;case 20:t.prev=20,t.t0=t.catch(3),s.e(t.t0);case 23:return t.prev=23,s.f(),t.finish(23);case 26:return t.abrupt("return",r);case 29:throw t.prev=29,t.t1=t.catch(0),new W(t.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return t.stop()}}),t,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function At(t){return t.reduce((function(t,e){return t+e.txn.fee}),0)}function It(t){return(e=t[0].txn.group)?Buffer.from(e).toString("base64"):"";var e}function xt(t){for(var e=[];;){var n=127&t;if(!(t>>=7)){e.push(n);break}e.push(128|n)}return e}function ht(t){return(new TextEncoder).encode(t)}var vt,Tt=new(function(){function t(){i(this,t),this.clientName="tinyman-js-sdk"}return c(t,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(t){this.clientName=t}},{key:"getAppCallTxnNoteWithClientName",value:function(t){var e=t===C.V1_1?"v1":t;return ht("tinyman/".concat(e,':j{"origin":"').concat(this.clientName,'"}'))}}]),t}()),yt=(p(vt={},C.V1_1,{testnet:62368684,mainnet:552635992}),p(vt,C.V2,{testnet:148607e3,mainnet:1002541853}),vt);function wt(t,e){var n=yt[e][t];if(!n)throw new Error("No Validator App exists for ".concat(t," network with ").concat(e," contract version"));return n}function _t(){return(_t=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationOptInTxnFromObject({from:i,appIndex:wt(s,o),note:Tt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function bt(){return(bt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.contractVersion,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationClearStateTxnFromObject({from:i,appIndex:wt(s,o),note:Tt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),e.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Et,Dt,St,Ot=function(t){return t.FixedInput="fixed-input",t.FixedOutput="fixed-output",t}({}),Nt=.003,Pt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),kt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Rt=ht("swap"),Ft=(p(Dt={},Ot.FixedInput,ht("fixed-input")),p(Dt,Ot.FixedOutput,ht("fixed-output")),Dt),Bt={ASSET_OPT_IN:ht("asset_opt_in")},Mt={testnet:184778019,mainnet:1083651166},Ct=(p(St={},Ot.FixedInput,7),p(St,Ot.FixedOutput,8),St);function jt(t){var e=Ut(t),n=e.assetIn,r=e.assetOut;return pt(r.asset.decimals,Number(r.amount))/pt(n.asset.decimals,Number(n.amount))}function Vt(t){var e=Mt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Xt(t){return t[t.length-1].quote.amount_out}function Qt(t){return t[0].quote.amount_in}function Ut(t){return{assetIn:Qt(t),assetOut:Xt(t)}}function qt(t){return Gt.apply(this,arguments)}function Gt(){return(Gt=o(a().mark((function e(n){var r,s,o,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return u=e.sent,c=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s,appArgs:[Bt.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=t.ALGORAND_MIN_TX_FEE*(p+1),e.abrupt("return",c);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(t){return zt.apply(this,arguments)}function zt(){return(zt=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w,_,b,E,D,S,O,N;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.initiatorAddr,s=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,e.next=3,s.getTransactionParams().do();case 3:return p=e.sent,l=[H(u[0].quote.amount_in.asset),H(u[0].quote.amount_out.asset),H(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],g=[Number(Qt(u).amount),Number(Xt(u).amount)],A=g[0],I=g[1],x=[u[0].pool.address,u[1].pool.address],h=x[0],v=x[1],T=i===Ot.FixedInput?A:ct("positive",c,A),y=i===Ot.FixedOutput?I:ct("negative",c,I),w=K(d),_=Vt(o),b=w?t.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,suggestedParams:p}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:t.getApplicationAddress(_),amount:T,assetIndex:d,suggestedParams:p}),(E=t.makeApplicationNoOpTxnFromObject({from:r,appIndex:_,appArgs:[Rt,Ft[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[h,v],suggestedParams:p,note:Tt.getAppCallTxnNoteWithClientName(C.V2)})).fee=t.ALGORAND_MIN_TX_FEE*(Ct[i]+1),D=[b,E],e.next=17,Jt({client:s,network:o,assetIDs:[d,f,m]});case 17:if(!((S=e.sent).length>0)){e.next=23;break}return e.next=21,qt({client:s,initiatorAddr:r,assetIDs:S,routerAppID:_});case 21:O=e.sent,D.unshift(O);case 23:return N=t.assignGroupID(D),e.abrupt("return",N.map((function(t){return{txn:t,signers:[r]}})));case 25:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(t){return Yt.apply(this,arguments)}function Yt(){return(Yt=o(a().mark((function e(n){var r,s,o,i,u,c;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.assetIDs,i=t.getApplicationAddress(Vt(s)),e.next=4,r.accountInformation(i).do();case 4:return u=e.sent,c=u.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!c.includes(t)})));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Kt(t){return Ht.apply(this,arguments)}function Ht(){return(Ht=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetInID,s=e.assetOutID,o=e.swapType,i=e.network,u={asset_in_id:String(r),asset_out_id:String(s),swap_type:o,amount:String(n)},t.next=4,fetch("".concat(M[i].v1,"/swap-router/quotes/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=t.sent,t.next=7,c.json();case 7:if(p=t.sent,c.ok){t.next=14;break}if(a=p,!Boolean(a)||void 0===a.fallback_message){t.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(b.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){t.next=16;break}throw new E(b.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return t.abrupt("return",p);case 17:case"end":return t.stop()}var a}),t)})))).apply(this,arguments)}var Zt=function(t){return t.Direct="direct",t.Router="router",t}({}),$t=4*t.ALGORAND_MIN_TX_FEE;function te(e){return(kt[e]+Lt)*t.ALGORAND_MIN_TX_FEE}function ee(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Nt;return Math.ceil(1/t)}function ne(t){return t1&&void 0!==arguments[1]?arguments[1]:Nt)}function re(e){return(kt[e]+1)*t.ALGORAND_MIN_TX_FEE}function ae(t){var e=t.assetIn,n=t.assetOut;return pt(n.decimals,Number(n.amount))/pt(e.decimals,Number(e.amount))}function se(t){var e=t.inputSupply,n=t.outputSupply,r=t.assetIn,a=t.assetOut,s=ae({assetIn:r,assetOut:a}),o=pt(a.decimals,Number(n))/pt(r.decimals,Number(e));return lt({decimalPlaces:5},Math.abs(s/o-1))}function oe(t){if(t.type===Zt.Router){var e=Qt(t.data.route);return{id:H(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetInID,amount:t.data.quote.assetInAmount}}function ie(t){if(t.type===Zt.Router){var e=Xt(t.data.route);return{id:H(e.asset),amount:Number(e.amount)}}return{id:t.data.quote.assetOutID,amount:t.data.quote.assetOutAmount}}function ue(t){return t.type===Zt.Direct?t.data.pool.contractVersion:C.V2}function ce(t){return t.type===Zt.Direct?t.data.quote.rate:jt(t.data.route)}function pe(t){for(var e=t[0],n=ce(e),r=1;rn&&(e=a,n=s)}return e}function le(t){return t instanceof E&&[b.SwapRouterInsufficientReservesError,b.SwapRouterLowSwapAmountError,b.OutputAmountExceedsAvailableLiquidityError,b.LowSwapAmountError].includes(t.type)}var de={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},fe={type:"app",global_state_schema:{num_uints:0,num_byte_slices:0},local_state_schema:{num_uints:16,num_byte_slices:0},name:"validator_app"},me=c((function t(e){i(this,t),this.schema={numLocalInts:e.local_state_schema.num_uints,numLocalByteSlices:e.local_state_schema.num_byte_slices,numGlobalInts:e.global_state_schema.num_uints,numGlobalByteSlices:e.global_state_schema.num_byte_slices}})),ge=new(function(n){l(a,me);var r=x(a);function a(t,e){var n;return i(this,a),(n=r.call(this,t)).poolLogicSigContractTemplate=e.logic.bytecode,n.templateVariables=e.logic.variables,n}return c(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=wt(n.network,C.V1_1),a=h(Y(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(e.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(t,e){return t.index-e.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:t.IntDecoding.DEFAULT;return new Promise(function(){var t=o(a().mark((function t(o,i){var u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,e.accountInformation(n).setIntDecoding(s).do();case 3:u=t.sent,o(r(r({},u),{},{minimum_required_balance:ye(u)})),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),i(new Error(t.t0.message||"Failed to fetch account information"));case 10:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e,n){return t.apply(this,arguments)}}())}function Te(t,e){var n=t["apps-local-state"].find((function(t){return t.id===e}));return n?Z({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function ye(t){var e=t["apps-total-schema"];return S+O*(t.assets||[]).length+P*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+R*Number(e&&e["num-uint"]||0)+k*(t["apps-total-extra-pages"]||0)}var we=ht("e");function _e(t){return be.apply(this,arguments)}function be(){return(be=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,_,b,E;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.pool,i=r.accountAddr,n.next=3,s.accountInformation(i).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=w(c),n.prev=10,m.s();case 12:if((g=m.n()).done){n.next=31;break}if((A=g.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(I=A["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:x=Z({stateArray:I}),h=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset1ID)])),v=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.asset2ID)])),T=e.fromByteArray($([t.decodeAddress(f).publicKey,we,t.encodeUint64(o.poolTokenID)])),y=x[h],_=x[v],b=x[T],"bigint"==typeof y&&(p=y),"bigint"==typeof _&&(l=_),"bigint"==typeof b&&(d=b);case 29:n.next=12;break;case 31:n.next=36;break;case 33:n.prev=33,n.t0=n.catch(10),m.e(n.t0);case 36:return n.prev=36,m.f(),n.finish(36);case 39:if(!((E={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||E.excessAsset2<0n||E.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(E));case 42:return n.abrupt("return",E);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Ee(){return(Ee=o(a().mark((function n(r){var s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,s.accountInformation(o).setIntDecoding(t.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(t){return t.id==i})),l=[],p&&p["key-value"])for(d=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fke)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(ke,"]): ").concat(k.issuedLiquidity));case 48:return n.abrupt("return",k);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Be(){return Be=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d=arguments;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,r=e.address,s=e.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){t.next=4;break}return t.abrupt("return",o[r]);case 4:return t.next=6,ve(n,r);case 6:return i=t.sent,u=Te(i,wt(s,C.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Oe[C.V1_1].asset1],asset2ID:u[Oe[C.V1_1].asset2],poolTokenID:p},o[r]=c),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t)}))),Be.apply(this,arguments)}var Me=Object.freeze({__proto__:null,getPoolAssets:function(t){return Be.apply(this,arguments)},getPoolInfo:Le,getPoolReserves:function(t,e){return Fe.apply(this,arguments)}});function Ce(t){return je.apply(this,arguments)}function je(){return(je=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.asset1ID,o=e.asset2ID,i=he(C.V2),u=i.generateLogicSigAccountForPool(e),c=wt(r,C.V2),p=u.address(),l=Y(s,o),t.next=8,ve(n,p);case 8:return d=t.sent,f=Te(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Se.READY:Se.NOT_CREATED,contractVersion:C.V2},f&&(m.asset1ProtocolFees=BigInt(f[Oe.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Oe.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Oe.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Oe.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Oe.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Oe.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Oe.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Oe.v2.totalFeeShare]),m.poolTokenID=Number(f[Oe.v2.poolTokenID]),m.asset1ID=Number(f[Oe.v2.asset1]),m.asset2ID=Number(f[Oe.v2.asset2])),t.abrupt("return",m);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ve(){return(Ve=o(a().mark((function t(e,n){var r,s,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,ve(e,n.account.address());case 2:return r=t.sent,s=Te(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},s&&(o.asset1=BigInt(s[Oe.v2.asset1Reserves]),o.asset2=BigInt(s[Oe.v2.asset2Reserves]),o.issuedLiquidity=BigInt(s[Oe.v2.issuedPoolTokens])),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xe(){return(Xe=o(a().mark((function t(e){var n,r,s,o,i,u;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.address,s=e.network,t.next=3,ve(n,r);case 3:return o=t.sent,i=Te(o,wt(s,C.V2)),u=null,i&&(u={asset1ID:i[Oe[C.V2].asset1],asset2ID:i[Oe[C.V2].asset2],poolTokenID:i[Oe[C.V2].poolTokenID]}),t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qe=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Ue(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Ue(e),r=null;return e&&!n&&e.asset1&&e.asset2&&"number"==typeof t.asset2&&"number"==typeof t.asset1&&(r=pt(t.asset1,e.asset1)/pt(t.asset2,e.asset2)),r},getPoolShare:function(t,e){var n=Number(e)/Number(t);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(t){return Promise.all([Le(t),Ce(t)])},isPoolEmpty:Ue,isPoolNotCreated:function(t){return(null==t?void 0:t.status)===Se.NOT_CREATED},isPoolReady:function(t){return(null==t?void 0:t.status)===Se.READY}}),We=r((p(qe={},C.V1_1,r(r({},Me),Ge)),p(qe,C.V2,r(r({},Qe),Ge)),qe),Ge),ze=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t[t.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",t[t.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",t[t.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",t}({}),Je={ASA_ALGO:96e4,ASA_ASA:859e3};function Ye(){return(Ye=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_,b,E,D,S,O;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=J(o,i),l=h(p,2),d=l[0],f=d.id,m=d.unit_name,g=l[1],A=g.id,I=g.unit_name,x=K(A),v=wt(s,C.V1_1),T=ge.generateLogicSigAccountForPool({network:s,asset1ID:f,asset2ID:A}),y=T.address(),w=t.makeApplicationOptInTxnFromObject({from:y,appIndex:v,note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),appArgs:[ht("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:x?[f]:[A],suggestedParams:c}),_=t.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:X.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(I),assetURL:"https://tinyman.org",suggestedParams:c}),b=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),E=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Ke(x),suggestedParams:c}),(D=[])[ze.FUNDING_TXN]=E,D[ze.VALIDATOR_APP_CALL]=w,D[ze.POOL_TOKEN_CREATE]=_,D[ze.ASSET1_OPT_IN]=b,x||(D[ze.ASSET2_OPT_IN]=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:A,amount:0,suggestedParams:c})),S=t.assignGroupID(D),O=[{txn:S[ze.FUNDING_TXN],signers:[u]},{txn:S[ze.VALIDATOR_APP_CALL],signers:[y]},{txn:S[ze.POOL_TOKEN_CREATE],signers:[y]},{txn:S[ze.ASSET1_OPT_IN],signers:[y]}],S[ze.ASSET2_OPT_IN]&&O.push({txn:S[ze.ASSET2_OPT_IN],signers:[y]}),e.abrupt("return",O);case 23:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ke(t){return t?Je.ASA_ALGO:Je.ASA_ASA}function He(){return(He=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=Y(i,u),f=h(d,2),m=f[0],g=f[1],A=ge.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===ze.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ze(t){return $e.apply(this,arguments)}function $e(){return($e=o(a().mark((function t(e){var n,r,s,o,i;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.signedTxns,s=e.txnIDs,t.prev=1,t.next=4,n.sendRawTransaction(r).do();case 4:return t.next=6,it(n,s[ze.POOL_TOKEN_CREATE]);case 6:if(o=t.sent,"number"==typeof(i=o["asset-index"])){t.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return t.abrupt("return",{poolTokenID:i});case 13:throw t.prev=13,t.t0=t.catch(1),new W(t.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return t.stop()}}),t,null,[[1,13]])})))).apply(this,arguments)}function tn(){return(tn=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.signedTxns,c=e.txnIDs,t.next=3,Ze({client:n,signedTxns:u,txnIDs:c});case 3:return t.abrupt("return",We.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var en={generateTxns:function(t){return Ye.apply(this,arguments)},signTxns:function(t){return He.apply(this,arguments)},execute:function(t){return tn.apply(this,arguments)},getBootstrapFundingTxnAmount:Ke};var nn=function(t){return t[t.FUNDING_TXN=0]="FUNDING_TXN",t[t.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",t}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(t,e){return sn.apply(this,arguments)}function sn(){return(sn=o(a().mark((function e(n,r){var s,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(o=null===(s=r.find((function(t){return"appl"===t.txn.type})))||void 0===s?void 0:s.txn.txID())){e.next=7;break}return e.next=4,t.waitForConfirmation(n,o,B);case 4:e.t0=e.sent,e.next=8;break;case 7:e.t0=void 0;case 8:return i=e.t0,e.abrupt("return",i);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function on(t,e){return un.apply(this,arguments)}function un(){return(un=o(a().mark((function t(e,n){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,an(e,n);case 2:return r=t.sent,t.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(t,e){return pn.apply(this,arguments)}function pn(){return(pn=o(a().mark((function e(n,r){var s,o;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(n,r);case 2:return s=e.sent,a=r[0].txn,o=t.encodeAddress(a.from.publicKey),e.abrupt("return",null==s?void 0:s.reduce((function(e,n){var r=e,a=n.txn.txn;return a.type===t.TransactionType.axfer&&t.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===t.TransactionType.pay&&t.encodeAddress(a.rcv)===o&&r.push({id:j,amount:a.amt}),r}),[]));case 5:case"end":return e.stop()}var a}),e)})))).apply(this,arguments)}function ln(){return(ln=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v,T,y,w,_;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(o,i),f=h(d,2),m=f[0].id,g=f[1].id,e.next=9,We.v2.getPoolInfo({client:r,network:s,asset1ID:m,asset2ID:g});case 9:if(e.sent.status!==Se.READY){e.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=A.address(),x=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[ht("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.V2),foreignAssets:[m,g],rekeyTo:l,suggestedParams:c})).fee=fn(x),T=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:I,amount:dn(x),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=T,y[nn.VALIDATOR_APP_CALL]=v,w=t.assignGroupID(y),(_=[])[nn.FUNDING_TXN]={txn:w[nn.FUNDING_TXN],signers:[u]},_[nn.VALIDATOR_APP_CALL]={txn:w[nn.VALIDATOR_APP_CALL],signers:[I]},e.abrupt("return",_);case 26:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function dn(t){return function(t,e){var n=he(t).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+O+O+N+R*r+L*a;return e||(s+=O),s}(C.V2,t)+fn(t)+O}function fn(e){return((e?rn.ASA_ALGO:rn.ASA_ASA)+1)*t.ALGORAND_MIN_TX_FEE}function mn(){return(mn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,e.next=3,o([r]);case 3:return c=e.sent,p=h(c,1),l=p[0],d=Y(i,u),f=h(d,2),m=f[0],g=f[1],A=xe.generateLogicSigAccountForPool({network:s,asset1ID:m,asset2ID:g}),I=[],x=r.map((function(e,n){if(n===nn.FUNDING_TXN)return I.push(e.txn.txID().toString()),l;var r=t.signLogicSigTransactionObject(e.txn,A),a=r.txID,s=r.blob;return I.push(a),s})),e.abrupt("return",{signedTxns:x,txnIDs:I});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(){return(gn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.network,s=e.pool,o=s.asset1ID,i=s.asset2ID,u=e.txGroup,c=e.signedTxns,t.prev=1,t.next=4,n.sendRawTransaction(c).do();case 4:return t.next=6,an(n,u);case 6:if(t.t1=p=t.sent,t.t0=null===t.t1,t.t0){t.next=10;break}t.t0=void 0===p;case 10:if(!t.t0){t.next=14;break}t.t2=void 0,t.next=15;break;case 14:t.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(t){return t.key===btoa(Oe.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=t.t2)){t.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return t.abrupt("return",We.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw t.prev=21,t.t3=t.catch(1),new W(t.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return t.stop()}}),t,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(t){return ln.apply(this,arguments)},signTxns:function(t){return mn.apply(this,arguments)},execute:function(t){return gn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(e){return t.ALGORAND_MIN_TX_FEE+dn(e)}};var xn,hn,vn,Tn,yn=(p(An={},C.V1_1,en),p(An,C.V2,In),p(An,"generateTxns",(function(t){return t.contractVersion===C.V1_1?en.generateTxns(t):In.generateTxns(t)})),p(An,"signTxns",(function(t){return t.contractVersion===C.V1_1?en.signTxns(t):In.signTxns(t)})),p(An,"execute",(function(t){return t.contractVersion===C.V1_1?en.execute(t):In.execute(t)})),p(An,"calculateBootstrapFundingTxnAmount",(function(t){var e=t.contractVersion,n=t.isAlgoPool;return e===C.V1_1?en.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),wn=function(t){return t[t.FEE_TXN=0]="FEE_TXN",t[t.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",t[t.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",t[t.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",t[t.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",t}({}),_n=5*t.ALGORAND_MIN_TX_FEE,bn=function(t){return t.SINGLE="single",t.FLEXIBLE="flexible",t.INITIAL="initial",t}({}),En=(p(xn={},bn.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),p(xn,bn.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),p(xn,bn.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),xn),Dn=(p(hn={},bn.INITIAL,1),p(hn,bn.SINGLE,2),p(hn,bn.FLEXIBLE,2),hn),Sn=(p(vn={},bn.INITIAL,3),p(vn,bn.FLEXIBLE,3),p(vn,bn.SINGLE,2),vn);function On(t){var e,n,r,a=t.reserves,s=t.totalFeeShare,o=t.asset1,i=t.asset2,u=a.asset1*a.asset2,c=a.asset1+BigInt(o.amount),p=a.asset2+BigInt(i.amount),l=c*p,d=BigInt(parseInt(String(Math.sqrt(Number(l*a.issuedLiquidity*a.issuedLiquidity/u))))),f=d-a.issuedLiquidity,m=f*c/d,g=f*p/d,A=BigInt(o.amount)-m,I=BigInt(i.amount)-g;if(A>I){var x=A;r=Pn(x,s),e={id:o.id,amount:x+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(I),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var h=I;r=Pn(h,s),e={id:i.id,amount:h+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(A),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:e,assetOut:n,swapFees:r,priceImpact:se({inputSupply:e.reserves,outputSupply:n.reserves,assetIn:e,assetOut:n})}}}function Nn(t,e){if(!t.amount||!e.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(t.amount)*Number(e.amount))-Ne)))}function Pn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function kn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Rn=(p(Tn={},C.V1_1,[ht("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[ht("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,ht("single")],FLEXIBLE_MODE:[Ln,ht("flexible")]}),Tn);function Fn(){return(Fn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=ct("negative",p,c.amount),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:wt(s,C.V1_1),appArgs:Rn.v1_1,accounts:[l],note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),foreignAssets:u.id==j?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),A=u.id===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),I=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),x=t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+I.fee,note:D,suggestedParams:f}),h=t.assignGroupID([x,m,g,A,I]),e.abrupt("return",[{txn:h[0],signers:[l]},{txn:h[1],signers:[o]},{txn:h[2],signers:[l]},{txn:h[3],signers:[l]},{txn:h[4],signers:[o]}]);case 12:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Bn(){return(Bn=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,i=r.account,e.next=4,o([s]);case 4:return u=e.sent,c=h(u,3),p=c[0],l=c[1],d=c[2],f=s.map((function(e,n){return n===wn.FEE_TXN?p:n===wn.ASSET1_IN_TXN?l:n===wn.ASSET2_IN_TXN?d:t.signLogicSigTransactionObject(e.txn,i).blob})),e.abrupt("return",f);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mn(){return(Mn=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,v;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.signedTxns,i=e.initiatorAddr,t.prev=1,u=BigInt(s[wn.LIQUDITY_OUT_TXN].txn.amount),t.next=5,_e({client:n,pool:r,accountAddr:i});case 5:return c=t.sent,t.next=8,mt(n,[o]);case 8:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,g=At(s),A=It(s),t.next=17,_e({client:n,pool:r,accountAddr:i});case 17:return I=t.sent,(x=I.excessPoolTokens-c.excessPoolTokens)<0n&&(x=0n),t.abrupt("return",{round:f,fees:g,poolTokenID:r.poolTokenID,poolTokenOut:u+x,excessAmount:{excessAmountForAddingLiquidity:x,totalExcessAmount:I.excessPoolTokens},txnID:m,groupID:A});case 23:throw t.prev=23,t.t0=t.catch(1),"SlippageTolerance"===(v=new W(t.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&v.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),v;case 28:case"end":return t.stop()}}),t,null,[[1,23]])})))).apply(this,arguments)}var Cn=Object.freeze({__proto__:null,execute:function(t){return Mn.apply(this,arguments)},generateTxns:function(t){return Fn.apply(this,arguments)},getQuote:function(t){var e=t.pool,n=t.reserves,r=t.asset1In,a=t.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(F))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(F,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:e.asset1ID,asset1In:BigInt(r),asset2ID:e.asset2ID,asset2In:BigInt(a),poolTokenID:e.poolTokenID,poolTokenOut:s-BigInt(F),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(e)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(g),rate:pt(p,Number(m))/pt(u,Number(l)),priceImpact:A}}function lr(t){var e=t.amount,n=t.assetIn,r=t.assetOut,a=t.pool;if(!a||!We.isPoolReady(a))throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i=n.id,u=n.decimals,c=r.id,p=r.decimals,l=BigInt(e),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapOutputAmount,a=t.totalFeeShare,s=t.decimals,o=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.outputAmount,a=e*n,s=BigInt(a/(n-r))-e;return s+=BigInt(1),s}({inputSupply:e,outputSupply:n,outputAmount:r}),i=function(t){var e=t.swapAmount,n=t.totalFeeShare,r=Math.floor(Number(e*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-e;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=se({inputSupply:e,outputSupply:n,assetIn:{amount:u,decimals:s.assetIn},assetOut:{amount:r,decimals:s.assetOut}});return{swapInputAmount:u,totalFeeAmount:i,priceImpact:c}}({inputSupply:s,outputSupply:o,swapOutputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapInputAmount,g=f.totalFeeAmount,A=f.priceImpact;if(l>o)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(ne(Number(m)))throw new E(b.LowSwapAmountError,"Swap amount is too low.");return{type:Zt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(g),rate:pt(p,Number(l))/pt(u,Number(m)),priceImpact:A}}}}function dr(t){return fr.apply(this,arguments)}function fr(){return(fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[],u?c.push(new Promise((function(t,e){try{var a=pr({amount:n,assetIn:r,assetOut:s,pool:u});t({type:Zt.Direct,data:{pool:u,quote:a}})}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:Ot.FixedInput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=6,cr(c);case 6:return p=t.sent,t.abrupt("return",pe(p));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mr(t){return gr.apply(this,arguments)}function gr(){return(gr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.amount,r=e.assetIn,s=e.assetOut,o=e.isSwapRouterEnabled,i=e.network,u=e.pool,c=[new Promise((function(t,e){try{t(lr({amount:n,assetIn:r,assetOut:s,pool:u}))}catch(t){e(t)}}))],o&&c.push(Kt({amount:n,assetInID:r.id,assetOutID:s.id,swapType:Ot.FixedOutput,network:i}).then((function(t){return{type:Zt.Router,data:t}}))),t.next=5,cr(c);case 5:return p=t.sent,t.abrupt("return",pe(p));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ar(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapInputAmount,a=t.totalFeeShare,s=t.decimals,o=BigInt(function(t){var e=t.inputAmount,n=t.totalFeeShare;return Math.floor(Number(e*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(t){var e=t.inputSupply,n=t.outputSupply,r=t.swapAmount,a=e*n,s=n-BigInt(a/(e+BigInt(r)));return s-=BigInt(1)}({inputSupply:e,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:se({inputSupply:e,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var Ir={getQuote:function(t){return ur.apply(this,arguments)},getFixedInputSwapQuote:dr,getFixedInputDirectSwapQuote:pr,getFixedOutputDirectSwapQuote:lr,getFixedOutputSwapQuote:mr,generateTxns:function(t){return or.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return ir.apply(this,arguments)},calculateFixedInputSwap:Ar};function xr(t,e){var n,r,a=BigInt(t),s=e.issuedLiquidity;return s>a+BigInt(Ne)?(n=a*e.asset1/s,r=a*e.asset2/s):(n=e.asset1,r=e.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function hr(){return(hr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.account.address(),f=s.poolTokenID){e.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(g=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:s.validatorAppID,note:Tt.getAppCallTxnNoteWithClientName(C.V2),appArgs:[tr,t.encodeUint64(ct("negative",p,u)),t.encodeUint64(ct("negative",p,c))],accounts:[d],foreignAssets:[s.asset1ID,s.asset2ID],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(A=[])[er.ASSET_TRANSFER_TXN]=m,A[er.APP_CALL_TXN]=g,I=t.assignGroupID(A),e.abrupt("return",[{txn:I[er.ASSET_TRANSFER_TXN],signers:[i]},{txn:I[er.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(){return(vr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=s.asset1ID,f=s.asset2ID,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(A=0,I=0,x=ct("negative",p,c),u!==d){e.next=17;break}A=x,I=0,e.next=23;break;case 17:if(u!==f){e.next=22;break}A=0,I=x,e.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return h=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:g,amount:i,suggestedParams:l}),(v=t.makeApplicationNoOpTxnFromObject({from:o,appIndex:s.validatorAppID,note:Tt.getAppCallTxnNoteWithClientName(C.V2),appArgs:[tr,t.encodeUint64(A),t.encodeUint64(I)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=($n+1)*t.ALGORAND_MIN_TX_FEE,(T=[])[er.ASSET_TRANSFER_TXN]=h,T[er.APP_CALL_TXN]=v,y=t.assignGroupID(T),e.abrupt("return",[{txn:y[er.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[er.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Tr(){return(Tr=o(a().mark((function t(e){var n,r,s,o,i,u,c;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.txGroup,s=e.signedTxns,t.next=3,mt(n,[s]);case 3:return o=t.sent,i=h(o,1),u=i[0].txnID,t.prev=6,t.next=9,cn(n,r);case 9:c=t.sent,t.next=14;break;case 12:t.prev=12,t.t0=t.catch(6);case 14:return t.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return t.stop()}}),t,null,[[6,12]])})))).apply(this,arguments)}var yr,wr={getQuote:function(t){var e=t.pool,n=t.reserves,r=t.poolTokenIn,a=BigInt(r),s=xr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:e.asset1ID,amount:o},asset2Out:{assetId:e.asset2ID,amount:i},poolTokenIn:{assetId:e.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(t){var e,n=t.pool,r=t.reserves,a=t.poolTokenIn,s=t.assetOutID,o=t.decimals,i=BigInt(a),u=xr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=Ir.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,g=d.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:c+f},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:f},swapFees:{assetId:n.asset2ID,amount:m},priceImpact:g}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var A=Ir.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),I=A.swapOutputAmount,x=A.totalFeeAmount,h=A.priceImpact;e={round:r.round,assetOut:{assetId:s,amount:p+I},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:I},swapFees:{assetId:n.asset2ID,amount:x},priceImpact:h}}}return e},generateTxns:function(t){return hr.apply(this,arguments)},generateSingleAssetOutTxns:function(t){return vr.apply(this,arguments)},signTxns:function(t){var e=t.txGroup;return(0,t.initiatorSigner)([e])},execute:function(t){return Tr.apply(this,arguments)}},_r=(p(yr={},C.V1_1,sr),p(yr,C.V2,wr),yr),br=3n,Er=1000n,Dr=function(t){return t[t.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",t[t.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",t[t.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",t[t.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",t}(Dr||{});function Sr(){return(Sr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.pool,s=n.txGroup,o=n.initiatorSigner,e.next=3,o([s]);case 3:return i=e.sent,u=h(i,2),c=u[0],p=u[1],l=s.map((function(e,n){return n===Dr.FEE_TXN_INDEX?c:n===Dr.ASSET_IN_TXN_INDEX?p:t.signLogicSigTransactionObject(e.txn,r.account.lsig).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Or(){return(Or=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v,T,y,w;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=n.client,s=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=s.pool,p=s.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){e.next=7;break}throw new W({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return e.next=9,r.getTransactionParams().do();case 9:return g=e.sent,A=[ht("swap"),o===Ot.FixedInput?ht("fi"):ht("fo")],I=t.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:A,accounts:[u],note:Tt.getAppCallTxnNoteWithClientName(C.V1_1),foreignAssets:c.asset2ID==j?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:g}),x=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,h=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:x,suggestedParams:g}),v=o===Ot.FixedInput?ct("negative",i,p.assetOutAmount):p.assetOutAmount,T=d===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:g}),y=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:I.fee+T.fee,note:D,suggestedParams:g}),w=t.assignGroupID([y,I,h,T]),e.abrupt("return",[{txn:w[0],signers:[u]},{txn:w[1],signers:[f]},{txn:w[2],signers:[u]},{txn:w[3],signers:[f]}]);case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Nr(t){var e=t.pool,n=t.reserves,a=t.assetIn,s=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(a.amount);if(a.id===e.asset1ID)o=e.asset2ID,i=n.asset1,u=n.asset2;else{if(a.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Input asset (#".concat(a.id,") doesn't belong to the pool ").concat(e.account.address(),"."));o=e.asset1ID,i=n.asset2,u=n.asset1}var p=c*br/Er,l=u-i*u/(i+(c-p));if(l>u)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var d={assetIn:{amount:c,decimals:s.assetIn},assetOut:{amount:l,decimals:s.assetOut}},f={round:n.round,assetInID:a.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ae(d),priceImpact:se(r({inputSupply:i,outputSupply:u},d))};return{type:Zt.Direct,data:{pool:e,quote:f}}}function Pr(t){return kr.apply(this,arguments)}function kr(){return(kr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,mt(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,i.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+x,excessAmount:{assetID:i.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(t){var e=t.pool,n=t.reserves,r=t.assetOut,a=t.decimals;if(e.status!==Se.READY)throw new E(b.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===e.asset1ID)s=e.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==e.asset2ID)throw new E(b.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(e.account.address(),"."));s=e.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(b.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Er/(Er-br),l=p-c,d=pt(a.assetOut,Number(u))/pt(a.assetIn,Number(p)),f=pt(a.assetOut,Number(i))/pt(a.assetIn,Number(o)),m=lt({decimalPlaces:5},Math.abs(d/f-1)),g={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Zt.Direct,data:{pool:e,quote:g}}}function Rr(t){return Fr.apply(this,arguments)}function Fr(){return(Fr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d,f,m,g,A,I,x;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.signedTxns,o=e.assetIn,i=e.assetOut,u=e.initiatorAddr,t.next=3,_e({client:n,pool:r,accountAddr:u});case 3:return c=t.sent,t.next=6,mt(n,[s]);case 6:return p=t.sent,l=h(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,t.next=13,_e({client:n,pool:r,accountAddr:u});case 13:return g=t.sent,o.id===r.asset1ID?(A=c.excessAsset1,I=g.excessAsset1):(A=c.excessAsset2,I=g.excessAsset2),(x=I-A)<0n&&(x=0n),t.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-x,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:x,totalExcessAmount:I},txnID:m});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Br(){return(Br=o(a().mark((function t(e){var n,s,o,i,u,c,p,l,d,f;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.client,s=e.pool,o=e.swapType,i=e.txGroup,u=e.signedTxns,c=e.initiatorAddr,s.status===Se.READY){t.next=3;break}throw new W({pool:s,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(t.prev=3,p={id:i[Dr.ASSET_IN_TXN_INDEX].txn.assetIndex||j,amount:i[Dr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Dr.ASSET_OUT_TXN_INDEX].txn.assetIndex||j,amount:i[Dr.ASSET_OUT_TXN_INDEX].txn.amount},o!==Ot.FixedInput){t.next=12;break}return t.next=9,Pr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=t.sent,t.next=15;break;case 12:return t.next=14,Rr({client:n,pool:s,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=t.sent;case 15:return t.abrupt("return",r(r({},d),{},{groupID:It(i),fees:At(i)}));case 18:throw t.prev=18,t.t0=t.catch(3),"SlippageTolerance"===(f=new W(t.t0,"We encountered something unexpected while swapping. Try again later.")).type&&f.setMessage("The swap failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),f;case 23:case"end":return t.stop()}}),t,null,[[3,18]])})))).apply(this,arguments)}var Mr,Cr={getQuote:function(t,e,n,r,a){return t===Ot.FixedInput?Nr({pool:e,reserves:n,assetIn:r,decimals:a}):Lr({pool:e,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Lr,generateTxns:function(t){return Or.apply(this,arguments)},signTxns:function(t){return Sr.apply(this,arguments)},execute:function(t){return Br.apply(this,arguments)},executeFixedOutputSwap:Rr};function jr(t){return Promise.allSettled(t).then((function(t){if(t.every((function(t){return"rejected"===t.status}))){var e=h(t.map((function(t){return t.reason})),2),n=e[0],r=e[1];if(le(n)&&!le(r))throw n;throw r}return t.filter((function(t){return"fulfilled"===t.status&&t.value})).map((function(t){return t.value}))}))}function Vr(){return(Vr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.isSwapRouterEnabled,u=e.pools,c=[],(p=u.find((function(t){return t.info.contractVersion===C.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(s.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(t){return t.info.contractVersion===C.V2})),c.push(Ir.getFixedInputSwapQuote({amount:r,assetIn:{id:H(s),decimals:s.decimals},assetOut:{id:H(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xr(){return(Xr=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.amount,s=e.assetIn,o=e.assetOut,i=e.pools,u=e.isSwapRouterEnabled,c=[],(p=i.find((function(t){return t.info.contractVersion===C.V1_1})))?c.push(new Promise((function(t,e){try{t(Cr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:s.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(t){e(t)}}))):c.push(Promise.reject(new E(b.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(t){return t.info.contractVersion===C.V2})),c.push(Ir.getFixedOutputSwapQuote({amount:r,assetIn:{id:H(s),decimals:s.decimals},assetOut:{id:H(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:e.network})),t.next=8,jr(c);case 8:return d=t.sent,t.abrupt("return",pe(d));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Qr=(p(Mr={},C.V1_1,Cr),p(Mr,C.V2,Ir),p(Mr,"getQuote",(function(t){var e=t.type;if(e===Ot.FixedInput)return function(t){return Vr.apply(this,arguments)}(t);if(e===Ot.FixedOutput)return function(t){return Xr.apply(this,arguments)}(t);throw new E(b.InvalidSwapTypeError,"Invalid swap type")})),p(Mr,"generateTxns",(function(t){return t.quote.type===Zt.Direct&&ue(t.quote)===C.V1_1?Cr.generateTxns(r(r({},t),{},{quoteAndPool:t.quote.data})):Ir.generateTxns(t)})),p(Mr,"signTxns",(function(t){if(t.quote.type===Zt.Direct&&ue(t.quote)===C.V1_1){var e=t.quote.data.pool;return Cr.signTxns(r(r({},t),{},{pool:e}))}return Ir.signTxns(t)})),p(Mr,"execute",(function(t){return t.contractVersion===C.V1_1?Cr.execute(t):Ir.execute(t)})),Mr);function Ur(){return(Ur=o(a().mark((function t(e){var n,r,s,o,i,u,c,p,l,d;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.client,r=e.pool,s=e.txGroup,o=e.initiatorSigner,t.prev=1,t.next=4,qr({txGroup:s,pool:r,initiatorSigner:o});case 4:return i=t.sent,t.next=7,mt(n,[i]);case 7:return u=t.sent,c=h(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,t.abrupt("return",{fees:At(s),confirmedRound:d,txnID:l,groupID:It(s)});case 15:throw t.prev=15,t.t0=t.catch(1),new W(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return t.stop()}}),t,null,[[1,15]])})))).apply(this,arguments)}function qr(t){return Gr.apply(this,arguments)}function Gr(){return(Gr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.txGroup,s=n.pool,o=n.initiatorSigner,e.next=3,o([r]);case 3:return i=e.sent,u=h(i,1),c=u[0],p=s.account.lsig,l=r.map((function(e,n){return 0===n?c:t.signLogicSigTransactionObject(e.txn,p).blob})),e.abrupt("return",l);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wr(){return Wr=o(a().mark((function e(n){var r,s,i,u,c,p;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.data,i=n.initiatorSigner,e.prev=1,u=s.map((function(t){var e=t.txGroup,n=t.pool;return{txns:e,txnFees:At(e),groupID:It(e),lsig:n.account.lsig}})),e.next=5,i(u.map((function(t){return t.txns})));case 5:return c=e.sent,p=Promise.all(u.map((function(e,n){return new Promise(function(){var s=o(a().mark((function s(o,i){var u,p,l,d,f,m;return a().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=e.txns.map((function(r,a){return 0===a?c[n]:t.signLogicSigTransactionObject(r.txn,e.lsig).blob})),a.next=4,mt(r,[u]);case 4:p=a.sent,l=h(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:e.txnFees,groupID:e.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),s,null,[[0,12]])})));return function(t,e){return s.apply(this,arguments)}}())}))),e.abrupt("return",p);case 10:throw e.prev=10,e.t0=e.catch(1),new W(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return e.stop()}}),e,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:return c=e.sent,p=s.account.address(),l=t.makeApplicationNoOpTxnFromObject({from:p,appIndex:s.validatorAppID,appArgs:[ht("redeem")],note:Tt.getAppCallTxnNoteWithClientName(s.contractVersion),accounts:[u],foreignAssets:0==s.asset2ID?[s.asset1ID,s.poolTokenID]:[s.asset1ID,s.asset2ID,s.poolTokenID],suggestedParams:c}),d=0===o?t.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:D,suggestedParams:c}),m=t.assignGroupID([f,l,d]),e.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(e){var n=e.suggestedParams,r=e.stakingAppID,a=e.initiatorAddr,s=e.liquidityAssetID,o=e.program,i=e.amount,u=t.encodeUint64(i),c=t.encodeUint64(o.id);return t.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[ht("commit"),u],note:$([ht("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function Yr(){return(Yr=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.client,s=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,e.next=3,r.getTransactionParams().do();case 3:if(l=e.sent,d=Jr({suggestedParams:l,stakingAppID:s,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){e.next=10;break}return m=t.makeApplicationNoOpTxnFromObject({appIndex:s,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[ht("log_balance")]}),f=t.assignGroupID([d,m]),e.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return e.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.ALGO_ASSET=V,exports.ALGO_ASSET_ID=j,exports.ASSET_OPT_IN_PROCESS_TXN_COUNT=1,exports.AddLiquidity=Kn,exports.BASE_MINIMUM_BALANCE=S,exports.Bootstrap=yn,exports.CONTRACT_VERSION=C,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=F,exports.MINIMUM_BALANCE_REQUIRED_PER_APP=N,exports.MINIMUM_BALANCE_REQUIRED_PER_ASSET=O,exports.MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA=L,exports.MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE=R,exports.OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT=1,exports.POOL_TOKEN_UNIT_NAME=X,exports.PoolStatus=Se,exports.REDEEM_PROCESS_TXN_COUNT=3,exports.RemoveLiquidity=_r,exports.Swap=Qr,exports.SwapQuoteError=E,exports.SwapQuoteErrorType=b,exports.SwapQuoteType=Zt,exports.SwapType=Ot,exports.V1_1AddLiquidityTxnIndices=wn,exports.V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT=5,exports.V1_1_REMOVE_LIQUIDITY_TXN_COUNT=Zn,exports.V2AddLiquidityTxnIndices=En,exports.V2AddLiquidityType=bn,exports.V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT=$n,exports.applySlippageToAmount=ct,exports.calculateAccountMinimumRequiredBalance=ye,exports.calculatePriceImpact=se,exports.calculateSwapRate=ae,exports.combineAndRegroupSignerTxns=function(){for(var e=arguments.length,n=new Array(e),a=0;a=t.minimum_required_balance},exports.isAccountOptedIntoApp=function(t){var e=t.appID;return t.accountAppsLocalState.some((function(t){return t.id===e}))},exports.isSwapAssetInAmountLow=ne,exports.isSwapQuoteErrorCausedByAmount=le,exports.poolUtils=We,exports.prepareCommitTransactions=function(t){return Yr.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Ur.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=xe,exports.tinymanJSSDKConfig=Tt; diff --git a/src/swap/v2/router/constants.ts b/src/swap/v2/router/constants.ts index 12317486..dee195ac 100644 --- a/src/swap/v2/router/constants.ts +++ b/src/swap/v2/router/constants.ts @@ -3,8 +3,7 @@ import {SwapType} from "../../constants"; export const SWAP_ROUTER_APP_ID: Record = { testnet: 184778019, - // This is temporary APP ID for testing only, remove it when the actual mainnet APP ID is deployed - mainnet: 1071281873 + mainnet: 1083651166 }; /** From c7ed62f4b52b7970c35ebe20b0858c0a34999e78 Mon Sep 17 00:00:00 2001 From: anlerkan Date: Thu, 13 Apr 2023 16:18:36 +0300 Subject: [PATCH 39/39] chore: bump up major version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index f1d41b09..d967fae8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@tinymanorg/tinyman-js-sdk", - "version": "2.1.1", + "version": "3.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@tinymanorg/tinyman-js-sdk", - "version": "2.1.1", + "version": "3.0.0", "license": "MIT", "dependencies": { "algosdk": "^2.1.0", diff --git a/package.json b/package.json index 01a33fac..889fbc5d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tinymanorg/tinyman-js-sdk", - "version": "2.1.1", + "version": "3.0.0", "description": "Tinyman JS SDK", "author": "Tinyman Core Team", "license": "MIT",